UNIT1 c
UNIT1 c
• scanf() is used to take input from the user, while printf() is used to display output to the user.
• It cannot be a keyword.
• It is case-sensitive.
#include <stdio.h>
int main()
return 0;
Output:
Negative
• Time complexity refers to the time an algorithm takes to execute as a function of the size of the
input.
2. Assign b to a.
• Answer: A = 14.
1. Finiteness
2. Definiteness
3. Input
4. Output
5. Effectiveness
Pseudocode:
Integer p, q, r, sum
Read p, q, r
Set sum = p + q + r
if ((p NOT EQUALS 0) and (sum EQUALS 11) and (q EQUALS 4) and (r NOT EQUALS 0))
Print "Success"
Otherwise
Print "Fail"
End if
Output:
Fail
12 marks----------------------------------------------------------------------------------
1. Explain in detail the various data types used in C, and write a C program to calculate the sum of five non-
negative numbers.
C language supports a rich set of data types. They are classified into:
#include <stdio.h>
int main() {
return 0;
This program prompts the user to input five non-negative numbers and then calculates and prints their sum.
1. Preprocessor Directives:
o These are instructions given to the preprocessor. They are used to include header files, define
constants, etc.
2. Global Declarations:
o Variables and functions that are declared outside of any function, typically at the top.
3. Main Function:
4. Local Declarations:
6. Return Statement:
o Indicates the termination of the main() function and returns control to the operating system.
Example:
C Tokens are the smallest units in C programs. They can be classified as:
1. Keywords:
o Reserved words that have special meaning in C. Example: int, return, if.
2. Identifiers:
3. Constants:
4. String Literals:
5. Operators:
o Example: +, -, *, /
6. Punctuation:
o Symbols used to separate statements and define the structure of the program.
o Example: {, }, ;, ,
4. Illustrate the basic organization of a computer system and its components with a neat sketch.
A computer system can be broadly divided into hardware and software components. Here is an illustration:
1. Hardware:
2. Software:
Sketch:
+---------------------------------+
| Input Devices |
+---------------------------------+
+---------------------------------+
+---------------------------------+
+---------------------------------+
| Memory (RAM) |
+---------------------------------+
+---------------------------------+
+---------------------------------+
V
+---------------------------------+
+---------------------------------+
In C, constants are fixed values used in the program that do not change during execution.
1. Integer Constants:
2. Floating-point Constants:
3. Character Constants:
4. String Constants:
o Example: "Hello"
5. Escape Constants:
The basic structure of a computer system consists of hardware, software, and users interacting with the system.
2. Software: Operating systems and application programs that interact with the hardware.
3. Users: People who interact with the system via input devices and receive output.
Sketch:
7. Summarize the concepts of algorithms, flowcharts, and pseudocode, providing suitable examples for each.
4. Display sum
5. End
o Example:
o Start
o Read A, B, C
o Print A
o Else if B > C
o Print B
o Else
o Print C
o End
o Example: +, -, *, /, %
o a + b: Addition
o a == b: Checks if a is equal to b.
o a && b:
Logical AND
4. Assignment Operator: Used to assign values.
o Example: =
o a = 5: Assigns 5 to a.
o a++: Increments a by 1.
9. Define flowchart. Explain its symbols in detail and draw the flowchart to find the greatest of three numbers.
Flowchart Definition:
A flowchart is a diagram that represents the flow of a process or algorithm, showing the steps and the logical flow of
control. It uses standardized symbols to illustrate the sequence of operations.
Flowchart Symbols:
1. Oval (Terminal Symbol): Used to represent the start and end of a process.
4. Diamond (Decision Symbol): Used for decision-making, represents a condition (true or false).
+------------------+
| Start |
+------------------+
+------------------+
| Read A, B, C |
+------------------+
+-----------------------+
Yes | No
V |
+------------------+ +-------------------+
| Print A | | Is B > C? |
+------------------+ +-------------------+
| |
V V
+------------------+ +-------------------+
| End | | Print B |
+------------------+ +-------------------+
+------------------+
| End |
+------------------+
10. Summarize the purpose of a flowchart and explain its uses. What are the basic symbols used in flowcharts?
Draw a flowchart to find the smallest of three numbers.
Purpose of a Flowchart:
• Clarifies Process: Helps in understanding the sequence of steps and the logic of a program or process.
• Improves Communication: Easy to understand and communicate the logic of a program or algorithm.
• Error Detection: Helps in identifying logical errors and inefficiencies early in the development stage.
• Documentation: Acts as a visual representation of a process for future reference or for others to
understand easily.
• Oval (Terminal Symbol): Used to mark the start and end of the process.
+------------------+
| Start |
+------------------+
+------------------+
| Read A, B, C |
+------------------+
+-----------------------+
+-----------------------+
Yes | No
V |
+------------------+ +-------------------+
| Print A | | Is B < C? |
+------------------+ +-------------------+
| |
V V
+------------------+ +-------------------+
| End | | Print B |
+------------------+ +-------------------+
+------------------+
| End |
+------------------+
11. Explain in detail the various data types used in C, and write a C program to calculate the sum of five non-
negative numbers.
Data Types in C
In C programming, data types define the type of data that a variable can hold. C provides several built-in data types,
which can be categorized as follows:
o int: Used to store integers (whole numbers) without decimal points. Size typically 4 bytes.
o float: Used to store single precision floating-point numbers (decimal values). Size typically 4
bytes.
o double: Used to store double precision floating-point numbers (decimal values with more
precision than float). Size typically 8 bytes.
o Structure: A user-defined data type that groups different data types together.
o Union: Similar to structures but uses the same memory space for all members.
o void: Represents a function that does not return any value or a pointer that doesn't point to any
specific data type.
#include <stdio.h>
int main() {
return 0;
}
Explanation:
Structure of a C Program
1. Preprocessor Directives:
o These are instructions that are processed by the preprocessor before the actual compilation. For
example, #include <stdio.h> is used to include the standard input-output header file.
2. Global Declarations:
o Variables, constants, and other resources that can be used throughout the program.
3. Main Function:
o The main() function is the starting point of any C program. Every C program must have this
function.
4. Function Definitions:
o Other functions that perform specific tasks, if needed. Functions can be called from main() or
other functions.
o Inside the main function (or other functions), you write the logic, including variable declarations,
conditional statements, loops, and function calls.
int main() {
// Variable declarations
// Input operation
// Output operation
display();
// Function definition
void display() {
1. Preprocessor Directive:
o #include <stdio.h>: Includes the standard input-output library necessary for using printf and scanf.
2. Global Declarations:
o The display() function is declared above the main() function, which is a prototype that tells the
compiler about the function's name and return type.
3. Main Function:
o It declares two integer variables num1 and num2, takes input for these variables, computes their
sum, and displays the result.
o A user-defined function display() is also called inside the main() function to show how functions
work.
4. Function Definitions:
o The display() function simply prints a message. This shows how functions are defined and called in
C.
13. Write a C program to input electricity unit consumption and calculate the total electricity bill according to
the given condition:
C Program:
#include <stdio.h>
int main() {
scanf("%f", &units);
} else {
bill = 100 * 0.50 + 100 * 0.75 + 100 * 1.20 + (units - 300) * 1.50;
return 0;
}
This program calculates the total electricity bill based on the consumption and applies the appropriate surcharge.