Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 65

Name – Jatin YADAV

Roll no – 33
Branch – CSE
Campus – Navi Mumbai

Chapter-1
Overview of C
Exercises

Review Questions
1.1 State whether the following statements are true or false.

(a) Every line in a C program should end with a semicolon - False

(b)The closing brace of the main() in a program is the logical end of the
program – True

(c)Comments cause the computer to print the text enclosed between /* and */
when executed – False

(d)Every c program ends with an END word-False

(e)A printfstatement can generate only one line of output-False

(f)The purpose of the header file such as stdio.his to store the source
code of a program- False

(g)A line in a program may have more than one statement- True

(h)Syntax errors will be detected by the compiler-True

(i)In C language lowercase letters are significant-True

(j)main() is where the program begins its execution-True

1.2 Which of the following statements are true?


(a) Every C program must have at least one user-defined function – False

(b) Declaration section contains instructions to the computer – True

(c) Only one function may be named main( ) – True

1.3 Which of the following statements about comments are false?

(a) Comments serve as internal documentation for programmers- True

(b) In C, we can have comments inside comments- False

(c) Use of comments reduces the speed of execution of a program- True

(d) A comment can be inserted in the middle of a statement- True

1.4 Fill in the blanks with appropriate words in each of the following
statements.

(a) Every program statement in a C program must end with a _semicolon_.

(b) The __printf__________ Function is used to display the output on the


screen.

(c)The ____math.h________ header file contains mathematical functions.

(d) The escape sequence character ____\n________ causes the cursor to move
to the next line on the screen.

Programming Exercises
1.1 Write a program to display the equation of a line in the form ax + by = c for
a = 5, b = 8 and c = 18.

Ans -

#include <stdlib.h>
#include <stdio.h>
int main()
{

printf("5x + 8y = 18");
return 0;
}

Output – 5x + 8y = 18

1.2 Write a program that will print your mailing address in the following form:

First line : Name

Second line : Door No, Street

Third line : City, Pin code


Ans –

#include <stdio.h>

#include <stdlib.h>

int main()

printf("------Form------\n");

printf("Name - Bhaskar\n");

printf("Door No - 1 Street - Farmhouse\n");

printf("City- Datia Pin code - 475661\n");

return 0;

Output –

------Form------

Name - Bhaskar

Door No - 1 Street - Farmhouse

City- Datia Pin code - 475661

1.3Write a program to output the following multiplication table:

5 x 1 = 5

5 x 2 = 10

5 x 3 = 15

. .

. .

5 x 10 = 50

Ans –

#include <stdio.h>

#include <stdlib.h>

int main()
{

int i,j=5,b;

for(i=1;i<=10;i++)

b = j * i;

printf("%d x %d = %d\n",j,i,b);

return 0;

Output - 5 x 1 = 5

5 x 2 = 10

5 x 3 = 15

5 x 4 = 20

5 x 5 = 25

5 x 6 = 30

5 x 7 = 35

5x 8 = 40

5x 9 = 45

5x 10 = 50

1.4Given the values of three variables a, b and c, write a program to compute and
display the value of x, where x = a / (b-c) - Execute your program for the
following values:

(a) a = 250, b = 85, c = 25


(b) a = 300, b = 70, c = 70
Comment on the output in each case.

Ans –

#include <stdio.h>

#include <stdlib.h>

int bhas1(int a,intb,int c);

int bhas2(int a,intb,int c);

int main()
{

bhas1(250,85,25);

bhas2(300,70,70);

return 0;

int bhas1(int a,intb,int c)

int x;

x = a/(b-c);

printf("%d\n", x);

int bhas2(int a,intb,int c)

//int x;

//x = a/(b-c);

printf("can't divide by zero");

Output-

can't divide by zero

1.5 Relationship between Celsius and Fahrenheit is governed by the formula F =


(9C/5)+32 .Write a program to convert the temperature (a) from Celsius to
Fahrenheit and (b) from Fahrenheit to Celsius.

Ans-

#include <stdio.h>

#include <stdlib.h>

float C_to_F(float TC);


float F_to_C(float TF);

int main()

C_to_F(70);

F_to_C(80);

return 0;

float C_to_F(float TC)

float TF;

TF = (TC * 1.8) + 32;

printf("Temperature in Fahrenheit is: %6.2f\n", TF);

float F_to_C(float TF)

float TC;

TC = (TF - 32)/1.8;

printf("Temperature in degree Celsius is: %6.2f", TC);

Output –

Temperature in Fahrenheit is: 158.00

Temperature in degree Celsius is: 26.67

1.6 Given the radius of a circle, write a program to compute and display its area.
Use a symbolic constant to define the p value and assume a suitable value for
radius.

Ans –

#include <stdio.h>

#include <stdlib.h>
#define PI 3.142857143

int main()

float Area;

float radius = 4;

Area = PI * radius * radius;

printf("Area of circle of given radius is %f", Area);

return 0;

Output - Area of circle of given radius is 50.285713

1.7 Given two integers 20 and 10, write a program that uses a function add( ) to
add these two numbers and sub( ) to find the difference of these two numbers and
then display the sum and difference in the following form:

20 + 10 = 30

20 – 10 = 10

Ans –

#include <stdio.h>

#include <stdlib.h>

int add(int a,int b);

int sub(int a,int b);

int main()

add(20,10);

sub(20,10);

return 0;

}
int add(int a,int b)

int c;

c = a + b;

printf("20 + 10 = %d\n", c);

int sub(int a,int b)

int c;

c = a - b;

printf("20 - 10 = %d", c);

Output –

20 + 10 = 30

20 - 10 = 10

1.8Write a program using one print statement to print the pattern of asterisks as
shown below:

* *

* * *

* * * *

Ans –

#include <stdio.h>

#include <stdlib.h>

int main()

int i, j, rows;

printf("Enter the number of rows: ");

scanf("%d", &rows);
for (i = 1; i<= rows; ++i)

for (j = 1; j <= i; ++j)

printf(" *");

printf("\n");

return 0;

Output - Enter the number of rows: 4

* *

* * *

* * * *

1.10 Write a program that will print the following figure using suitable
characters.

|--------------------|
|--------------------|

|--------------------|
|--------------------|

|--------------------| >>------------- >|--------------------|

|--------------------|
|--------------------|

|--------------------|
|--------------------|

Ans –

#include <stdio.h>

#include <stdlib.h>

int main()

{
printf("|--------------------|
|--------------------|\n");

printf("|--------------------|
|--------------------|\n");

printf("|--------------------| >>------------->|--------------------|\n");

printf("|--------------------|
|--------------------|\n");

printf("|--------------------|
|--------------------|\n");

return 0;

Output - |--------------------|
|--------------------|

|--------------------|
|--------------------|

|--------------------| >>--------------
>|--------------------|

|--------------------|
|--------------------|

|--------------------|
|--------------------|

1.11 Area of a triangle is given by the formula A = S(S-a) (S-b) (S-c) Where a, b
and c are sides of the triangle and 2S = a + b + c. Write a program to compute the
area of the triangle given the values of a, b and c.

Ans –

#include <stdio.h>

#include <stdlib.h>

#include <math.h>

int main()

float a,b,c,S,A;

a = 100;
b = 90;

c = 60;

S = (a + b + c)/2;

A = sqrt(S * (S-a) * (S-b) * (S-c));

printf("Area of a triangle: %f", A);

return 0;

Output - Area of a triangle: 2666.341064

1.12 Write a program to display the following simple arithmetic calculator .

x = y =

sum = Difference =

Product = Division =

Ans-

#include <stdio.h>

#include <stdlib.h>

int main()

double num1,num2;

char op;

printf("Enter a number: ");

scanf("%lf", &num1);

printf("Enter operator: ");

scanf(" %c", &op);

printf("Enter a number: ");

scanf("%lf", &num2);
if(op == '+')

printf("%f", num1+num2);

else if(op == '-')

printf("%f", num1-num2);

else if(op == '/')

printf("%f", num1/num2);

else if(op == '*')

printf("%f", num1*num2);

else

printf("Invalid Operator");

return 0;

Output –

Enter a number: 3

Enter operator: +

Enter a number: 8

The calculated number is 11.000000

1.13 Distance between two points (x1, y1) and (x2, y2) is governed by the formula
D2 = (x2 – x1) 2 + (y2 – y1) 2 Write a program to compute D given the
coordinates of the points.
Ans -

#include <stdio.h>

#include <stdlib.h>

#include <math.h>

int main()

int x1,y1,x2,y2,D;

x1 = 4;

x2 = 6;

y1 = 10;

y2 = 12;

D = sqrt(pow((x2 - x1),2) + pow((y2 - y1),2));

printf("The value of D is %d", D);

return 0;

Output - The value of D is 2

1.14 A point on the circumference of a circle whose center is (o, o) is (4,5).


Write a program to compute perimeter and area of the circle. (Hint: use the
formula given in the Ex. 1.11)

Ans –

#include <stdio.h>

#include <stdlib.h>

#include <math.h>

#define PI 3.14

int main()

float x1,y1,x2,y2,r,P,A;
x1 = 0;

x2 = 4;

y1 = 0;

y2 = 5;

r = sqrt(pow((x2 - x1),2) + pow((y2 - y1),2));

P = 2 * PI * r;

A = PI * r * r;

printf("Perimeter of Circle is %f\n", P);

printf("Area of Circle is %f\n", A);

return 0;

Output - Perimeter of Circle is 40.211620

Area of Circle is 128.740005

1.15 The line joining the points (2,2) and (5,6) which lie on the circumference of
a circle is the diameter of the circle. Write a program to compute the area of the
circle.

Ans –

#include <stdio.h>

#include <stdlib.h>

#include <math.h>

#define PI 3.14

int main()

float r,D,A,x1 = 2,x2 = 5,y1 = 2,y2 = 6;

D = sqrt(pow((x2 - x1),2) + pow((y2 - y1),2));


r = D/2;

A = PI * r * r;

printf("Area of a Circle is %f", A);

return 0;

Output - Area of a Circle is 19.625000

Chapter-2
Constants, Variablesand
DataTypes
Exercises

Review Questions
2.1 State whether the following statements are true or false.

(a)All variables must be given a type when they are declared - True
(b)ANSI C treats the variables name and Name to be same - False

(c)Character constants are coded using double quotes - False

(d)The keyword void is a data type in C - True

(e)Declarations can appear anywhere in a program - False

(f)Initialization is the process of assigning a value to a variable at the


time of declaration –False

(g)The scanf function can be used to read only one value at a time - False

(h)Any valid printable ASCII character can be used in an identifier - False

(i)The underscore can be used anywhere in an identifier - True

(j)Floating point constants, by default, denote float type values - False

(k)Like variables, constants have a type - True

(l)All static variables are automatically initialized to zero –True

2.2 Fill in the blanks with appropriate words.

(a) A variable can be made constant by declaring it with the qualifier


__constant______ at the time of initialization.

(b)___255____ is the largest value that an unsigned short int type variable
can store.

(c)A global variable is also known as __external______ variable.

(d)The keyword _Int____ can be used to create a data type identifier.

ProgrammingExercises

2.1 Write a program to determine and print the sum of the following harmonic series for a given value
of n: 1+ 1/2 +1/3 +....+ 1/n
The value of n should be given interactively through the terminal.
Ans –
#include <stdio.h>
#include <stdlib.h>

double sum(int n)
{
double i, s = 0.0;
for (i = 1; i<= n; i++)
{
s = s + 1/i;
}

return s;
}

int main()
{
int n = 5;
printf("Sum is %f", sum(n));
return 0;
}
Output - Sum is 2.283333

2.2 Write a program to read the price of an item in decimal form (like 15.95) and print the output in
paise (like 1595 paise).
Ans –
#include <stdio.h>
#include <stdlib.h>

int main()
{
double Ruppes;
double Paise;
printf("Enter the price of an item in Rs: ");
scanf("%lf", &Ruppes);
Paise = Ruppes * 100;
printf("Output in paise: %f", Paise);
return 0;
}
Output - Enter the price of an item in Rs: 45
Output in paise: 4500.000000

2.3 Write a program that prints the even numbers from 1 to 100.
Ans –
#include <stdio.h>
#include <stdlib.h>

int main()
{
int i;
for(i=1;i<=100;i++)
{
if(i % 2 == 0)
{
printf("%d\n", i);
}
}
return 0;
}
Output –
2
4
6
8
10
12
14
16
18
20
22
24
26
28
30
32
34
36
38
40
42
44
46
48
50
52
54
56
58
60
62
64
66
68
70
72
74
76
78
80
82
84
86
88
90
92
94
96
98
100

2.4 Write a program that requests two float type numbers from the user and then divides the first
number by the second and display the result along with the numbers.
Ans –
#include <stdio.h>
#include <stdlib.h>

int main()
{
float a,b,c;
printf("Enter a first float number: ");
scanf("%f",&a);
printf("Enter a second float number: ");
scanf("%f",&b);
c = a/b;
printf("%f/%f = %f",a,b,c);

return 0;
}
Output –
Enter a first float number: 6.6
Enter a second float number: 8.6
6.600000/8.600000 = 0.767442

2.5 The price of one kg of rice is Rs. 16.75 and one kg of sugar is Rs. 15. Write a program to get these
values from the user and display the prices as follows:
*** LIST OF ITEMS ***
Item Price
Rice Rs 16.75
Sugar Rs 15.00
Ans –
#include <stdio.h>
#include <stdlib.h>

int main()
{
float pr1,pr2;
printf("Enter price of 1kg of rice -> ");
scanf("%f",&pr1);
printf("Enter price of 1kg of Sugar -> ");
scanf("%f",&pr2);
printf("***List OF ITEMS***\n");
printf("Item Price\n");
printf("Rice Rs %5.2f\n",pr1);
printf("Sugar Rs %5.2f\n",pr2);
return 0;
}
Output –
Enter price of 1kg of rice ->20
Enter price of 1kg of Sugar ->30
***List OF ITEMS***
Item Price
Rice Rs 20.00
Sugar Rs 30.00

2.7 Write a program to do the following: (a) Declare x and y as integer variables and z as a short
integer variable. (b) Assign two 6 digit numbers to x and y (c) Assign the sum of x and y to z (d)
Output the values of x, y and z Comment on the output
Ans –
#include <stdio.h>
#include <stdlib.h>

int main()
{
int x,y;
unsigned short z;
x = 654498;
y = 576932;
z = x + y;
printf("The value of x is -> %d\n",x);//Print the value of x
printf("The value of y is -> %d\n",y);//Print the value of y
printf("The value of z is -> %i\n",z);//Print the value of z
return 0;
}
Output- The value of x is ->654498
The value of y is ->576932
The value of z is ->51782

2.8 Write a program to read two floating point numbers using a scanf statement, assign their sum to an
integer variable and then output the values of all the three variables.
Ans –
#include <stdio.h>
#include <stdlib.h>

int main()
{
float x,y;
int a;
printf("Enter a floating number1 -> ");
scanf("%f",&x);
printf("Enter an another floating number2 -> ");
scanf("%f",&y);

a= x + y;

printf("The value of floating number1 is -> %f\n",x);


printf("The value of floating number2 is -> %f\n",y);
printf("The Sum of floating number1 and floating number2 is -> %d\n",a);

return 0;
}
Output - Enter a floating number1 ->5.8
Enter an another floating number2 ->7.8
The value of floating number1 is -> 5.800000
The value of floating number2 is -> 7.800000
The Sum of floating number1 and floating number2 is ->13

2.9 Write a program to illustrate the use of typedef declaration in a program.


Ans –
// Structure using typedef:

#include <stdio.h>
#include <string.h>

typedef struct student


{
int Roll_No;
char name[20];
float percentage;
} status;

int main()
{
status record;
record.Roll_No = 38;
strcpy(record.name, "Bhaskar");
record.percentage = 86.5;
printf(" Roll no is: %d \n", record.Roll_No);
printf(" Name is: %s \n", record.name);
printf(" Percentage is: %f \n", record.percentage);
return 0;
}
Output –
Roll no is: 38
Name is: Bhaskar
Percentage is: 86.500000

2.10 Write a program to illustrate the use of symbolic constants in a real-life application.
Ans –
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define PI 3.14 //Using Symbolic constant

int main()
{
float r,D,A,x1 = 2,x2 = 5,y1 = 2,y2 = 6;

D = sqrt(pow((x2 - x1),2) + pow((y2 - y1),2));


r = D/2;
A = PI * r * r;
printf("Area of a Circle is %f", A);
return 0;
}
Output – Area of a Circle is 19.625000

Operators and Expressions


Chapter-3

Review Questions
3.1State whether the following statements are true or false.
(a)The expression !(x<=y) is same as the expression x>y – True
(b) A unary expression consists of only one operand with no operators – False
(c) All arithmetic operators have the same level of precedence – False
(d) An expression statement is terminated with a period - False
(e) The operators <=, >= and != all enjoy the same level of priority – False
(f) The modulus operator % can be used only with integers – True
(g) In C, if a data item is zero, it is considered false – True
(h) During the evaluation of mixed expressions, an implicit cast is generated automatically-
True
(i) An explicit cast can be used to change the expression – True
(j) Associativity is used to decide which of several different expressions is evaluated first
-False
(k) Parentheses can be used to change the order of evaluation expressions- True
(l) During modulo division, the sign of the result is positive, if both the operands are of the
same sign- True
3.2Fill in the blanks with appropriate words.
(a) The expression containing all the integer operands is called_arithmetic_______
expression.
(b) C supports as many as __6_____relational operators.
(c) The __sizeof_________operator returns the number of bytes the operand occupies.
(d) _Precedence________is used to determine the order in which different operators in an
expression are evaluated.
(e) An expression that combines two or more relational expressions is termed as
___logical_______ expression.
(f) The use of _implicit type_______ on a variable can change its type in the memory.
(g) The order of evaluation can be changed by using __parenthesis____ in an expression.
(h) The operator ___%______cannot be used with real operands.

Programming Exercises
3.1 Given the values of the variables x, y and z, write a program to rotate their values such that x has
the value of y, y has the value of z, and z has the value of x.
Ans-
#include <stdio.h>
#include <stdlib.h>

int main()
{
int x,y,z;

y = 4;
x = y;
printf("The value of x is %d\n",x);
z = 8;
y = z;
printf("The value of y is %d\n",y);
x = 12;
z = x;
printf("The value of z is %d\n",z);
return 0;
}
Output – The value of x is 4
The value of y is 8
The value of z is 12
3.4 Write a program that will obtain the length and width of a rectangle from the user and compute its
area and perimeter.
Ans –
#include <stdio.h>
#include <stdlib.h>

int main()
{
int l,w,A,P;

printf("Enter the length of a rectangle: ");


scanf("%d",&l);

printf("Enter the width of a rectangle: ");


scanf("%d",&w);

A = l * w;
P = 2 * ( l + w );

printf("Area of the rectangle: %d\n",A);


printf("Perimeter of the rectangle: %d",P);

return 0;
}
Output - Enter the length of a rectangle: 5
Enter the width of a rectangle: 6
Area of the rectangle: 30
Perimeter of the rectangle: 22

3.5 Given an integer number, write a program that displays the number as follows: [LO 3.3 H] First
line : all digits Second line : all except first digit Third line : all except first two digits ……. Last line :
The last digit For example, the number 5678 will be displayed as:
5678
678
78
8
Ans –
#include <stdio.h>
#include <stdlib.h>

int main()
{
int i,j;
for( i = 0; i<= 9; i++)
{
for(j = i; j<=9; j++)
{
printf("%d ",j);
}
printf("\n");
}
return 0;
}
Output - 0 1 2 3 4 5 6 7 8 9
123456789
23456789
3456789
456789
56789
6789
789
89
9

3.6 The straight-line method of computing the yearly depreciation of the value of an item is given by
Depreciation = (Purchase Price - Salvage Value) /Years of Service - Write a program to determine the
salvage value of an item when the purchase price, years of service, and the annual depreciation are
given.
Ans –
#include <stdio.h>
#include <stdlib.h>

int main()
{
int Purchase_Price,Salvage_Value,Years,Dep;

Purchase_Price = 40;
Salvage_Value = 10;
Years = 2;

Dep = (Purchase_Price - Salvage_Value)/Years;


printf("The value of the depreciation: %d",Dep);
return 0;
}
Output - The value of the depreciation: 15

3.9 In inventory management, the Economic Order Quantity for a single item is given by EOQ = 2 ¥ ¥
demand rate setup costs holding cost per itemper unit time and the optimal Time Between Orders
TBO = 2 ¥ ¥ setup costs demand rate holding cost per unit time.
Ans –
#include <stdio.h>
#include <stdlib.h>
#include<math.h>

int main()
{
int EOQ,TBO,dr,su,hcpiut;

printf("Enter value of demand rate: ");


scanf("%d",&dr);
printf("Enter value of setup costs: ");
scanf("%d",&su);
printf("Enter value of holding cost per item per unit time: ");
scanf("%d",&hcpiut);

EOQ = sqrt((2 * dr * su)/hcpiut);


TBO = sqrt((2 * su)/(dr * hcpiut));

printf("The value of Economic Order Quantity: %d\n",EOQ);


printf("The value of Time Between Orders: %d\n",TBO);

return 0;
}
Output –
Enter value of demand rate: 45
Enter value of setup costs: 67
Enter value of holding cost per item per unit time: 32
The value of Economic Order Quantity: 13
The value of Time Between Orders: 0

3.10 For a certain electrical circuit with an inductance L and resistance R, the damped natural
frequency is given by Frequency = 1 4 2 2 LC R C .It is desired to study the variation of this
frequency with C (capacitance). Write a program to calculate the frequency for different values of C
starting from 0.01 to 0.1 in steps of 0.01.
Ans –
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
void main()
{
float L,R,C,frequency;

printf("Enter inductance L:");


scanf("%f",&L);

printf("\\nEnter resistance R:");


scanf("%f",&R);

printf("Enter capacitance C:");


scanf("%f",&C);

for(C=0.01;C<=0.1;C+=0.01)
{
frequency=sqrt((1/(L*C))-((R*R)/(4*C*C)));
printf("Frequency is:%f",frequency);
printf("\n");
}

3.11Write a program to read a four digit integer and print the sum of its digits. Hint: Use / and %
operators.
Ans –
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a,b,c,d,sum;

printf("Enter any four digits number: ");


scanf("%d%d%d%d",&a,&b,&c,&d);
sum = a + b + c + d;
printf("%d",sum);
return 0;
}
Output –
Enter any four digits number: 1 4 6 4
15

3.12 Write a program to print the size of various data types in C.


Ans –
#include <stdio.h>
#include <stdlib.h>

int main()
{
printf("Size of int in bytes is: %d\n",sizeof(int));
printf("Size of float in bytes is: %d\n",sizeof(float));
printf("Size of double in bytes is: %d\n",sizeof(double));
printf("Size of char in bytes is: %d\n",sizeof(char));
return 0;
}
Output- Size of int in bytes is: 4
Size of float in bytes is: 4
Size of double in bytes is: 8
Size of char in bytes is: 1
3.13 Given three values, write a program to read three values from keyboard and print out the largest
of them without using if statement.
Ans –
#include <stdio.h>
#include <stdlib.h>

int main()
{
int a,b,c,x;
printf("Enter the 3 numbers: ");
scanf("%d %d %d",&a,&b,&c);
x = (a>b && a>c)?a:((b>c && b>a)?b:c);
printf("The maximum number is %d",x);
return 0;
}
Output - Enter the 3 numbers: 12 45 10
The maximum number is 45

3.14 Write a program to read two integer values m and n and to decide and print whether m is a
multiple of n.
Ans –
#include <stdio.h>
#include <stdlib.h>

int main()
{
int m,n;
printf("Enter two numbers: ");
scanf("%d %d",&m,&n);
if(m%n == 0)
{
printf("%d is a multiple of %d",m,n);
}
else
{
printf("Sorry!");
}
return 0;
}
Output – Enter two numbers: 4 2
4 is a multiple of 2

3.15 Write a program to read three values using scanf statement and print the following results:
(a) Sum of the values
(b) Average of the three values
(c) Largest of the three
(d) Smallest of the three
Ans –
#include <stdio.h>
#include <stdlib.h>

int Sum();
int Average();
int Largest();
int Smallest();

int main()
{
Sum();
Average();
Largest();
Smallest();
return 0;
}
int Sum()
{
int SUM,a,b,c;
printf("Enter the three numbers for sum: ");
scanf("%d %d %d",&a,&b,&c);
SUM = a + b + c;
printf("The sum of three numbers is: %d\n",SUM);
}
int Average()
{
int AVERAGE,x,y,z;
printf("Enter the three numbers for average: ");
scanf("%d %d %d",&x,&y,&z);
AVERAGE = (x + y + z)/3;
printf("The average of three numbers is: %d\n",AVERAGE);
}
int Largest()
{
int A,B,C;
printf("Enter three numbers to find Largest number: ");
scanf("%d %d %d",&A,&B,&C);
if(A > B && A > C)
{
printf("%d is the largest number\n",A);
}
else if(B > A && B > C)
{
printf("%d is the largest number\n",B);
}
else
{
printf("%d is the largest number\n",C);
}
}
int Smallest()
{
int X,Y,Z;
printf("Enter three numbers to find Smallest number: ");
scanf("%d %d %d",&X,&Y,&Z);
if(X < Y && X < Z)
{
printf("%d is the smallest number\n",X);
}
else if(Y < X && Y < Z)
{
printf("%d is the smallest number\n",Y);
}
else
{
printf("%d is the smallest number\n",Z);
}
}
Output -Enter the three numbers for sum: 45 56 54
The sum of three numbers is: 155
Enter the three numbers for average: 23 33 43
The average of three numbers is: 33
Enter three numbers to find Largest number: 12 34 19
34 is the largest number
Enter three numbers to find Smallest number: 18 90 12
12 is the smallest number

3.17 Write a program to print a table of sin and cos functions for the interval from 0 to 180 degrees in
increments of 15 a shown here.
Ans –
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main()
{
float x;
int i;
printf("x(degrees) sin(x) cos(x)\n");
printf("------------------------------------\n");
for(i=0;i<=180;i=i+15)
{
x = i * (3.14/180);
printf("%d %5.2f %5.2f\n",i,sin(x),cos(x));
}
return 0;
}
Output –
x(degrees) sin(x) cos(x)
------------------------------------
0 0.00 1.00
15 0.26 0.97
30 0.50 0.87
45 0.71 0.71
60 0.87 0.50
75 0.97 0.26
90 1.00 0.00
105 0.97 -0.26
120 0.87 -0.50
135 0.71 -0.71
150 0.50 -0.87
165 0.26 -0.97
180 0.00 -1.00

3.18 Write a program to compute the values of square-roots and squares of the numbers 0 to 100 in
steps 10 and print the output in a tabular form as shown below.
Ans –
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main()
{
int i;
printf("Number Square-root Square\n");
for(i=0;i<=100;i=i+10)
{
printf("%d %5.2f %d\n",i,sqrt(i),i*i);
}
return 0;
}
Output –
Number Square-root Square
0 0.00 0
10 3.16 100
20 4.47 400
30 5.48 900
40 6.32 1600
50 7.07 2500
60 7.75 3600
70 8.37 4900
80 8.94 6400
90 9.49 8100
100 10.00 10000

3.19 Write a program that determines whether a given integer is odd or even and displays the number
and description on the same line.
Ans –
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a;
printf("Enter a positive integer: ");
scanf("%d",&a);
if(a%2 == 0)
{
printf("The number is even");
}
else
{
printf("The number is odd");
}
return 0;
}
Output- Enter a positive integer: 34
The number is even

Chapter - 4
Managing Input and
Output Operations

Review Questions
4.1 State whether the following statements are true or false.
(a) The purpose of the header file is to store the programs created by the users – False
(b) The C standard function that receives a single character from the keyboard is getchar –
True
(c) The input list in a scanf statement can contain one or more variables – True
(d) The scanf function cannot be used to read a single character from the keyboard –False
(e) The getchar cannot be used to read a line of text from the keyboard – False
(f) Variables form a legal element of the format control string of a printf statement – True
(g) The format specification %+ –8d prints an integer left-justified in a field width of 8 with a
plus sign, if the number is positive – True
(h) If the field width of a format specifier is larger than the actual width of the value, the
value is printed right-justified in the field – True
(i) The format specification %5s will print only the first 5 characters of a given string to be
printed –False
(j) When an input stream contains more data items than the number of specifications in a
scanf statement, the unused items will be used by the next scanf call in the program- False
(k) Format specifiers for output convert internal representations for data to readable
characters – True
(l) The print list in a printf statement can contain function calls – True

4.2 Fill in the blanks in the following statements.


(a) The __%hd_______ specification is used to read or write a short integer.
(b) For reading a double type value, we must use the specification _____%lf____ .
(c) For using character functions, we must include the header file __top_______ in the
program.
(d) To print the data left-justified, we must use __%6.4_______ in the field specification.
(e) The conversion specifier ____%b_____ is used to print integers in hexadecimal form.
(f) The specification _________ is used to read a data from input list and discard it without
assigning it to many variable.
(g) The specification %[ ] is used for reading strings that contain __string data_______ .
(h) By default, the real numbers are printed with a precision of _____6____ decimal places.
(i) The specifier _____%f____ prints floating-point values in the scientific notation.
(j) The specification ____%c_____ may be used in scanf to terminate reading at the
encounter of a particular character.

Programming Exercises
4.1 Given the string “WORDPROCESSING”, write a program to read the string from the terminal
and display the same in the following formats:
(a) WORD PROCESSING
(b) WORD PROCESSING
(c) W.P.
Ans –
#include <stdio.h>
#include <stdlib.h>

int main()
{
char a[20],b[20],c[20],d[20];
printf("Enter the string: ");
scanf("%s %s", a,b);
printf("Enter the first character of each word (word and processing): ");
scanf("%s %s", c,d);
printf("(a) %s %s\n",a,b);
printf("(b) %s\n %s\n",a,b);
printf("(c) %s.%s.\n",c,d);

return 0;
}
Output - Enter the string: WORD PROCESSING
Enter the first character of each word (word and processing): W P
(a) WORD PROCESSING
(b) WORD
PROCESSING
(c) W.P.

4.2 Write a program to read the values of x and y and print the results of the following expressions in
one line:
(a) (x+y)/(x-y)
(b) (x +y)/ 2
(c) (x+y)(x-y)
Ans –
#include <stdio.h>
#include <stdlib.h>

int main()
{
int x,y,a,b,c;
printf("Enter the values of x: ");
scanf("%d",&x);
printf("Enter the values of y: ");
scanf("%d",&y);

a = (x+y)/(x-y);
b = (x+y)/2;
c = (x+y) * (x-y);

printf("%d ", a);


printf("%d ", b);
printf("%d ", c);

return 0;
}
Output - Enter the values of x: 34
Enter the values of y: 24
5 29 580

4.3 Write a program to read the following numbers, round them off to the nearest integers and print
out the results in integer form:
35.7 50.21 – 23.73 – 46.45
Ans –
#include <stdio.h>
#include <stdlib.h>
int main()
{

int p,i;

float a;

for(i=1;i<=4;i++)

printf("ENTER REAL NUMBER FOR GET NEAREST INTEGER NUMBER: ");


scanf("%f",&a);

if(a>=0)

p=a+0.5;

else

p=a-0.5;

printf("NEAREST INTEGER NUMBER OF %f IS= %d\n",a,p);

return 0;
}
Output - ENTER REAL NUMBER FOR GET NEAREST INTEGER NUMBER: 35.7
NEAREST INTEGER NUMBER OF 35.700001 IS= 36
ENTER REAL NUMBER FOR GET NEAREST INTEGER NUMBER: 50.21
NEAREST INTEGER NUMBER OF 50.209999 IS= 50
ENTER REAL NUMBER FOR GET NEAREST INTEGER NUMBER: -23.73
NEAREST INTEGER NUMBER OF -23.730000 IS= -24
ENTER REAL NUMBER FOR GET NEAREST INTEGER NUMBER: -46.75
NEAREST INTEGER NUMBER OF -46.750000 IS= -47

4.5 Write an interactive program to demonstrate the process of multiplication. The program should
ask the user to enter two two-digit integers and print the product of integers as shown below.
Ans-
#include <stdio.h>
#include <stdlib.h>

int main()
{
int a,b,m;
printf("Enter two digit number: ");
scanf("%d",&a);
printf("Enter two digit number: ");
scanf("%d",&b);
if(a < 100 && b < 100 && a >= 10 && b >=10)
{
m = a * b;
printf("%d",m);
}
else
{
printf("Ops You haven't entered two digit number");
}

return 0;
}
Output - Enter two digit number: 56
Enter two digit number: 78
4368
4.7 Write a program that prints the value 10.45678 in exponential format with the following
specifications:
(a) correct to two decimal places;
(b) correct to four decimal places; and
(c) correct to eight decimal places
Ans-
#include <stdio.h>
#include <stdlib.h>

int main()
{
float a = 10.45678;

printf("%4.2f\n",a);
printf("%4.4f\n",a);
printf("%4.8f\n",a);

return 0;
}
Output - 10.46
10.4568
10.45678043

4.8 Write a program to print the value 345.6789 in fixed-point format with the following
specifications:
(a) correct to two decimal places;
(b) correct to five decimal places; and
(c) correct to zero decimal places.
Ans –
#include <stdio.h>
#include <stdlib.h>

int main()
{
float a = 345.6789;

printf("%4.2f\n",a);
printf("%4.5f\n",a);
printf("%4.0f\n",a);

return 0;
}
Output - 345.68
345.67889
346

4.9 Write a program to read the name ANIL KUMAR GUPTA in three parts using the scanf
statement and to display the same in the following format using the printf statemen.
(a) ANIL K. GUPTA
(b) A.K. GUPTA
(c) GUPTA A.K.
Ans-
#include <stdio.h>
#include <stdlib.h>

int main()
{
char a[10],b[10],c[10];
char x = 'A';
char y = 'K';
printf("Enter the string: ");
scanf("%s%s%s",a,b,c);
printf("(a)%s %c.%s\n",a,y,c);
printf("(b)%c.%c. %s\n",x,y,c);
printf("(c)%s %c.%c.\n",c,x,y);

return 0;
}
Output –
Enter the string: ANIL KUMAR GUPTA
(a)ANIL K.GUPTA
(b)A.K. GUPTA
(c)GUPTA A.K.

4.10 Write a program to read and display the following table of data.
Name Code Price
Fan 67831 1234.50
Motor 450 5786.70
The name and code must be left-justified and price must be right-justified.
Ans-
#include <stdio.h>
#include <stdlib.h>

int main()
{
int code1,code2;
float price1,price2;
char name1[10],name2[10];
printf("Enter first name,code,price: ");
scanf("%s %d %f",name1,&code1,&price1);
printf("Enter second name,code,price: ");
scanf("%s %d %f",name2,&code2,&price2);
printf("Name Code Price\n");
printf("%s %d %6.2f\n",name1,code1,price1);
printf("%s %d %6.2f\n",name2,code2,price2);
return 0;
}
Output- Enter first name,code,price: Fan 67831 1234.50
Enter second name,code,price: Motor 450 5786.70
Name Code Price
Fan 67831 1234.50
Motor 450 5786.70

Chapter - 5
Decisionmaking
andBranching

ReviewQuestions
5.1 State whether the following are true or false:
(a)A switch expression can be of any type – False
(b)A program stops its execution when a break statement is encountered –True
(c)Each case label can have only one statement - False
(d)The default case is required in the switch statement- True
(e) When if statements are nested, the last else gets associated with the nearest if without
an else - False
(f) One if can have more than one else clause - False
(g) Each expression in the elseif must test the same variable - False
(h) A switch statement can always be replaced by a series of if..else statements - True
(i) Any expression can be used for the if expression - False
(j) The predicate !( (x >= 10)¦(y = = 5) ) is equivalent to (x < 10) && ( y !=5 ) – False

5.2 Fill in the blanks in the following statements.


(a) The _&&______ operator is true only when both the operands are true.
(b) Multiway selection can be accomplished using an elseif statement or the
__switch________ statement.
(c) The __default____ statement when executed in a switch statement causes immediate
exit from the structure.
(d) The expression ! (x ! = y ) can be replaced by the expression __(x==y)______.
(e) ) The ternary conditional expression using the operator ?: could be easily coded using
__if-else__statement.
ProgrammingExercises
5.1 Write a program to determine whether a given number is ‘odd’ or ‘even’ and print the message
NUMBER IS EVEN or NUMBER IS ODD (a) without using else option , and (b) with else option.
Ans –
#include <stdio.h>
#include <stdlib.h>

int num1();
int num2();

int main()
{
int a;
printf("Enter a positive integer: ");
scanf("%d",&a);
num1(a);
num2(a);
return 0;
}
int num1(int a)
{

(a%2 == 0)?printf("(a)The Number is Even\n"):printf("(a)The Number is Odd\n");

int num2(int a)
{
if(a%2 == 0)
{
printf("(b)The Number is Even\n");
}
else
{
printf("(b)The Number is Odd\n");
}

}
Output - Enter a positive integer: 4
(a)The Number is Even
(b)The Number is Even

5.2Write a program to find the number of and sum of all integers greater than 100 and less than 200
that are divisible by 7.
Ans –
#include <stdio.h>
#include <stdlib.h>

int main()
{
int i,sum=0;
printf("The number are greater than 100 and less than 200 are divisible by 7 are:- \n");

for(i=101;i<200;i++)
{
if(i%7 == 0)
{
printf("%d\n",i);
sum = sum + i;
}
}
printf("Sum of all integers: %d\n",sum);

return 0;
}
Output – The number are greater than 100 and less than 200 are divisible by 7 are:-
105
112
119
126
133
140
147
154
161
168
175
182
189
196
Sum of all integers: 2107

5.3A set of two linear equations with two unknowns x1 and x2 is given below: [LO 5.2 H] ax1 + bx2
= m cx1 + dx2 = n The set has a unique solution x1 = md bn ad cb - - x2 = na mc ad cb - - provided
the denominator ad – cb is not equal to zero. Write a program that will read the values of constants a,
b, c, d, m, and n and compute the values of x1 and x2. An appropriate message should be printed if ad
– cb = 0.
Ans –
#include <stdio.h>
#include <stdlib.h>

int main()
{
int a,b,c,d,m,n;
float x1,x2;

printf("Enter value of a: ");


scanf("%d",&a);
printf("Enter value of b: ");
scanf("%d",&b);
printf("Enter value of c: ");
scanf("%d",&c);
printf("Enter value of d: ");
scanf("%d",&d);
printf("Enter value of m: ");
scanf("%d",&m);
printf("Enter value of n: ");
scanf("%d",&n);
if(((a*d)-(c*b)) != 0)
{
x1 = ((m*d)-(b*n))/((a*d)-(c*b));
printf("%f\n",x1);
x2 = ((n*a)-(m*c))/((a*d)-(c*b));
printf("%f\n",x2);
}
else
{
printf("Sorry!Thedinominator is equal to zero");
}

return 0;
}
Output - Enter value of a: 45
Enter value of b: 87
Enter value of c: 90
Enter value of d: 234
Enter value of m: 2455
Enter value of n: 200
206.000000
-78.000000
5.4 Given a list of marks ranging from 0 to 100, write a program to compute and print the number of
students: [LO 5.2 M] (a) who have obtained more than 80 marks, (b) who have obtained more than 60
marks, (c) who have obtained more than 40 marks, (d) who have obtained 40 or less marks, (e) in the
range 81 to 100, (f) in the range 61 to 80, (g) in the range 41 to 60, and (h) in the range 0 to 40. The
program should use a minimum number of if statements.
Ans –
#include <stdio.h>
#include <stdlib.h>

int main()
{
int a[100],n,i,c1,c2,c3,c4;
c1=c2=c3=c4=0;
printf("Enter the no of students in the class: ");
scanf("%d",&n);
printf("Enter the marks of %d student: ",n);

for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
if(a[i]>80 && a[i]<=100)
{
c1++;
}
else if(a[i]>60 && a[i]<=80)
{
c2++;
}
else if(a[i]>40 && a[i]<=60)
{
c3++;
}
else
{
c4++;
}
}
printf("\nStudent whose marks in between 81 and 100 are :%d\n",c1);
printf("\nStudent whose marks in between 61 and 80 are :%d\n",c2);
printf("\nStudent whose marks in between 41 and 60 are :%d\n",c3);
printf("\nStudent whose marks less then 40 or equal to 40 are :%d\n",c4);
return 0;
}
Output - Enter the no of students in the class: 4
Enter the marks of 4 student: 78
34
90
67

Student whose marks in between 81 and 100 are :1

Student whose marks in between 61 and 80 are :2

Student whose marks in between 41 and 60 are :0

Student whose marks less then 40 or equal to 40 are :1

5.5 Admission to a professional course is subject to the following conditions: [LO 5.2 M] (a) Marks in
Mathematics >= 60 (b) Marks in Physics >= 50 (c) Marks in Chemistry >= 40 (d) Total in all three
subjects >= 200 or Total in Mathematics and Physics >= 150 Given the marks in the three subjects,
write a program to process the applications to list the eligible candidates.
Ans-
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a,b,c,d,i;
printf("Admission for professional course for 50 student at a time\n");
printf("----------------------------------------------------------------------------\n");
for(i=1;i<=50;i++)
{
printf("Enter a marks in mathematics: ");
scanf("%d",&a);
printf("Enter a marks in physics: ");
scanf("%d",&b);
printf("Enter a marks in chemistry: ");
scanf("%d",&c);
printf("Enter the total marks of three subject: ");
scanf("%d",&d);

if(a>=60 && b>=50 && c>=40 && (a+b+c)>=200)


{
printf("You are eligible for professional course\n");
}
else
{
printf("Sorry!You are not eligible for professional course\n");
}
}
return 0;
}
Output - Admission for professional course for 50 student at a time
----------------------------------------------------------------------------
Enter a marks in mathematics: 78
Enter a marks in physics: 89
Enter a marks in chemistry: 98
Enter the total marks of three subject: 265
You are eligible for professional course
5.7 Shown below is a Floyd’s triangle.
1
23
456
7 8 9 10
11 .. .. .. 15 . .
79 .. .. .. .. .. .. 91
(a) Write a program to print this triangle.
(b) Modify the program to produce the following form of Floyd’s triangle.
1
01
101
0101
10101

Ans – (a)
#include <stdio.h>
#include <stdlib.h>

int main()
{
int i,j,m=1;

for(i=1;i<=13;i++)
{
for(j=1;j<=i;j++)
{
printf("%d ",m);
m++;
}
printf("\n");
}
return 0;
}
Output –1
23
456
7 8 9 10
11 12 13 14 15
16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31 32 33 34 35 36
37 38 39 40 41 42 43 44 45
46 47 48 49 50 51 52 53 54 55
56 57 58 59 60 61 62 63 64 65 66
67 68 69 70 71 72 73 74 75 76 77 78
79 80 81 82 83 84 85 86 87 88 89 90 91

(b)
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i,j;

for(i=1; i<=5; i++)


{
for(j=1; j<=i; j++)
{
if((i+j)%2==1)
{
printf("0");
}
else
{
printf("1");
}
}
printf("\n");
}
return 0;
}
Output – 1
01
101
0101
10101

5.9 Write a program that will read the value of x and evaluate the following function y = 1 0 0 0 1 0
for x for x for x < = - < Ï Ì Ô Ó Ô using (a) nested if statements, (b) else if statements,
Ans –
(a)
#include <stdio.h>
#include <stdlib.h>

int main()
{
int x,y;
printf("Enter the value of x: ");
scanf("%d",&x);
if(x == 0)
{
y = 0;
printf("The value of y is %d",y);
}
else
{
if(x>0)
{
y = 1;
printf("The value of y is %d",y);
}
else
{
y = -1;
printf("The value of y is %d",y);
}
}
return 0;
}
Output - Enter the value of x: 4
The value of y is 1

(b)
#include <stdio.h>
#include <stdlib.h>

int main()
{
int x,y;
printf("Enter the value of x: ");
scanf("%d",&x);
if(x == 0)
{
y = 0;
printf("The value of y is %d",y);
}
else if(x>0)
{
y = 1;
printf("The value of y is %d",y);
}
else
{
y = -1;
printf("The value of y is %d",y);
}
return 0;
}
Output - Enter the value of x: 4
The value of y is 1

5.10 Write a program to compute the real roots of a quadratic equation [LO 5.2 H] ax2 + bx + c = 0
The roots are given by the equations x1 = – b + b ac a 2 4 2 - x2 = – b – b ac a 2 4 2 - The program
should request for the values of the constants a, b and c and print the values of x1 and x2. Use the
following rules: (a) No solution, if both a and b are zero (b) There is only one root, if a = 0 (x = –c/b)
(c) There are no real roots, if b2 – 4 ac is negative (d) Otherwise, there are two real roots Test your
program with appropriate data so that all logical paths are working as per your design. Incorporate
appropriate output messages.
Ans –
#include <stdio.h>
#include <stdlib.h>

int main()
{
int a,b,c;
float x1,x2;
printf("Enter value of a: ");
scanf("%d",&a);
printf("Enter value of b: ");
scanf("%d",&b);
printf("Enter value of c: ");
scanf("%d",&c);

if(a==0 && b == 0)
{
x1= ((-b) + (sqrt(((b*b)-(4*a*c)))/(2*a)));
x2= ((-b) - (sqrt(((b*b)-(4*a*c))/(2*a))));
printf("The value of x1 is %f\n",x1);
printf("The value of x2 is %f\n",x2);
printf("No solution\n");
}
else if(a == 0)
{
x1= ((-b) + (sqrt(((b*b)-(4*a*c)))/(2*a)));
x2= ((-b) - (sqrt(((b*b)-(4*a*c))/(2*a))));
printf("The value of x1 is %f\n",x1);
printf("The value of x2 is %f\n",x2);
}
else if(((b*b)-(4*a*c)) < 0)
{
x1= ((-b) + (sqrt(((b*b)-(4*a*c)))/(2*a)));
x2= ((-b) - (sqrt(((b*b)-(4*a*c))/(2*a))));
printf("The value of x1 is %d\n",x1);
printf("The value of x2 is %d\n",x2);
}
else
{
x1= ((-b) + (sqrt(((b*b)-(4*a*c)))/(2*a)));
x2= ((-b) - (sqrt(((b*b)-(4*a*c))/(2*a))));
printf("The value of x1 is %f\n",x1);
printf("The value of x2 is %f\n",x2);
printf("No Real Roots");
}
return 0;
}
Output –
Enter value of a: 34
Enter value of b: 21
Enter value of c: 13
The value of x1 is 0
The value of x2 is 0

5.11 Write a program to read three integer values from the keyboard and displays the output stating
that they are the sides of right-angled triangle.
Ans –
#include <stdio.h>
#include <stdlib.h>

int main()
{
int a,b,c;
printf("Enter the value of a: ");
scanf("%d",&a);
printf("Enter the value of b: ");
scanf("%d",&b);
printf("Enter the value of c: ");
scanf("%d",&c);
printf(" /|\n");
printf(" / |\n");
printf("%d / | %d\n",a,b);
printf(" / |\n");
printf(" /____|\n");
printf(" %d", c);
return 0;
}
Output –
Enter the value of a: 5
Enter the value of b: 4
Enter the value of c: 9
/|
/|
5 / |4
/ |
/____|
9

5.13 Write a program to compute and display the sum of all integers that are divisible by 6 but not
divisible by 4 and lie between 0 and 100. The program should also count and display the number of
such values.
Ans –
#include <stdio.h>
#include <stdlib.h>

int main()
{
int i,sum=0;
printf("Integers\n");
for(i=1;i<100;i++)
{
if((i%6) == 0 && (i%4) != 0)
{

printf("%d\n",i);
sum = sum + i;

}
}
printf("Sum of all above integers\n");
printf("%d\n",sum);
return 0;
}
Output –
Integers
6
18
30
42
54
66
78
90
Sum of all above integers
384

5.14 Write an interactive program that could read a positive integer number and decide whether the
number is a prime number and display the output accordingly. [LO 5.2 M] Modify the program to
count all the prime numbers that lie between 100 and 200. NOTE: A prime number is a positive
integer that is divisible only by 1 or by itself.
Ans –
#include <stdio.h>
#include <stdlib.h>

int main()
{
int b;
printf("Enter a positive integer: ");
scanf("%d",&b);

if(b!=2 && (b%2)!=0 && b!=1)


{
printf("%d is a prime number\n",b);
}
else
{
printf("%d is not a prime number",b);
}

return 0;
}
Output-
Enter a positive integer: 4
4 is not a prime number

You might also like