Unit I QB
Unit I QB
PART A
The function scanf() is used for formatted input from the standard input and provides many of
the conversion facilities.
It is used for formatted output to standard output device,( screen) . The format specification
contains string and the data to be output, as the arguments(parameter) to the printf() function.
for(intilialization;test-condition;increment/decrem
ent)
{
Body of the loop;
}
Function Callings: The user-defined functions can be called inside any functions like main(),
userdefined function, etc.
Function Definition: The function definition block is used to define the user-defined functions
with statements.
while(test_condition)
{
Body of the loop;
}
Keywords have fixed meaning and these meaning cannot be changed. These keywords
can be used only for their intended purpose, they cannot be used as programmer-defined
identifiers.
The following are examples of few keywords are predefined by C
auto double int struct
break else if switch
case enum do while
PART B &C
1.Define a data type. Mention the different data types supported by C language, giving an
example to each
Each variable in C has an associated data type. It specifies the type of data that the variable can
store like integer, character, floating, double, etc. Each data type requires different amounts of
memory and has some specific operations which can be performed over it. The data type is a
collection of data with values having fixed values, meaning as well as its characteristics.
The data types in C can be classified as follows:
Types Description
Primitive Data Primitive data types are the most basic data types that are used for
Types representing simple values such as integers, float, characters, etc.
User Defined
The user-defined data types are defined by the user himself.
Data Types
The data types that are derived from the primitive or built-in
Derived Types
datatypes are referred to as Derived Data Types.
Different data types also have different ranges up to which they can store numbers. These ranges
may vary from compiler to compiler. Below is a list of ranges along with the memory
requirement and format specifiers on the 32-bit GCC compiler.
Size
Range Format
Data Type (bytes)
Specifier
unsigned
2 0 to 65,535 %hu
short int
unsigned long
4 0 to 4,294,967,295 %lu
int
unsigned long
8 0 to 18,446,744,073,709,551,615 %llu
long int
float 4 %f
1.2E-38 to 3.4E+38
double 8 %lf
1.7E-308 to 1.7E+308
long double 16
3.4E-4932 to 1.1E+4932 %Lf
Output
Integer value with positive data: 9
Integer value with negative data: -9
Integer value with an unsigned int data: 89
Integer value with an long int data: 99998
Character Data Type
Character data type allows its variable to store only a single character. The size of the character is
1 byte. It is the most basic data type in C. It stores a single character and requires a single byte of
memory in almost all compilers.
● Range: (-128 to 127) or (0 to 255)
● Size: 1 byte
● Format Specifier: %c
Syntax of char
The char keyword is used to declare the variable of character type:
char var_name;
Example of char
Output
Value of a: a
Value of a after increment is: b
Value of c: c
Float Data Type
In C programming float data type is used to store floating-point values. Float in C is used to store
decimal and exponential values. It is used to store decimal numbers (numbers with floating point
values) with single precision.
Output
9.000000
2.500000
0.000200
Double Data Type
A Double data type in C is used to store decimal numbers (numbers with floating point values)
with double precision. It is used to define numeric values which hold numbers with decimal
values in C.
The double data type is basically a precision sort of data type that is capable of holding 64 bits of
decimal numbers or floating points. Since double has more precision as compared to that float
then it is much more obvious that it occupies twice the memory occupied by the floating-point
type. It can easily accommodate about 16 to 17 digits after or before a decimal point.
● Range: 1.7E-308 to 1.7E+308
● Size: 8 bytes
● Format Specifier: %lf
Syntax of Double
The variable can be declared as double precision floating point using the double keyword:
double var_name;
Example of Double
// C Program to demonstrate
// use of double data type
#include <stdio.h>
int main()
{
double a = 123123123.00;
double b = 12.293123;
double c = 2312312312.123123;
printf("%lf\n", a);
printf("%lf\n", b);
printf("%lf", c);
return 0;
}
Output
123123123.000000
12.293123
2312312312.123123
Void Data Type
The void data type in C is used to specify that no value is present. It does not provide a result
value to its caller. It has no values and no operations. It is used to represent nothing. Void is used
in multiple ways as function return type, function arguments as void, and pointers to void.
Syntax:
// function return type void
void exit(int check);
// Function without any parameter can accept void.
int print(void);
// memory allocation function which
// returns a pointer to void.
void *malloc (size_t size);
Example of Void
// C program to demonstrate
// use of void pointers
#include <stdio.h>
int main()
{
int val = 30;
void* ptr = &val;
printf("%d", *(int*)ptr);
return 0;
}
Output
30
Call by Value
Call by value in C is where in the arguments we pass value and that value can be used in function
for performing the operation. Values passed in the function are stored in temporary memory so
the changes performed in the function don’t affect the actual value of the variable passed.
Example:
// C Program to implement
// Call by value
#include <stdio.h>
// Call by value
int sum(int x, int y)
{
int c;
c = x + y;
// Integer value retured
return c;
}
// Driver Code
int main()
{
// Integer Declared
int a = 3, b = 2;
// Function Called
int c = sum(a, b);
printf("Sum of %d and %d : %d", a, b, c);
return 0;
}
Output
Sum of 3 and 2 : 5
Call by Reference
Call by reference is the method in C where we call the function with the passing address as
arguments. We pass the address of the memory blocks which can be further stored in a pointer
variable that can be used in the function. Now, changes performed in the values inside the
function can be directly reflected in the main memory.
Example:
// C Program to implement
// Call by reference
#include <stdio.h>
// Call by reference
void swap(int* x, int* y)
{
int temp = *x;
*x = *y;
*y = temp;
}
// Driver Code
int main()
{
// Declaring Integer
int x = 1, y = 5;
printf("Before Swapping: x:%d , y:%d\n", x, y);
// Calling the function
swap(&x, &y);
printf("After Swapping: x:%d , y:%d\n", x, y);
return 0;
}
Output
Before Swapping: x:1 , y:5
After Swapping: x:5 , y:1
Types of Function According to Arguments and Return Value
Functions can be differentiated into 4 types according to the arguments passed and value returns
these are:
1. Function with arguments and return value
2. Function with arguments and no return value
3. Function with no arguments and with return value
4. Function with no arguments and no return value
1. Function with arguments and return value
Syntax:
Function declaration : int function ( int );
Function call : function( x );
Function definition:
int function( int x )
{
statements;
return x;
}
statements are executed and which are skipped, to loop through statements until a condition is
met, or to jump to a different part of the program.
Characteristics:
○ Control statements serve to control the execution flow of a program.
○ Control statements in C are classified into selection statements, iteration statements, and
jump statements.
○ Selection statements serve to execute code based on a certain circumstance.
○ Iteration statements loop through a code block until a condition is met.
○ Jump statements serve to move control from one section of a program to another.
○ The if statement, for loop, while loop, switch statement, break statement, and continue
statement are C's most widely used control statements.
Usage:
Control statements are used extensively in programming, especially in more complex programs.
By managing the flow of execution based on particular conditions, they enable a programmer to
design more efficient and understandable code.
C Code:
Here is an example of how control statements can be used in C:
1. #include <stdio.h>
2.
3. int main() {
4. int num = 10;
5.
6. if (num > 0) {
7. printf("The number is positive\n");
8. } else {
9. printf("The number is negative\n");
10. }
11.
12. for (int i = 0; i < 5; i++) {
13. printf("%d\n", i);
14. }
15.
16. int i = 0;
17. while (i < 5) {
18. printf("%d\n", i);
19. i++;
20. }
21.
22. switch (num) {
23. case 0:
24. printf("The number is zero\n");
25. break;
26. case 10:
27. printf("The number is ten\n");
28. break;
29. default:
30. printf("The number is not zero or ten\n");
31. break;
32. }
33.
34. return 0;
35. }
Output
The number is positive
0
1
2
3
4
0
1
2
3
4
The number is ten
The above C code demonstrates the use of different control statements such as if-else statements,
switch statements, for loops, while loops and return statements. The code initializes a variable
'num' to 10 and then proceeds to use different control statements to manipulate the flow of the
program based on the value of 'num'.
The first control statement is an if-else statement that examines whether 'num' is greater than
zero. If it is, the program prints to the console, "The number is positive." If not, it displays "The
number is negative." This shows how to utilize selection statements in C.
The for loop is the second control statement used, which sets a variable 'i' to 0 and then
performs a block of code as long as 'i' is less than 5. The program prints the value of 'i' to the
console within the code block. This shows how to utilize iteration statements in C.
The while loop is the third control statement used. It sets a variable 'i' to 0 and then performs a
code block if 'i' is less than 5. The program within the block of code prints the value of 'i' to the
console and increments 'i' by 1. This also shows how to utilize iteration statements in C.
The fourth control statement used is the switch statement, which checks the value of 'num' and
executes different blocks of code depending on its value. If 'num' is 0, the program prints "The
number is zero". If 'num' is 10, the program prints "The number is ten". If 'num' is neither 0 nor
10, the program prints "The number is not zero or ten". This demonstrates the use of switch
statements in C.
Finally, the program ends with a return statement, which returns the value 0 to the operating
system to indicate that the program ran successfully.
In conclusion, the above C code provides a simple example of how different control statements
can be used in a program to manipulate the flow of execution based on certain conditions. It is a
good starting point for beginners to understand the basic concepts of control statements in C.
Advantages:
○ Control statements allow programmers to control the flow of execution in their programs,
enabling it to be simple to develop efficient and understandable code.
○ They allow programmers to handle different scenarios and conditions in their programs.
Disadvantages:
○ Overuse of control statements can make code difficult to read and maintain.
○ Complex control statements can be difficult to debug and can introduce bugs in the code.
4. What is an operator? Explain the arithmetic, relational, logical, and assignment
operators in C language.
An operator is a symbol that operates on a value or a variable. For example: + is an operator to
perform addition.
C has a wide range of operators to perform various operations.
C Arithmetic Operators
An arithmetic operator performs mathematical operations such as addition, subtraction,
multiplication, division etc on numerical values (constants and variables).
Operator Meaning of Operator
* multiplication
/ division
return 0;
}
Run Code
Output
a+b = 13
a-b = 5
a*b = 36
a/b = 2
Remainder when a divided by b=1
In normal calculation, 9/4 = 2.25. However, the output is 2 in the program.
It is because both the variables a and b are integers. Hence, the output is also an integer. The
compiler neglects the term after the decimal point and shows answer 2 instead of 2.25.
The modulo operator % computes the remainder. When a=9 is divided by b=4, the remainder is
1. The % operator can only be used with integers.
Suppose a = 5.0, b = 2.0, c = 5 and d = 2. Then in C programming,
// Either one of the operands is a floating-point number
a/b = 2.5
a/d = 2.5
c/b = 2.5
// Both operands are integers
c/d = 2
return 0;
}
Run Code
Output
++a = 11
--b = 99
++c = 11.500000
--d = 99.500000
Here, the operators ++ and -- are used as prefixes. These two operators can also be used as
postfixes like a++ and a--. Visit this page to learn more about how increment and decrement
operators work when used as postfix.
C Assignment Operators
An assignment operator is used for assigning a value to a variable. The most common
assignment operator is =
Operator Example Same as
= a=b a=b
+= a += b a = a+b
-= a -= b a = a-b
*= a *= b a = a*b
/= a /= b a = a/b
%= a %= b a = a%b
Example 3: Assignment Operators
// Working of assignment operators
#include <stdio.h>
int main()
{
int a = 5, c;
c = a; // c is 5
printf("c = %d\n", c);
c += a; // c is 10
printf("c = %d\n", c);
c -= a; // c is 5
printf("c = %d\n", c);
c *= a; // c is 25
printf("c = %d\n", c);
c /= a; // c is 5
printf("c = %d\n", c);
c %= a; // c = 0
printf("c = %d\n", c);
return 0;
}
Run Code
Output
c=5
c = 10
c=5
c = 25
c=5
c=0
C Relational Operators
A relational operator checks the relationship between two operands. If the relation is true, it
returns 1; if the relation is false, it returns value 0.
Relational operators are used in decision making and loops.
Operator Meaning of Operator Example
== Equal to 5 == 3 is evaluated to 0
return 0;
}
Run Code
Output
5 == 5 is 1
5 == 10 is 0
5 > 5 is 0
5 > 10 is 0
5 < 5 is 0
5 < 10 is 1
5 != 5 is 0
5 != 10 is 1
5 >= 5 is 1
5 >= 10 is 0
5 <= 5 is 1
5 <= 10 is 1
C Logical Operators
An expression containing logical operator returns either 0 or 1 depending upon whether
expression results true or false. Logical operators are commonly used in decision making in C
programming.
Opera Meaning Example
tor
#include <stdio.h>
int main()
{
int a = 5, b = 5, c = 10, result;
return 0;
}
Run Code
Output
(a == b) && (c > b) is 1
(a == b) && (c < b) is 0
(a == b) || (c < b) is 1
(a != b) || (c < b) is 0
!(a != b) is 1
!(a == b) is 0
Explanation of logical operator program
● (a == b) && (c > 5) evaluates to 1 because both operands (a == b) and (c > b) is 1 (true).
● (a == b) && (c < b) evaluates to 0 because operand (c < b) is 0 (false).
● (a == b) || (c < b) evaluates to 1 because (a = b) is 1 (true).
● (a != b) || (c < b) evaluates to 0 because both operand (a != b) and (c < b) are 0 (false).
● !(a != b) evaluates to 1 because operand (a != b) is 0 (false). Hence, !(a != b) is 1 (true).
● !(a == b) evaluates to 0 because (a == b) is 1 (true). Hence, !(a == b) is 0 (false).
5.Write an algorithm/flowchart and C program to compute roots of quadratic equation
Start
Read a, b, c values
Compute d = b2 4ac
if d > 0 then
r1 = b+ sqrt (d)/(2*a)
r2 = b sqrt(d)/(2*a)
Otherwise if d = 0 then
compute r1 = -b/2a, r2=-b/2a
print r1,r2 values
Otherwise if d < 0 then print roots are imaginary
Stop
# include<stdio.h>
# include<math.h>
int main () {
float a,b,c,r1,r2,d;
d= b*b - 4*a*c;
if (d>0) {
r1 = -b+sqrt (d) / (2*a);
r2 = -b-sqrt (d) / (2*a);
printf ("The real roots = %f %f", r1, r2);
}
else if (d==0) {
r1 = -b/(2*a);
r2 = -b/(2*a);
printf ("Roots are equal =%f %f", r1, r2);
}
else
printf("Roots are imaginary");
return 0;
}
6.Write a C program using a function to check whether the given number is palindrome or
not.
Algorithm
A simple method for this problem is to first reverse all the digits of a given number and then
compare the reverse of the number with a given number. If both are the same, then return true,
else false.
1. Declare an integer reversed = 0 and num = original_number
2. Now to reverse the number, do the following while num >= 0
1. reverse = 10 * reverse + num % 10;
2. num /= 10;
3. After getting the reverse number, compare it with the original _number,
4. If the reverse is equal to the original_number, then the number is palindrome.
5. Else, the number is not palindrome.
C program to check whether a number is palindrome or not
#include <stdio.h>
// Driver code
int main()
{
// This is our given number
int original_number = 12321;
}
else {
printf(
" Given number %d is not a palindrome number",
original_number);
}
return 0;
}
7.Write an algorithm and C program to find the sum of numbers from 1 to n‘
Step by step descriptive logic to find sum of n natural numbers.
1. Input upper limit to find sum of natural numbers. Store it in some variable say N.
2. Initialize another variable to store sum of numbers say sum = 0.
3. In order to find sum we need to iterate through all natural numbers between 1 to n.
Initialize a loop from 1 to N, increment loop counter by 1 for each iteration. The
loop structure should look like for(i=1; i<=N; i++).
4. Inside the loop add previous value of sum with i. Which is sum = sum + i.
5. Finally after loop print the value of sum.
#include <stdio.h>
int main()
{
int i, n, sum=0; /* Input upper limit from user */
printf("Enter upper limit: ");
scanf("%d", &n);
/* Find sum of all numbers */
for(i=1; i<=n; i++) { sum += i; }
printf("Sum of first %d natural numbers = %d", n, sum);
return 0; }
8.Write a C program to perform the addition of two matrices.
Step 1: Start
Step 2: Declare matrix mat1[row][col];
and matrix mat2[row][col];
and matrix sum[row][col]; row= no. of rows, col= no. of columns
Step 3: Read row, col, mat1[][] and mat2[][]
Step 4: Declare variable i=0, j=0
Step 5: Repeat until i < row
5.1: Repeat until j < col
sum[i][j]=mat1[i][j] + mat2[i][j]
Set j=j+1
5.2: Set i=i+1
Step 6: sum is the required matrix after addition
Step 7: Stop