You are on page 1of 116

ProgramminginCISBN:9788131760314

Chapter 3: Operators and Expressions


1. Write a program to shift the entered number by three bits left and display the result.
#include<stdio.h>
#include<conio.h>
void main()
{
int x,y;
clrscr();
printf("Enter number to be shifted:");
scanf("%d",&x);
x<<=3;
y=x;
printf("\nThe left shifted data is=%d");
getch();
}

Output:
Enter number to be shifted:12
The left shifted data is=96

2. Write a program to shift the entered number by five bits right and display the result.
#include<stdio.h>
#include<conio.h>
void main()
{
int x,y;
clrscr();
printf("Enter number to be shifted:");
scanf("%d",&x);
x>>=5;
y=x;
printf("\nThe left shifted data is=%d");
getch();
}

Output:
Enter number to be shifted:32
The left shifted data is=1

3. Write a program to mask the most significant digit of the entered number.
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;

ByProf.AshokN.Kamthane

Copyright2012DorlingKindersleyIndiaPvt.Ltd.

ProgramminginCISBN:9788131760314

clrscr();
printf("Enter two integers:");
scanf("%d %d",&a,&b);
c=a % b;
printf("\nThe result after operation (C)=%d",a-c);
getch();

Output:
Enter two integers:18 10
The result after operation (C)=10

4. Write a program to enter two numbers and find the smallest out of them. Use
conditional operator.
#include<stdio.h>
#include<conio.h>
void main()
{
int x,y;
clrscr();
printf("Enter two numbers:");
scanf("%d %d",&x,&y);
if(x<y)
printf("%d is smallest.",x);
else
printf("%d is smallest.",y);
getch();
}

Output:
Enter two numbers:45 32
32 is smallest

5. Write a program to enter a number and carry out modular division operation by 2, 3
and 4 and display the remainders.
#include<stdio.h>
#include<conio.h>
void main()
{
int n;
clrscr();
printf("Enter a number (N):");
scanf("%d",&n);
printf("N mod 2=%d\n",n%2);
printf("N mod 3=%d\n",n%3);
printf("N mod 4=%d\n",n%4);
getch();
}

Output:
ByProf.AshokN.Kamthane

Copyright2012DorlingKindersleyIndiaPvt.Ltd.

ProgramminginCISBN:9788131760314
Enter
N mod
N mod
N mod

a number (N):7
2=1
3=1
4=3

6. Attempt the program with division operation and find the quotients.
#include<stdio.h>
#include<conio.h>
void main()
{
int n;
clrscr();
printf("Enter a number (N):");
scanf("%d",&n);
printf("N/2=%d\n",n/2);
printf("N/3=%d\n",n/3);
printf("N/4=%d\n",n/4);
getch();
}

Output:
Enter a number (N):7
N/2=3
N/3=2
N/4=1

7. Write a program to enter an integer number and display its equivalent values in octal
and hexadecimal.
#include<stdio.h>
#include<conio.h>
void main()
{
int num;
clrscr();
printf("Enter a nubmer:");
scanf("%d",&num);
printf("Decimal=%d\n",num);
printf("Octal=%o\n",num);
printf("Hexadecimal=%X\n",num);
getch();
}

Output:
Enter a nubmer:12
Decimal=12
Octal=14
Hexadecimal=C

ByProf.AshokN.Kamthane

Copyright2012DorlingKindersleyIndiaPvt.Ltd.

ProgramminginCISBN:9788131760314

8. Write a program to convert hexadecimal to decimal numbers. Enter the numbers such
as 0x1c, 0x18, 0xbc, 0xcd.
#include<stdio.h>
#include<conio.h>
void main()
{
int hex;
clrscr();
printf("Enter number in hexadecimal:");
scanf("%X",&hex);
printf("Decimal=%d",hex);
getch();
}

Output:
Enter number in hexadecimal:0X1C
Decimal=28
Enter number in hexadecimal:0X18
Decimal=24
Enter number in hexadecimal:0XBC
Decimal=188
Enter number in hexadecimal:0XCD
Decimal=205

9. Write a program to find the average temperature of five sunny days. Assume the
temperature in Celsius.
#include<stdio.h>
#include<conio.h>
void main()
{
int d1,d2,d3,d4,d5;
float avg;
clrscr();
printf("Enter temperature of five days in celsius:");
scanf("%d %d %d %d %d",&d1,&d2,&d3,&d4,&d5);
avg=(d1+d2+d3+d4+d5)/5.0;
printf("Average temperature of five days=%f",avg);
getch();
}

Output:
Enter temperature of five days in celsius:30 35 32 34 33

ByProf.AshokN.Kamthane

Copyright2012DorlingKindersleyIndiaPvt.Ltd.

ProgramminginCISBN:9788131760314
Average temperature of five days=32.799999

10. Write a program to enter two numbers. Make a comparison between them with a
conditional operator. If the first number is greater than the second perform multiplication
otherwise perform division operation.
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
clrscr();
printf("Enter two numbers:");
scanf("%d %d",&a,&b);
if(a>b)
printf("Multiplication=%d",a*b);
else
printf("Division=%d",a/b);
getch();
}

Output:
Enter two numbers:35 23
Multiplication=805

11. Write a program to calculate the total cost of the vehicle by adding basic cost with (i)
excise duty (15%); (ii) sales tax (10%); (iii) octroi (5%) and (iv) road tax (1%). Input the
basic cost.
#include<stdio.h>
#include<conio.h>
void main()
{
float basic,ex,st,oct,rd,total;
clrscr();
printf("Enter basic cost of vehicle:");
scanf("%f",&basic);
ex=basic*15/100;
st=basic*10/100;
oct=basic*5/100;
rd=basic*1/100;
total=basic+ex+st+oct+rd;
printf("Total cost=%f",total);
getch();
}

Output:
Enter basic cost of vehicle:15000
Total cost=19650.000000

ByProf.AshokN.Kamthane

Copyright2012DorlingKindersleyIndiaPvt.Ltd.

ProgramminginCISBN:9788131760314

12. Write a program to display ASCII equivalents of


(a) A, B,C and a,b,c.
(b) a-C, b-A and c B.
(c) a+c, b*a and c+12.
#include<stdio.h>
#include<conio.h>
void main()
{
char A,B,C,a,b,c;
clrscr();
A='A';
B='B';
C='C';
a='a';
b='b';
c='c';
printf("ASCII equivalents=\n");
printf("A=%d\nB=%d\nC=%d\na=%d\nb=%d\nc=%d\n",A,B,C,a,b,c);
printf("a - C=%d\n",'a'-'C');
printf("b - A=%d\n",'b'-'A');
printf("c - B=%d\n",'c'-'B');
printf("a + c=%d\n",'a'+'c');
printf("b * a=%d\n",'b'*'a');
printf("c + 12=%d\n",'c'+12);
getch();
}

Output:
ASCII equivalents=
A=65
B=66
C=67
a=97
b=98
c=99
a - C=30
b - A=33
c - B=33
a + c=196
b * a=9506
c + 12=111

13. Write a program to enter a number that should be less than 100 and greater than 9.
Display the number in reverse order using modular division and division operation.
ByProf.AshokN.Kamthane

Copyright2012DorlingKindersleyIndiaPvt.Ltd.

ProgramminginCISBN:9788131760314
#include<stdio.h>
#include<conio.h>
void main()
{
int n,d1,d2;
clrscr();
printf("Enter a number:");
scanf("%d",&n);
d1=n%10;
d2=n/10;
n=d1*10+d2;
printf("After reverse number is=%d",n);
getch();
}

Output:
Enter a number:34
After reverse number is=43

14. Write a program to enter a four-digit number. Display the digits of the number in the
reverse order using modular division and division operation. Perform addition and
multiplication of digits.
#include<stdio.h>
#include<conio.h>
void main()
{
int n,d1,d2,d3,d4;
clrscr();
printf("Enter a four digit number:");
scanf("%d",&n);
d1=n%10;
d2=(n/10)%10;
d3=(n/100)%10;
d4=n/1000;
n=d1*1000+d2*100+d3*10+d4;
printf("After reverse number is=%d",n);
getch();
}

Output:
Enter a four digit number:1234
After reverse number is=4321

15. Write a program to display numbers from 0 to 9. Use ASCII range 48 to 59 and
control string %c.
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();

ByProf.AshokN.Kamthane

Copyright2012DorlingKindersleyIndiaPvt.Ltd.

ProgramminginCISBN:9788131760314

printf("Numbers from 0 to 9 with ASCII values:\n");


printf("Numbers\tASCII values\n");
printf("%c\t%d\n",48,48);
printf("%c\t%d\n",49,49);
printf("%c\t%d\n",51,51);
printf("%c\t%d\n",52,52);
printf("%c\t%d\n",53,53);
printf("%c\t%d\n",54,54);
printf("%c\t%d\n",55,55);
printf("%c\t%d\n",56,56);
printf("%c\t%d\n",57,57);
getch();

Output:
Numbers
Numbers
0
1
3
4
5
6
7
8
9

from 0 to 9 with ASCII values:


ASCII values
48
49
51
52
53
54
55
56
57

16. Write a program to evaluate the following expressions and display their results.
(a) x 2 +(2*x3)*(2*x)
(b) x1+y2+z3
assume variables are integers.
#include<stdio.h>
#include<conio.h>
void main()
{
int x,y,z,e;
clrscr();
printf("Enter values of x,y and z:");
scanf("%d %d %d",&x,&y,&z);
printf("x*x+(2*x*x*x)*(2*x)=%d",x*x+(2*x*x*x)*(2*x));
printf("\nx+y*y+z*z*z=%d",x+y*y+z*z*z);
getch();
}

Output:
Enter values of x,y and z:1 2 3
x*x+(2*x*x*x)*(2*x)=5
x+y*y+z*z*z=32

ByProf.AshokN.Kamthane

Copyright2012DorlingKindersleyIndiaPvt.Ltd.

ProgramminginCISBN:9788131760314

17. Write a program to print whether the number entered is even or odd. Use conditional
operator.
#include<stdio.h>
#include<conio.h>
void main()
{
int num,flag;
clrscr();
printf("Enter a number:");
scanf("%d",&num);
num%2==0?printf("Number is even."):printf("Number is odd.");
getch();
}

Output:
Enter a number:8
Number is even.
Enter a number:13
Number is odd.

18. Write a program to print whether the number entered is even or odd. Use conditional
operator.

void main()
{
int a;
clrscr();
printf("Enter a number:");
scanf("%d",&a);
if(a % 2==0)
printf("\nEntered number %d is even ",a);
else
printf("\nEntered number %d is odd ",a);
getche();
}

Output:
Enter a number:99
Entered number 99 is odd

ByProf.AshokN.Kamthane

Copyright2012DorlingKindersleyIndiaPvt.Ltd.

ProgramminginCISBN:9788131760314

Chapter 4: Input and Output in C


1. Write a program to input the rainfall of three consecutive days in CMS and find its
average.
#include<stdio.h>
#include<conio.h>
void main()
{
float r1,r2,r3,avg;
clrscr();
printf("Enter rainfall of three days:");
scanf("%f %f %f",&r1,&r2,&r3);
avg=(r1+r2+r3)/3;
printf("\nAverage rainfall=%.2f",avg);
getch();
}

Output:
Enter rainfall of three days:30 31 33
Average rainfall=31.33

2. Find the simple interest? Inputs are principal amount, period in year and rate of
interest.
#include<stdio.h>
#include<conio.h>
void main()
{
float p,n,r,interest;
clrscr();
printf("Enter principal amount,period and rate:\n");
scanf("%f %f %f",&p,&n,&r);
interest=p*n*r/100;
printf("Simple interest=%.2f",interest);
getch();
}

Output:
Enter principal amount,period and rate:
2000 2 2.5
Simple interest=100.00

3. Write a program to find the total number of minutes of 12 hours.


ByProf.AshokN.Kamthane

Copyright2012DorlingKindersleyIndiaPvt.Ltd.

ProgramminginCISBN:9788131760314

#include<stdio.h>
#include<conio.h>
void main()
{
int hr,min;
clrscr();
printf("Enter hours:");
scanf("%d",&hr);
min=hr*60;
printf("Minutes in 12 hrs=%d",min);
getch();
}

Output:
Enter hours:12
Minutes in 12 hrs=720

4. Find the area and perimeter of (a) square and (b) rectangle. Input the side(s) through
the keyboard.
#include<stdio.h>
#include<conio.h>
void main()
{
float l,b,s,area,perim;
clrscr();
printf("Enter side of square:");
scanf("%f",&s);
area=s*s;
perim=4*s;
printf("Area=%.2f\nPerimeter=%.2f", area,perim);
printf("\n\nEnter length and breadth of rectangle:");
scanf("%f %f",&l,&b);
area=l*b;
perim=2*(l+b);
printf("Area=%.2f\nPerimeter=%.2f", area,perim);
getch();
}

Output:
Enter side of square:4
Area=16.00
Perimeter=16.00
Enter length and breadth of rectangle:20 30
Area=600.00
Perimeter=100.00

ByProf.AshokN.Kamthane

Copyright2012DorlingKindersleyIndiaPvt.Ltd.

ProgramminginCISBN:9788131760314

5. Accept any three numbers and find their squares and cubes.
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
clrscr();
printf("Enter any three numbers:");
scanf("%d %d %d",&a,&b,&c);
printf("\nNo.\tSquare\tCube\n");
printf("%d\t%d\t%d\n",a,a*a,a*a*a);
printf("%d\t%d\t%d\n",b,b*b,b*b*b);
printf("%d\t%d\t%d\n",c,c*c,c*c*c);
getch();
}

Output:
Enter any three numbers:4 5 6
No.
4
5
6

Square
16
25
36

Cube
64
125
216

6. The speed of a van is 80 km/hour. Find the number of hours required for covering a
distance of 500 km? Write a program in this regard.
#include<stdio.h>
#include<conio.h>
void main()
{
int hr,min;
clrscr();
printf("Speed of car=80 km/hour.");
hr=500/80;
if((min=500%80)>59)
{
hr+=1;
min=min%60;
}
printf("\nTime reqd to cover distance of 500 km=%d hrs %d
mins",hr,min);
getch();
}

Output:
Speed of car=80 km/hour.
Time reqd to cover distance of 500 km=6 hrs 20 mins

ByProf.AshokN.Kamthane

Copyright2012DorlingKindersleyIndiaPvt.Ltd.

ProgramminginCISBN:9788131760314

7. Write a program to convert inches into centimeters.


#include<stdio.h>
#include<conio.h>
void main()
{
float inch,cm;
clrscr();
printf("Enter inches=");
scanf("%f",&inch);
cm=inch*2.54;
printf("%.3f inch= %.3f cms",inch,cm);
getch();
}

Output:
Enter inches=32
32.000 inch= 81.3 cms

8. Write a program to enter the name of this book and display it.
#include<stdio.h>
#include<conio.h>
#include<dos.h>
void main()
{
static char name[10];
clrscr();
printf("\n Enter name of the book :");
gets(name);
printf("\n You Entered :");
sleep(5);
puts(name);
getch();
}

Output:
Enter name of the book: Programming in C
You Entered: Programming in C

9. Write a program to store and interchange two float numbers in variables a and b.
#include<stdio.h>
#include<conio.h>
void main()
{
float a,b;
clrscr();

ByProf.AshokN.Kamthane

Copyright2012DorlingKindersleyIndiaPvt.Ltd.

ProgramminginCISBN:9788131760314

printf("Enter two values of a and b:");


scanf("%f %f",&a,&b);
printf("\nBefore interchang:\na=%.2f b=%.2f",a,b);
a=a+b;
b=a-b;
a=a-b;
printf("\n\nAfter interchange:\na=%.2f b=%.2f",a,b);
getch();

Output:
Enter two values of a and b:12 24
Before interchang:
a=12.00 b=24.00
After interchange:
a=24.00 b=12.00

10. Write a program to enter text with gets() and display it using printf() statement.
Also find the length of the text.
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char text[20],n;
clrscr();
printf("Enter your text:");
gets(text);
printf("%s",text);
n=strlen(text);
printf("\nLength of the string:%d",n);
getch();
}

Output:
Enter your text:Progamming in C
Progamming in C
Length of the string:15

11. Write a program to ensure that the subtraction of any two-digit number and its reverse
is always the multiple of nine. For example, entered number is 54 and its reverse is 45.
The difference between them is 9.
#include<stdio.h>
#include<conio.h>

ByProf.AshokN.Kamthane

Copyright2012DorlingKindersleyIndiaPvt.Ltd.

ProgramminginCISBN:9788131760314
void main()
{
int num,num1,d1,d2,diff;
clrscr();
printf("Enter a two digit number:");
scanf("%d",&num);
d1=num%10;
d2=num/10;
num1=d1*10+d2;
printf("Reverse number:%d",num1);
if(num>num1)
diff=num-num1;
else
diff=num1-num;
if(diff==9)
printf("\nThe difference is always 9.");
else
printf("\nThe difference is not always 9.");
getch();
}

Output:
Enter a two digit number:43
Reverse number:34
The difference is always 9.

12. Write a program to convert kilograms into grams.


#include<stdio.h>
#include<conio.h>
void main()
{
float kg,gm;
clrscr();
printf("Enter weight in kg:");
scanf("%f",&kg);
gm=kg*1000;
printf("\n%.3f kg= %.3f gm",kg,gm);
getch();
}

Output:
Enter weight in kg:2.34
2.340 kg= 2340.000 gm

13. Write a program to find the total amount when there are five notes of Rs. 100, three
notes of Rs. 50 and 20 notes of Rs. 20.
#include<stdio.h>
#include<conio.h>
void main()

ByProf.AshokN.Kamthane

Copyright2012DorlingKindersleyIndiaPvt.Ltd.

ProgramminginCISBN:9788131760314
{

int h,f,t,total;
clrscr();
h=100;
f=50;
t=20;
total=h*5+f*3+t*20;
printf("\n Given notes:");
printf("\n %d X 5= %d",h,h*5);
printf("\n %d X 3 = %d",f,f*3);
printf("\n %d X 20= %d",t,t*20);
printf("\n total = %d",total);
getch();

Output:
Given notes:
100 X 5= 500
50 X 3 = 150
20 X 20= 400
total = 1050

14. Write a program to enter the temperature in Fahrenheit and convert it to Celsius.
Formula to be used is tc= ((tf-32)*5)/9 where tc and tf are temperatures in Celsius and
Fahrenheit, respectively.
#include<stdio.h>
#include<conio.h>
void main()
{
float tc,tf;
clrscr();
printf("Enter temperature in fhrenheit:");
scanf("%f",&tf);
tc=((tf-32)*5)/9;
printf("\Temperature in Celsius: %.3f",tc);
getch();
}

Output:
Enter temperature in fhrenheit:98
Temperature in Celsius: 36.667

15. Write a program to display the list of c program files and directories. Use system()
function to execute DOS commands.
#include<conio.h>
#include<stdio.h>
#include<process.h>

ByProf.AshokN.Kamthane

Copyright2012DorlingKindersleyIndiaPvt.Ltd.

ProgramminginCISBN:9788131760314
#include<stdlib.h>
void main()
{
printf("\n Display files with DOS command:");
system(" dir");
getche();
}

16. Write a program to find radius of circle if the area is accepted from the user.
#include<stdio.h>
#include<conio.h>
#include<math.h>
main()
{
float area,rad;
clrscr();
printf("Enter area of circle: ");
scanf("%f",&area);
rad=sqrt(area/3.14);
printf("\nRadius= %.2f",rad);
getch();
}

Output:
Enter area of circle: 6.28
Radius= 1.41

17. Write a program to find length of thread required to form a square if area of square is
taken from user.
#include<stdio.h>
#include<conio.h>
#include<math.h>
main()
{
float area,side;
clrscr();
printf("Enter area of square in sq.meter: ");
scanf("%f",&area);
side=sqrt(area);
printf("\nLength of thread should be %.2f meter",side*4);
getch();
}

Output:
Enter area of square in sq.meter: 4
Length of thread should be 8.00 meter

ByProf.AshokN.Kamthane

Copyright2012DorlingKindersleyIndiaPvt.Ltd.

ProgramminginCISBN:9788131760314

18. Write a program to calculate strike rate of a batsman in an inning. Accept runs and
balls faced from user.
#include<stdio.h>
#include<conio.h>
#include<math.h>
main()
{
int run,ball;
float sr;
clrscr();
printf("Enter runs and balls faced: ");
scanf("%d %d",&run,&ball);
sr=(float)run/ball*100;
printf("\nStrike rate of batsman= %.2f",sr);
getch();
}

Output:
Enter runs and balls faced: 145 120
Strike rate of batsman= 120.83

19. Write a program to accept marks in five subjects of a student and calculate average of
marks.
#include<stdio.h>
#include<conio.h>
main()
{
int m1,m2,m3,m4,m5,total;
float avg,per;
clrscr();
printf("Enter marks in five subjects: ");
scanf("%d %d %d %d %d",&m1,&m2,&m3,&m4,&m5);
total=m1+m2+m3+m4+m5;
avg=(float)total/5;
printf("\nAverage= %.2f",avg);
getch();
}

Output:
Enter marks in five subjects: 45 67 87 59 60
Average= 63.60

ByProf.AshokN.Kamthane

Copyright2012DorlingKindersleyIndiaPvt.Ltd.

ProgramminginCISBN:9788131760314

Chapter 5: Decision Statements


1. Write a program to check whether the blood donor is eligible or not for donating blood.
The conditions laid down are as under. Use if statements As follows:
(a) Age should be greater than 18 years but not more than 55 years.
(b) Weight should be more than 45 kg.
#include<stdio.h>
#include<conio.h>
void main()
{
int age;
float weight;
clrscr();
printf("Enter age and weight of donor:");
scanf("%d %f",&age,&weight);
if(age>=18 && age<=55 && weight>45)
printf("\nPerson is eligible for donating blood.");
else
printf("\nPerson is not eligible for donating blood.");
getch();
}

Output:
Enter age and weight of donor:23 56
Person is eligible for donating blood.

2. Write a program to check whether the voter is eligible for voting or not. If his/her age
is equal to or greater than 18, display message Eligible otherwise Non-eligible. Use
the if statement.
#include<stdio.h>
#include<conio.h>
void main()
{
int age;
clrscr();
printf("Enter age of person to vote:");
scanf("%d",&age);
if(age>=18)
printf("\nEligible.");
else
printf("\nNon-eligible.");
getch();
}

Output:
ByProf.AshokN.Kamthane

Copyright2012DorlingKindersleyIndiaPvt.Ltd.

ProgramminginCISBN:9788131760314
Enter age of person to vote:45
Eligible.

3. Write a program to calculate bill of a job work done as follows. Use if-else
statement.
(a) Rate of typing Rs. 3/page.
(b) Printing of 1st copy Rs. 5/page and later every copy Rs. 3/page.
User should enter the number of pages and print out copies he/she wants.
#include<stdio.h>
#include<conio.h>
void main()
{
float rate_type,rate_print,total;
int copy;
clrscr();
printf("Enter no. of copies:");
scanf("%d",&copy);
rate_type=3;
rate_print=5;
if(copy==1)
{
rate_type=rate_type*copy;
rate_print=rate_print*copy;
total=rate_type+rate_print;
}
else
{
rate_print=3;
rate_type=rate_type*copy;
rate_print=rate_print*copy;
total=rate_type+rate_print;
}
printf("\Cost of total jobwork: Rs. %.2f",total);
getch();
}

Output:
Enter no. of copies: 5
Cost of total jobwork: Rs. 30.00

4. Write a program to calculate the amount of the bill for the following jobs.
(a) Scanning and hardcopy of a passport photo Rs. 5.
(b) Scanning and hardcopies (more than 10) Rs. 3.
#include<stdio.h>
#include<conio.h>
void main()
{
int copy;
float rate,total;

ByProf.AshokN.Kamthane

Copyright2012DorlingKindersleyIndiaPvt.Ltd.

ProgramminginCISBN:9788131760314

clrscr();
printf("Enter no. of copies: ");
scanf("%d",&copy);
if(copy>=10)
{
rate=3;
total=copy * rate;
}
else
{
rate=5;
total=copy * rate;
}
printf("\nTotal cost of jobwork: Rs.%.2f",total);
getch();

Output:
Enter no. of copies: 12
Total cost of jobwork: Rs.36.00

5. Write a program to calculate bill of Internet browsing. The conditions are given below.
(a) 1 Hour 20 Rs.
(b) Hour 10 Rs.
(c) Hours 90 Rs.
Owner should enter number of hours spent by customer.
#include<stdio.h>
#include<conio.h>
void main()
{
float hr,bill;
clrscr();
printf("Enter no. of hours:");
scanf("%f",&hr);
if(hr>=1)
bill=hr * 20;
if(hr==0.5)
bill=10;
printf("\nTotal bill= Rs. %.2f",bill);
getch();
}

Output:
Enter no. of hours: 3
Total bill= Rs. 60.00

ByProf.AshokN.Kamthane

Copyright2012DorlingKindersleyIndiaPvt.Ltd.

ProgramminginCISBN:9788131760314

6. Write a program to enter a character through keyboard. Use switch() case structure
and print appropriate message. Recognize the entered character whether it is vowel,
consonants, or symbol
#include<stdio.h>
#include<conio.h>
#include<ctype.h>
void main()
{
int n;
char ch;
clrscr();
printf("Enter any character: ");
scanf("%c",&ch);
if(ch>='A' && ch<='Z' || ch>='a' && ch<='z')
{
ch=toupper(ch);
if(ch=='A'||ch=='E'||ch=='I'||ch=='O'||ch=='U')
n=1;
else
n=2;
}
else
n=3;
switch(n)
{
case 1:
printf("Character is vowel.");
break;
case 2:
printf("Character is consonant.");
break;
default:
printf("Character is symbol.");
}
getch();
}

Output:
Enter any character: *
Character is symbol.
Enter any character: D
Character is consonant.
Enter any character: e
Character is vowel.

ByProf.AshokN.Kamthane

Copyright2012DorlingKindersleyIndiaPvt.Ltd.

ProgramminginCISBN:9788131760314

7. The table given below is a list of gases, liquids and solids. By entering one by one
substances through the keyboard, recognize their state (gas, liquid and solid).
WATER
OXYGEN
IRON
GOLD

OZONE
PETROL
ICE
MERCURY

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char state[10];
int ch;
clrscr();
printf("Given substances:\n");
printf("1.Water\t 2.Ozone\n");
printf("3.Oxygen 4.Petrol\n");
printf("5.Iron\t 6.Ice\n");
printf("7.Gold\t 8.Mercury\n");
printf("Enter your choice: ");
scanf("%d",&ch);
if(ch==5 || ch==6 ||ch==7)
strcpy(state,"SOLID");
else if(ch==1 || ch==4 || ch==8)
strcpy(state,"LIQUID");
else
strcpy(state,"GAS");
printf("\nThe given substance is %s",state);
getch();
}

Output:
Given substances:
1.Water 2.Ozone
3.Oxygen 4.Petrol
5.Iron
6.Ice
7.Gold
8.Mercury
Enter your choice: 6
The given substance is SOLID

ByProf.AshokN.Kamthane

Copyright2012DorlingKindersleyIndiaPvt.Ltd.

ProgramminginCISBN:9788131760314

8. Write a program to calculate the sum of remainders obtained by dividing with modular
division operations by 2 on 1 to 9 numbers.
#include<stdio.h>
#include<conio.h>
void main()
{
int sum;
clrscr();
sum=0;
sum+=(1%2);
sum+=(2%2);
sum+=(3%2);
sum+=(4%2);
sum+=(5%2);
sum+=(6%2);
sum+=(7%2);
sum+=(8%2);
sum+=(9%2);
printf("Sum of remainders= %d",sum);
getch();
}

Output:
Sum of remainders= 5

ByProf.AshokN.Kamthane

Copyright2012DorlingKindersleyIndiaPvt.Ltd.

ProgramminginCISBN:9788131760314

Chapter 6: Loop Control


1. Write a program to display alphabets as given below.
az by cx dw ev fu gt hs Ir jq kp lo mn nm ol pk qj ri sh tg uf ve wd xc yb za.
#include<stdio.h>
#include<conio.h>
void main()
{
char i,j;
clrscr();
for(i=97,j=122;i<=122,j>=97;i++,j--)
{
printf("%c%c ",i,j);
}
getch();
}

Output:
az by cx dw ev fu gt hs ir jq kp lo mn nm ol pk qj ri sh tg uf ve wd xc
yb za

2. Write a program to display count values from 0 to 100 and flash each digit for one
second. Reset the counter after it reaches to hundred. The procedure is to be repeated.
Use for loop.
#include<stdio.h>
#include<conio.h>
#include<dos.h>
void main()
{
int c;
printf("Count= ");
for(c=0;c<=100;c++)
{
clrscr();
printf("%d",c);
sleep(1);
if(c==100)
c=0;
}
getch();
}

ByProf.AshokN.Kamthane

Copyright2012DorlingKindersleyIndiaPvt.Ltd.

ProgramminginCISBN:9788131760314

3. Develop a program to simulate seconds in a clock. Put the 60 dots on the circle with
equal distance between each other and mark them 0 to 59. A seconds pointer is to be
shown with any symbol. Also print the total number of revolution made by seconds
pointer.
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>
void main()
{
int gd=DETECT,gm,x,y,t;
initgraph(&gd,&gm,"C:\\TC\\BGI\\");/* Give the path of BGI folder*/
setcolor(4);
x=getmaxx();
y=getmaxy();
x=x/2;
y=y/2;
circle(x,y,150);
t=0;
while(t<360)
{
setcolor(15);
arc(x,y,t,t+1,150);
t=t+6;
}
t=90;
while(t>-270)
{
setcolor(14);
arc(x,y,t-6,t-4,142);
delay(1000);
setcolor(0);
arc(x,y,t-6,t-4,142);
t=t-6;
}
}

Output:
Run the program and see the simulation of clock.

ByProf.AshokN.Kamthane

Copyright2012DorlingKindersleyIndiaPvt.Ltd.

ProgramminginCISBN:9788131760314

4. Write a program to calculate the sum of first and last numbers from 1 to 10. (Example
1+10, 2+9, 3+8 sums should be always 11.)
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,sum;
clrscr();
j=10;
for(i=1;i<=10;i++)
{
if(i<=5)
{
sum=i+j;
printf("%d + %d = %d\n",i,j,sum);
j=j-1;
}
else
break;
}
getch();

Output:
1
2
3
4
5

+
+
+
+
+

10 = 11
9 = 11
8 = 11
7 = 11
6 = 11

5. Write a program to find the total number of votes in favour of persons A and B.
Assume 10 voters will be casting their votes to these persons. Count the number of votes
gained by A and B. User can enter his/her choices by pressing only A or B.
#include<stdio.h>
#include<conio.h>
#include<ctype.h>
void main()
{
int ca,cb,i;
char v;
clrscr();
printf("Vote for 'A' or 'B'\n");
ca=0;
cb=0;
for(i=1;i<=10;i++)
{
printf("\nEnter your vote:");
v=getche();
v=toupper(v);
if(v=='A')
ca++;

ByProf.AshokN.Kamthane

Copyright2012DorlingKindersleyIndiaPvt.Ltd.

ProgramminginCISBN:9788131760314
if(v=='B')
cb++;

}
printf("\nTotal votes for 'A' = %d",ca);
printf("\nTotal votes for 'B' = %d",cb);
getch();

Output:
Vote for 'A' or 'B'
Enter
Enter
Enter
Enter
Enter
Enter
Enter
Enter
Enter
Enter
Total
Total

your vote:A
your vote:A
your vote:B
your vote:B
your vote:B
your vote:B
your vote:A
your vote:B
your vote:B
your vote:A
votes for 'A' = 4
votes for 'B' = 6

6. Write a program to pass the resolution in a meeting comprising of five members. If


three or more votes are obtained the resolution is passed otherwise rejected.
#include<stdio.h>
#include<conio.h>
void main()
{
char v;
int m,y,n;
clrscr();
printf("Vote to pass resolution (Y / N):\n");
y=n=0;
for(m=1;m<=5;m++)
{
printf("\nEnter vote: ");
v=getche();
if(v=='Y')
y++;
else
n++;
}
if(y>n)
printf("\n\nResolution is passed.");
else
printf("\n\nResolution is rejected.");
getch();
}

ByProf.AshokN.Kamthane

Copyright2012DorlingKindersleyIndiaPvt.Ltd.

ProgramminginCISBN:9788131760314

Output:
Vote to pass resolution (Y/N):
Enter
Enter
Enter
Enter
Enter

vote:
vote:
vote:
vote:
vote:

Y
N
N
Y
Y

Resolution is passed.

7. Assume that there are 99 voters voting to a person for selecting chairmans
candidature. If he secures more than 2/3 votes he should be declared as chairman
otherwise his candidature will be rejected.
#include<stdio.h>
#include<conio.h>
void main()
{
char v;
int ch,p;
clrscr();
ch=0;
printf("Vote for person for chairman (Y/N):");
for(p=1;p<=6;p++)
{
printf("\nEnter vote: ");
v=getche();
if(v=='Y')
ch++;
}
if(ch>=(2*p/3))
printf("\nPerson is selected as Chairman.");
else
printf("\nPerson's candidature is rejected.");
getch();
}

Output:
Vote for person for chairman (Y/N):
Enter vote: Y
Enter vote: Y
Enter vote: N
Enter vote: Y
Enter vote: Y
Enter vote: N
Person is selected as Chairman.

ByProf.AshokN.Kamthane

Copyright2012DorlingKindersleyIndiaPvt.Ltd.

ProgramminginCISBN:9788131760314

8. Write a program to display the numbers of a series 1, 3, 9, 27, 81. . . . n by using the
for loop.
#include<stdio.h>
#include<conio.h>
void main()
{
int n,num,i;
clrscr();
printf("How many number in series: ");
scanf("%d",&n);
printf("\nRequired series: ");
num=1;
for(i=0;i<n;i++)
{
printf("%d ",num);
num=num*3;
}
getch();
}

Output:
How many number in series: 5
Required series: 1 3 9 27 81

9. Write a program to check that entered input data for the following. Whenever input is
non-zero or positive display numbers from 1 to that number, otherwise display message
`negative or zero. The program is to be performed for 10 numbers.
#include<stdio.h>
#include<conio.h>
void main()
{
int num,i,j;
clrscr();
i=1;
while(i<=5)
{
printf("\nEnter a number: ");
scanf("%d",&num);
if(num>0)
{
j=1;
while(j<=num)
{
printf("%d ",j);
j++;
}

ByProf.AshokN.Kamthane

Copyright2012DorlingKindersleyIndiaPvt.Ltd.

ProgramminginCISBN:9788131760314
}
else if(num<0)
printf("Number is negative.");
else
printf("Number is zero.");
i++;
printf("\n") ;

}
getch();

Output:
Enter a number: 3
1 2 3
Enter a number: 4
1 2 3 4
Enter a number: -1
Number is negative.
Enter a number: 0
Number is zero.
Enter a number: -123
Number is negative.

10. Write a program to check entered data types for 10 times. If a character is entered
print `Character is entered otherwise `Numeric is entered for numerical values.
#include<stdio.h>
#include<conio.h>
void main()
{
char ch;
int i;
clrscr();
i=1;
while(i<=10)
{
printf("Enter data: ");
ch=getche();
if(ch>=48 && ch<=57)
printf("\nNumeric is entered.");
else
printf("\nCharacter is entered.");
i++;
printf("\n");
}
getch();
}

ByProf.AshokN.Kamthane

Copyright2012DorlingKindersleyIndiaPvt.Ltd.

ProgramminginCISBN:9788131760314

Output:
Enter data: d
Character is entered.
Enter data: 7
Numeric is entered.
Enter data: 1
Numeric is entered.
Enter data: 2
Numeric is entered.
Enter data: j
Character is entered.
Enter data: m
Character is entered.
Enter data: R
Character is entered.
Enter data: B
Character is entered.
Enter data: 0
Numeric is entered.
Enter data: 1
Numeric is entered.

11. Write a program to find the sum of the first hundred natural numbers. (1+2+3+ . . .
+100).
#include<stdio.h>
#include<conio.h>
void main()
{
int n,sum;
clrscr();
n=1;
sum=0;
while(n<=100)
{
sum=sum+n;
n++;
}
printf("Sum of first hundred number (1 to 100): %d",sum);
getch();
}

ByProf.AshokN.Kamthane

Copyright2012DorlingKindersleyIndiaPvt.Ltd.

ProgramminginCISBN:9788131760314

Output:
Sum of first hundred number (1 to 100): 5050

12. Write a program to display numbers 11, 22, 33. . . , 99 using ASCII values from 48 to
57 in loops.
#include<stdio.h>
#include<conio.h>
void main()
{
int i;
clrscr();
for(i=49;i<=57;i++)
{
printf("%c%c ",i,i);
}
getch();
}

Output:
11 22 33 44 55 66 77 88 99

13. Create an infinite for loop. Check each value of the for loop. If the value is odd,
display it otherwise continue with iterations. Print even numbers from 1 to 100. Use
break statement to terminate the program.
#include<stdio.h>
#include<conio.h>
void main()
{
int n;
clrscr();
n=1;
printf("Even numbers from (1 to 100)= ");
for(;;)
{
if(n==100)
break;
if(n%2==0)
printf("%d ",n);
n++;
}
getch();
}

Output:
Even numbers from (1 to 100)= 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

ByProf.AshokN.Kamthane

Copyright2012DorlingKindersleyIndiaPvt.Ltd.

ProgramminginCISBN:9788131760314

14. Write a program to show the display as a rectangle of characters as shown below.
Z
YZY
XYZYX
WXYZYXW
XYZYX
YZY
Z
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,k,x,y;
clrscr();
x=6;
y=2;
for(i=90;i>=87;i--)
{
gotoxy(x,y);
for(j=i;j<=90;j++)
{
printf("%c",j);
}
if(i<90)
{
for(j=89;j>=i;j--)
{
printf("%c",j);
}
}
printf("\n");
x--;
y++;

}
x+=2;
for(i=88;i<=90;i++)
{
gotoxy(x,y);
for(j=i;j<=90;j++)
{
printf("%c",j);
}
if(i!=90)
{
for(j=89;j>=i;j--)
{
printf("%c",j);
}
}
printf("\n");

ByProf.AshokN.Kamthane

Copyright2012DorlingKindersleyIndiaPvt.Ltd.

ProgramminginCISBN:9788131760314
x++;
y++;

}
getch();

Output:
Z
YZY
XYZYX
WXYZYXW
XYZYX
YZY
Z

15. Write a program to read 10 numbers through the keyboard and count number of
positive, negative and zero numbers.
#include<stdio.h>
#include<conio.h>
void main()
{
int num,i;
clrscr();
for(i=1;i<=10;i++)
{
printf("\nEnter number:
scanf("%d",&num);
if(num==0)
printf("Number is
else if(num>0)
printf("Number is
else
printf("Number is
}
getch();
}

");
zero.");
positive.");
negative.");

Output:
Enter number: 3
Number is positive.
Enter number: 6
Number is positive.
Enter number: -12
Number is negative.
Enter number: 0
Number is zero.
Enter number: -345

ByProf.AshokN.Kamthane

Copyright2012DorlingKindersleyIndiaPvt.Ltd.

ProgramminginCISBN:9788131760314
Number is negative.
Enter number: 1234
Number is positive.
Enter number: 00
Number is zero.
Enter number: -999
Number is negative.
Enter number: 23
Number is positive.
Enter number: 0
Number is zero.

16. Write a nested for loop that prints a 5 X 10 pattern of 0s.


#include<stdio.h>
#include<conio.h>
void main()
{
int i,j;
clrscr();
for(i=1;i<=5;i++)
{
for(j=1;j<=10;j++)
{
printf(" 0");
}
printf("\n");
}
getch();
}

Output:
0
0
0
0
0

0
0
0
0
0

0
0
0
0
0

0
0
0
0
0

0
0
0
0
0

0
0
0
0
0

0
0
0
0
0

0
0
0
0
0

0
0
0
0
0

0
0
0
0
0

17. Is it possible to create a loop using the goto statement? If yes write the code for it.
#include<stdio.h>
#include<conio.h>
void main()
{
int num;
clrscr();
num=1;
label: printf("%d ",num);
num++;

ByProf.AshokN.Kamthane

Copyright2012DorlingKindersleyIndiaPvt.Ltd.

ProgramminginCISBN:9788131760314

if(num<=10)
goto label;
getch();

Output:
1 2 3 4 5 6 7 8 9 10

18. Write a program to find the triangular number of a given integer. Fox example
triangular of 5 is (1+2+3+4+5) 15. Use do-while loop.
#include<stdio.h>
#include<conio.h>
void main()
{
int n,num,sum;
clrscr();
printf("Enter a number: ");
scanf("%d",&num);
sum=0;
n=1;
do
{
sum=sum+n;
n=n+1;
}
while(n<=num);
printf("Triangular number of %d is %d.",num,sum);
getch();
}

Output:
Enter a number: 6
Triangular number of 6 is 21.

19. Write a program to display all ASCII numbers and their equivalent characters
numbers and symbols. Use do-while loop. User should prompt every time to press Y
or N. If user presses Y display next alphabet otherwise terminate the program.
#include<stdio.h>
#include<conio.h>
void main()
{
char c;
int i;
clrscr();
printf("ASCII\tCHARACTERS\n");
i=1;
do
{

ByProf.AshokN.Kamthane

Copyright2012DorlingKindersleyIndiaPvt.Ltd.

ProgramminginCISBN:9788131760314
printf("%d\t%c\t",i,i);
i++;
printf("Press 'Y' to contunue or 'N' to terminate:");
c=getche();
printf("\n");
if(c=='N')
exit();

}
while(c=='Y');
getch();

Output:
ASCII
1
2
3
4
5
6

CHARACTERS

Press

Press

Press

Press

Press

Press

'Y'
'Y'
'Y'
'Y'
'Y'
'Y'

to
to
to
to
to
to

contunue
contunue
contunue
contunue
contunue
contunue

or
or
or
or
or
or

'N'
'N'
'N'
'N'
'N'
'N'

to
to
to
to
to
to

terminate:Y
terminate:Y
terminate:Y
terminate:Y
terminate:Y
terminate:N

20. Accept any five two numbers. If the first number is smaller than the second then
display sum of their squares, otherwise sum of cubes.
#include<stdio.h>
#include<conio.h>
void main()
{
int i,a,b,sum;
clrscr();
for(i=1;i<=5;i++)
{
printf("\nEnter two numbers: ");
scanf("%d %d",&a,&b);
if(a<b)
{
sum=a*a+b*b;
printf("%d < %d so %d.",a,b,sum);
}
else
{
sum=a*a*a+b*b*b;
printf("%d > %d so %d.",a,b,sum);
}
}
getch();
}

Output:
Enter two numbers: 3 4
3 < 4 so 25.
Enter two numbers: 5 7

ByProf.AshokN.Kamthane

Copyright2012DorlingKindersleyIndiaPvt.Ltd.

ProgramminginCISBN:9788131760314
5 < 7
Enter
8 > 6
Enter
7 > 2
Enter
6 > 3

so 74.
two numbers: 8 6
so 728.
two numbers: 7 2
so 351.
two numbers: 6 3
so 243.

21. Evaluate the following series. Use do-while loop.


(a) 1+3+5+7. . . . n
(b) 1+4+25+36 . . . n
(c) x+x 2 /2!+x 3 /3!+ . . n
(d) 1+x+x 2 +x 3 +. . . . x n
(a) 1+3+5+7. . . . n

#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int x,y,n,sum;
clrscr();
printf("Enter length of series: ");
scanf("%d",&n);
sum=0;
x=1;
y=1;
printf("Required evaluation: \n");
do
{
printf("%d ",y);
sum=sum+y;
y+=2;
if(x==n)
break;
printf("+ ");

x++;
}
while(x<=n);
printf("\nSum= %d",sum);
getch();

Output:
Enter length of series: 4
Required evaluation:
1 + 3 + 5 + 7
Sum= 16
(b) 1+4+25+36 . . . n

#include<stdio.h>

ByProf.AshokN.Kamthane

Copyright2012DorlingKindersleyIndiaPvt.Ltd.

ProgramminginCISBN:9788131760314
#include<conio.h>
#include<math.h>
void main()
{
int x,n,sum;
clrscr();
printf("Enter length of series: ");
scanf("%d",&n);
sum=0;
x=1;
printf("Required Evaluation: \n");
do
{
printf("%d ",x*x);
sum=sum+x*x;
if(x==n)
break;
printf("+ ");
x++;
}
while(x<=n);
printf("\nSum= %d",sum);
getch();
}

Output:
Enter length of series: 4
Required Evaluation:
1 + 4 + 9 + 16
Sum= 30
(c) x+x 2 /2!+x 3 /3!+ . . n
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
float x,n,num,sum,fact,f;
clrscr();
printf("Enter length of series: ");
scanf("%f",&num);
printf("Enter value of x: ");
scanf("%f",&x);
sum=0;
n=1;
printf("\nRequired evaluation: ");
do
{
fact=1;
f=1;
while(f<=n)
{

ByProf.AshokN.Kamthane

Copyright2012DorlingKindersleyIndiaPvt.Ltd.

ProgramminginCISBN:9788131760314

fact=fact * f;
f++;

sum=sum+pow(x,n)/fact;
n++;

}
while(n<=num);
printf("%.2f",sum);
getch();

Output:
Enter length of series: 4
Enter value of x: 2
Required evaluation: 6.00
(d) 1+x+x 2 +x 3 +. . . . x n
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int x,n,i,sum;
clrscr();
printf("Enter value of x and n: ");
scanf("%d %d",&x,&n);
sum=0;
i=0;
do
{
sum=sum+pow(x,i);
i++;
}
while(i<=n);
printf("Required evaluation= %d",sum);
getch();
}

Output:
Enter value of x and n: 2 4
Required evaluation= 31

22. Write a program to display the following, using do-while loop.


(a) a+1+b+2+c+3. . . .n, where n is an integer.
(b) z+y+x. . .+a.
(c) za+yb+xc. . .+az.
ByProf.AshokN.Kamthane

Copyright2012DorlingKindersleyIndiaPvt.Ltd.

ProgramminginCISBN:9788131760314

(a) a+1+b+2+c+3. . . .n, where n is an integer.

#include<stdio.h>
#include<conio.h>
void main()
{
int n,i;
char c;
clrscr();
printf("Enter value of n: ");
scanf("%d",&n);
i=1;
c='a';
do
{
printf("%c ",c);
printf("+ ");
printf("%d ",i);
if(i==n)
break;
printf("+ ");
i++;
c++;
}
while(i<=n);
getch();
}

Output:
Enter value of n: 5
a + 1 + b + 2 + c + 3 + d + 4 + e + 5
(b) z+y+x. . .+a.

#include<stdio.h>
#include<conio.h>
void main()
{
char c;
clrscr();
c='z';
do
{
printf("%c ",c);
if(c=='a')
break;
printf("+ ");
c--;
}
while(c>='a');
getch();
}

ByProf.AshokN.Kamthane

Copyright2012DorlingKindersleyIndiaPvt.Ltd.

ProgramminginCISBN:9788131760314

Output:
z + y + x + w + v + u + t + s + r + q + p + o + n + m + l + k + j + i +
h + g + f + e + d + c + b + a
(c) za+yb+xc. . .+az.
#include<stdio.h>
#include<conio.h>
void main()
{
char c,d;
clrscr();
c='z';
d='a';
do
{
printf("%c%c",c,d);
if(c=='a')
break;
printf("+ ");
c--;
d++;
}
while(c>='a');
getch();
}

Output:
za+ yb+ xc+ wd+ ve+ uf+ tg+ sh+ ri+ qj+ pk+ ol+ nm+ mn+ lo+ kp+ jq+ ir+
hs+ gt+ fu+ ev+ dw+ cx+ by+ az

23. Enter the 10 numbers through the keyboard and sort them in ascending and
descending order, using do-while loop.
#include<stdio.h>
void main()
{
int n,arr[10],i=0,j,t;
printf("Enter ten no.s\n");
do
{
scanf("%d",&arr[i]);
i++;
}while(i<10);
i=0;
do
{
j=i;
do
{
ByProf.AshokN.Kamthane

Copyright2012DorlingKindersleyIndiaPvt.Ltd.

ProgramminginCISBN:9788131760314
if(arr[i]>arr[j])
{
t=arr[i];
arr[i]=arr[j];
arr[j]=t;
}
j++;
}while(j<10);
i++;
}while(i<10);
printf("The elements in ascending order are \n");
i=0;
do
{
printf("%d\n",arr[i]);
i++;
}while(i<10);
printf("The elements in descending order are \n");
i=9;
do
{
printf("%d\n",arr[i]);
i--;
}while(i>=0);
}

Output:
Enter ten nos.
1 2 3 4 5 10 9 8 7 6
The elements in ascending order are
1
2
3
4
5
6
7
8
9
10
The elements in descending order are
10
9
8
7
6
5
4
3

ByProf.AshokN.Kamthane

Copyright2012DorlingKindersleyIndiaPvt.Ltd.

ProgramminginCISBN:9788131760314
2
1

24. Enter text through the keyboard and display it in the reverse order. Use do-while
loop.
#include<process.h>
void main()
{
char a[10]={"Priya"};
int i=0;
clrscr();
while(a[i++]!='\0');
i--;
while(i>-1)
{
printf("%c",a[i]);
i--;
}
getche();
}

Output:
ayirp

25. Print multiplication of digits of any number. For example number 235, multiplication
to be 5*3*2 = 30. Use do-while loop.
#include<stdio.h>
#include<conio.h>
void main()
{
int num,d,mul;
clrscr();
printf("Enter a number(above two digit): ");
scanf("%d",&num);
printf("\nMultiplication of digit= ");
mul=1;
do
{
d=num%10;
mul=mul*d;
num=num/10;
printf("%d ",d);

if(num==0)
break;
else
printf("* ");

ByProf.AshokN.Kamthane

Copyright2012DorlingKindersleyIndiaPvt.Ltd.

ProgramminginCISBN:9788131760314

while(num>0);
printf("= %d",mul);
getch();

Output:
Enter a number(above two digit): 345
Multiplication of digit= 5 * 4 * 3 = 60

26. Print square roots of each digit of any number. Consider each digit as perfect square.
For example, for 494 the square roots to be printed should be 2 3 2.
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int n,d,i,div,sum;
clrscr();
printf("Enter number of digits: ");
scanf("%d",&d);
printf("Enter the number: ");
scanf("%d",&n);
div=pow(10,d-1);
sum=0;
while(div>0)
{
i=n/div;
sum=sum+sqrt(i)*div;
n=n%div;
div=div/10;
}
printf("Square Root: %d",sum);
getch();
}

Output:
Enter number of digits: 3
Enter the number: 499
Square Root: 233

27. Write a program to read a positive integer number n and generate the numbers in the
following way. If entered number is 3 the output will be as follows.
(a) 9 4 1 0 1 4 9
(b) 9 4 1 0 1 2 3
(a) 9 4 1 0 1 4 9

#include<stdio.h>
#include<conio.h>

ByProf.AshokN.Kamthane

Copyright2012DorlingKindersleyIndiaPvt.Ltd.

ProgramminginCISBN:9788131760314
#include<math.h>
void main()
{
int n,i,d;
clrscr();
printf("Enter a number n: ");
scanf("%d",&n);
for(i=-n;i<=n;i++)
{
d=pow(i,2);
printf("%d ",d);
}
}

getch();

Output:
Enter a number n: 5
25 16 9 4 1 0 1 4 9 16 25
(b) 9 4 1 0 1 2 3

#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int n,i,d;
clrscr();
printf("Enter a number n: ");
scanf("%d",&n);
for(i=-n;i<=n;i++)
{
if(i<0)
{
d=pow(i,2);
printf("%d ",d);
}
else
printf("%d ",i);
}
}

getch();

Output:
Enter a number n: 5
25 16 9 4 1 0 1 2 3 4 5

ByProf.AshokN.Kamthane

Copyright2012DorlingKindersleyIndiaPvt.Ltd.

ProgramminginCISBN:9788131760314

28. Write a program to enter two integer values through the keyboard. Using while loop,
perform the product of two integers. In case product is zero (0), loop should be
terminated otherwise loop will continue.
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int num1,num2;
clrscr();
while(1)
{
printf("\nEnter two numbers: ");
scanf("%d %d",&num1,&num2);
printf("Product= %d",num1*num2);
if(num1*num2 == 0)
break;
}
getch();
}

Output:
Enter two numbers: 3 2
Product= 6
Enter two numbers: 4 0
Product= 0

29. Write a program to enter a single character either in lower or uppercase. Display its
corresponding ASCII equivalent number. Use the while loop for checking ASCII
equivalent numbers for different characters. When capital E is pressed, the program
should be terminated.
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
char c;
clrscr();
while(1)
{
printf("Enter a character: ");
c=getche();
if(c=='E')
break;
printf("\nASCII code= %d\n",c);
}
getch();
}

Output:
ByProf.AshokN.Kamthane

Copyright2012DorlingKindersleyIndiaPvt.Ltd.

ProgramminginCISBN:9788131760314
Enter a character: r
ASCII code= 114
Enter a character: h
ASCII code= 104
Enter a character: A
ASCII code= 65
Enter a character: 1
ASCII code= 49
Enter a character: 9
ASCII code= 57

30. Write a program to read a positive integer number n and generate the numbers in the
following way. If entered number is 4 the output will be as follows. OUTPUT: 4! 3! 2! 1!
0 1! 2! 3! 4! 5!.
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int n,i;
clrscr();
printf("Enter the number: ");
scanf("%d",&n);
for(i=-n;i<=n+1;i++)
{
if(i!=0)
{
printf("%d",abs(i));
printf("! ");
}
else
printf("%d ",i);
}
getch();
}

Output:
Enter the number: 5
5! 4! 3! 2! 1! 0 1! 2! 3! 4! 5! 6!

31. Write a program to read a positive integer number n and generate the numbers in the
different ways as given below. If the entered number is 4 the output will be as follows.
(a) 2 4 6 8 10 . . . n (provided n is even).
(b) 1 3 5 7 9. . . . n (provided n is odd).
#include<stdio.h>
#include<conio.h>

ByProf.AshokN.Kamthane

Copyright2012DorlingKindersleyIndiaPvt.Ltd.

ProgramminginCISBN:9788131760314
#include<math.h>
void main()
{
int n,i,even,odd;
clrscr();
printf("Enter the number: ");
scanf("%d",&n);
printf("\n a) ");
even=2;
for(i=1;i<=n;i++)
{
printf("%d ",even);
even+=2;
}
odd=1;
printf("\n b) ");
for(i=1;i<=n;i++)
{
printf("%d ",odd);
odd+=2;
}
getch();
}

Output:
Enter the number: 6
a) 2 4 6 8 10 12
b) 1 3 5 7 9 11

32. Write a program to read a positive integer number n and perform the squares of
individual digits.
For example n=205 then the output will be 25 0 4.
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int n,d;
clrscr();
printf("Enter the number: ");
scanf("%d",&n);
printf("Square of digits: ");
while(n>0)
{
d=n%10;
d=pow(d,2);
printf("%d ",d);
n=n/10;
}
getch();
}

ByProf.AshokN.Kamthane

Copyright2012DorlingKindersleyIndiaPvt.Ltd.

ProgramminginCISBN:9788131760314

Output:
Enter the number: 234
Square of digits: 16 9 4

33. What would be the output of the given below program?


#include<conio.h>
#include<stdio.h>
void main()
{
while(0)
{
printf("\n Hello");
}
}

Output:
Unreachable code due to while(0)

ByProf.AshokN.Kamthane

Copyright2012DorlingKindersleyIndiaPvt.Ltd.

ProgramminginCISBN:9788131760314

Chapter 7: Data Structure: Array


1. Write a program to read 10 integers in an array. Find the largest and smallest number.
#include<conio.h>
#include<stdio.h>
void main()
{
int max,min,i,a[10];
clrscr();
printf("Enter the elements of the array \n");
for(i=0;i<10;i++)
{
printf("Enter number #%d : ",i+1);
scanf("%d",&a[i]);
}
max=min=a[0];
for(i=1;i<10;i++)
{
if(max<a[i])
max=a[i];
else if(min>a[i])
min=a[i];
}
printf("\nThe smallest and largest numbers are %d and
%drespectively.",min,max);
getch();
}

Output:
Enter
Enter
Enter
Enter
Enter
Enter
Enter
Enter
Enter
Enter
Enter

the elements of the array


number #1 : 23
number #2 : 55
number #3 : 3
number #4 : 456
number #5 : 7
number #6 : 8989
number #7 : 4
number #8 : 356
number #9 : 6
number #10 : 4

The smallest and largest numbers are 3 and 8989 respectively.

ByProf.AshokN.Kamthane

Copyright2012DorlingKindersleyIndiaPvt.Ltd.

ProgramminginCISBN:9788131760314

2. Write a program to read a number containing five digits. Perform square of each digit.
For example number is 45252. Output should be square of each digit, i.e. 4 25 4 25.
#include<conio.h>
#include<stdio.h>
#include<math.h>
void main()
{
int a[5],i;
clrscr();
printf("Enter the 5 digit number :");
for(i=0;i<5;i++)
scanf("%1d",&a[i]);
printf("\nThe output is :\n");
for(i=4;i>-1;i--)
printf("%d ",(int)pow(a[i],2));
getch();
}

Output:
Enter the 5 digit number: 43234
The output is:
16 9 4 9 16

3. Write a program to read a number of any lengths. Perform the addition and subtraction
on the largest and smallest digits of it.
#include<conio.h>
#include<stdio.h>
#include<alloc.h>
void main()
{
int *a,i,min,max,n;
clrscr();
printf("Enter the length of number (i.e number of digits in a number)
:");
scanf("%d",&n);
a=(int *)malloc(n*sizeof(int));
printf("Enter the %d digit number :",n);
for(i=0;i<n;i++)
scanf("%1d",&a[i]);
min=max=a[0];
for(i=0;i<n;i++)
{
if(max<a[i])max=a[i];
else if(min>a[i])min=a[i];
}
printf("\nThe minimum and the maximun numbers are %d and
%d.\n",min,max);

ByProf.AshokN.Kamthane

Copyright2012DorlingKindersleyIndiaPvt.Ltd.

ProgramminginCISBN:9788131760314
printf("\nTheir addition is %d",min+max);
printf("\nTheir substraction is %d",max-min);
getch();
}

Output:
Enter the length of number (i.e number of digits in a number) :7
Enter the 7 digit number: 8675339
The minimum and the maximun numbers are 3 and 9.
Their addition is 12
Their substraction is 6

4. Write a program to read three digits positive integer number n and generate possible
permutation of numbers using their digits. For example n=123 then the permutations are
123, 132, 213, 231,312,321.
#include<conio.h>
#include<stdio.h>
void main()
{
int a[3],i,j,k;
clrscr();
printf("Enter any three digit positive number. :");
for(i=0;i<3;i++)
scanf("%1d",&a[i]);
printf("The possible permutations of entered numbers are :\n");
for(i=0;i<3;i++)
for(j=0;j<3;j++)
for(k=0;k<3;k++)
if(!(i==j ||j==k ||k==i))
printf("%d%d%d\n",a[i],a[j],a[k]);
getch();
}

Output:
Enter any three digit positive number: 674
The possible permutations of entered numbers are:
674
647
764
746
467
476

5. Write a program to read the text. Find out number of lines in it.
#include<conio.h>
#include<stdio.h>
void main()
{

ByProf.AshokN.Kamthane

Copyright2012DorlingKindersleyIndiaPvt.Ltd.

ProgramminginCISBN:9788131760314
FILE *fp;
char ch;
int count=0;
fp=fopen("temp.txt","w");
clrscr();
printf("Enter # to denote the end of text.");
printf("\nEnter the text :\n");
while(1)
{
ch=getchar();
if(ch=='\n')
count++;
else if(ch=='#') break;
else
fputc(ch,fp);
}
printf("\n\nThe number of lines are : %d",count+1);
getch();
}

Output:
Enter # to denote the end of text.
Enter the text :
Hi
This is college of engineering Nanded
Welcomes you all
Thank you#
The number of lines are : 4

6. Write a program to read any three characters. Find out all the possible combinations.
#include<conio.h>
#include<stdio.h>
void main()
{
int i,j,k;
char a[3];
clrscr();
printf("Enter any three character.\n");
for(i=0;i<3;i++)
{
printf("Enter character #%d :",i+1);
scanf(" %c",&a[i]);
}
printf("The possible permutations of entered numbers are :\n");
for(i=0;i<3;i++)
for(j=0;j<3;j++)
for(k=0;k<3;k++)
if(!(i==j ||j==k ||k==i))
printf("%c%c%c\n",a[i],a[j],a[k]);
getch();
}

Output:
ByProf.AshokN.Kamthane

Copyright2012DorlingKindersleyIndiaPvt.Ltd.

ProgramminginCISBN:9788131760314
Enter any three character.
Enter character #1: f
Enter character #2: g
Enter character #3: s
The possible permutations of entered numbers are:
fgs
fsg
gfs
gsf
sfg
sgf

7. Write a program to enter string in lower and uppercase. Convert lower to uppercase
and vice versa and display the string.
#include<conio.h>
#include<ctype.h>
#include<stdio.h>
void main()
{
char *a,*b;
int i=0;
clrscr();
printf("Enter the two strings :");
scanf("%s%s",a,b);
while(a[i]!='\0')
{
if(islower(a[i]))a[i]=toupper(a[i]);
else a[i]=tolower(a[i]);
i++;
}
i=0;
while(b[i]!='\0')
{
if(islower(b[i]))b[i]=toupper(b[i]);
else b[i]=tolower(b[i]);
i++;
}
printf("\nThe converted strings are %s and %s",a,b);
getch();
}

Output:
Enter the two strings: HARSHAD shrikant
The converted strings are harshad and SHRIKANT

8. Read the marks of five subjects obtained by five students in an examination. Display
the top two student codes and their marks.
ByProf.AshokN.Kamthane

Copyright2012DorlingKindersleyIndiaPvt.Ltd.

ProgramminginCISBN:9788131760314
#include<conio.h>
#include<stdio.h>
struct student
{
int s,s1,s2,s3,s4;
int code;
int sum;
};
void main()
{
struct student a[5];
int i,max,max2;
int n,f,s;
clrscr();
for(i=0;i<5;i++){
printf("Enter the marks of student #%d\n ",i+1);
printf("Enter student code :");
scanf("%d",&a[i].code);
printf("subject #1 :");
scanf("%d",&a[i].s);
printf("subject #2 :");
scanf("%d",&a[i].s1);
printf("subject #3 :");
scanf("%d",&a[i].s2);
printf("subject #4 :");
scanf("%d",&a[i].s3);
printf("subject #5 :");
scanf("%d",&a[i].s4);
a[i].sum =a[i].s+a[i].s1+a[i].s2+a[i].s3+a[i].s4;
}
max=a[0].sum;
f=0;
for(i=1;i<5;i++)
{
if(max<a[i].sum){
max=a[i].sum;
f=i;
}
}
max2=0;
for(i=0;i<5;i++)
{
if(max2<a[i].sum&&f!=i){
max2=a[i].sum;
s=i;
}
}
printf("The details of top 2 students are :\n");
printf("student code s1
s2
s3
s4
s5
sum\n");
printf("%11d%5d%5d%5d%5d%5d%6d",a[f].code,a[f].s,a[f].s1,a[f].s2,a[f].s3,a[f].s4
,a[f].sum);

ByProf.AshokN.Kamthane

Copyright2012DorlingKindersleyIndiaPvt.Ltd.

ProgramminginCISBN:9788131760314
printf("\n%11d%5d%5d%5d%5d%5d%6d",a[s].code,a[s].s,a[s].s1,a[s].s2,a[s].s3,a[s].s4
,a[s].sum);
getch();
}

Output:
Enter the marks of student #1
Enter student code :678
subject #1 :78
subject #2 :89
subject #3 :78
subject #4 :67
subject #5 :8
Enter the marks of student #2
Enter student code :756
subject #1 :88
subject #2 :67
subject #3 :87
subject #4 :66
subject #5 :65
Enter the marks of student #3
Enter student code :098
subject #1 :67
subject #2 :98
subject #3 :87
subject #4 :76
subject #5 :45
Enter the marks of student #4
Enter student code :834
subject #1 :76
subject #2 :87
subject #3 :65
subject #4 :55
subject #5 :34
Enter the marks of student #5
Enter student code :124
subject #1 :75
subject #2 :43
subject #3 :56
subject #4 :7
subject #5 :67
The details of top 2 students are
student code s1
s2
s3
s4
756
88
67
87
66
98
67
98
87
76

:
s5
65
45

sum
373
373

9. Write a program to enter five numbers using array and rearrange the array in the
reverse order. For Example numbers entered are 5 8 3 2 4 and after arranging array
elements must be 4 2 3 8 5.
ByProf.AshokN.Kamthane

Copyright2012DorlingKindersleyIndiaPvt.Ltd.

ProgramminginCISBN:9788131760314

#include<conio.h>
#include<stdio.h>
#include<alloc.h>
void main()
{
int *a,n,i,*r;
clrscr();
printf("Enter the number of elements of the array :");
scanf("%d",&n);
a=(int*)malloc(sizeof(int)*n);
r=(int*)malloc(sizeof(int)*n);
printf("Enter the elements of the array.\n");
for(i=0;i<n;i++)
{
printf("Enter element #%d: ",i+1);
scanf("%d",a+i);
r[i]=a[i];
}
printf("\n\nThe elements of the array are {");
for(i=0;i<n;i++) printf("%d,",a[i]);
printf("\b}\n\nAfter reversing the elements of the array are \n{");
for(i=0;i<n;i++)
a[n-i-1]=r[i];
for(i=0;i<n;i++) printf("%d,",a[i]);
printf("\b}");
getch();
}

Output:
Enter
Enter
Enter
Enter
Enter
Enter
Enter

the number of elements of the array: 5


the elements of the array.
element #1: 23
element #2: 54
element #3: 3
element #4: 4
element #5: 33

The elements of the array are {23,54,3,4,33}


After reversing the elements of the array are
{33,4,3,54,23}

10. Accept a text up to 50 words and perform the following actions.


(a) Count total vowels, consonants, spaces, sentences and words with spaces.
(b) Program should erase more than one space between two successive words.
[Hint: total number of sentences can be computed based on total number of full stops.]
#include<ctype.h>
#include<conio.h>
#include<stdio.h>

ByProf.AshokN.Kamthane

Copyright2012DorlingKindersleyIndiaPvt.Ltd.

ProgramminginCISBN:9788131760314
void main()
{
int wspace=0,vo=0,con=0,word=0,sen=0,i=0,flag=0;
char ch;
char a[100];//for 100 characters
clrscr();
printf("Enter # to denote the end of string.\n");
printf("Enter the string upto 50 words :\n");
while(1)
{
ch=getchar();
if(ch=='#')break;
else if(isalpha(ch))
{
if(ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u')vo++;
else con++;
a[i++]=ch;
flag=0;
}
else if(isspace(ch)){
if(flag==1)
{
wspace++;
a[i-1]=ch;
continue;
}
else
{
wspace++;
a[i++]=ch;
flag=1;
word++;
}
}
else if(ch=='.'){
sen++;
a[i++]=ch;
flag=0;
}
else {
a[i++]=ch;
flag=0;
}
}
a[i]='\0';
printf("\n\nThe meta data information is :\n");
printf("Type\t\tcount\n");
printf("Vowels\t\t%d",vo);
printf("\nconsonents\t%d",con);
printf("\nwords\t\t%d",word+1);
printf("\nSentences\t%d",sen);
printf("\nwhite spaces\t%d",wspace);
printf("\n\n\nThe formatted text is \n\n\"");
printf("%s \"\n",a);

ByProf.AshokN.Kamthane

Copyright2012DorlingKindersleyIndiaPvt.Ltd.

ProgramminginCISBN:9788131760314
getch();
}

Output:
Enter # to denote the end of string.
Enter the string upto 50 words:
Hi
this is
from SGGS,
Nanded.
Welcomes you.
Keep enjoying. bye.#
The meta data information is:
Type
count
Vowels
17
consonents
31
words
11
Sentences
4
white spaces
22
The formatted text is
"Hi this is from SGGS, Nanded.
Welcomes you.
Keep enjoying. bye. "

11. Evaluate the following series. Calculate every term and store its result in an array.
Using array calculate the final result.
(a) x= 1 1 +2 2 +3 3 +4 4 nh
(b) x=1! + 2! + 3!+ n!
(c) x=1!-2!+3!- . . . . n!
#include<conio.h>
#include<stdio.h>
#include<math.h>
void main()
{
double ans[3]={0};
int i,n,k,j;
clrscr();
printf("Enter the value of n:");
scanf("%d",&n);
for(i=1;i<=n;i++){
ans[0]+=pow(i,i);
k=1;
for(j=i;j>=2;j--){
k=k*j;
}
ans[1]+=k;
ans[2]+=pow(-1,i+1)*k;
}
printf("The values for the series given are :");
for(i=0;i<3;i++){
printf("\n%lf",ans[i]);
}
getch();

ByProf.AshokN.Kamthane

Copyright2012DorlingKindersleyIndiaPvt.Ltd.

ProgramminginCISBN:9788131760314
}

Output:
Enter the value of n: 5
The values for the series given are:
3413.000000
153.000000
101.000000

12. Refer the question number 12 from the book.


#include<conio.h>
#include<stdio.h>
void main()
{
int item[5]={1,2,3,4,5};
float p[5]={850,1150,75,125,50};
int n,ch;
char k;
float cost;
clrscr();
do
{
printf("\nItem code\tPrice\n");
printf("1.10th book set\t%f\n",p[0]);
printf("2.12th book set\t%f\n",p[1]);
printf("NOTEBOOKS\n");
printf("3.100 pages/dozen%f\n",p[2]);
printf("4.200 pages/dozen%f\n",p[3]);
printf("5.Pen set\t%f\n",p[4]);
printf("\nEnter item number you want to
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("\nEnter the number of sets you
scanf("%d",&n);
if(n>10)
{
cost = p[0]*n;
printf("\nTotal cost of %d item set is
discount is %f",n,cost,cost*15/100.0);
}
else
{
cost = p[0]*n;
printf("\nTotal cost of %d item set is
discount is %f",n,cost,cost-cost/10.0);
}

purchase :");

want to purchase :");

%f\nTotal cost after 15%%

%f\nTotal cost after 10%%

ByProf.AshokN.Kamthane

Copyright2012DorlingKindersleyIndiaPvt.Ltd.

ProgramminginCISBN:9788131760314
break;
case 2:
printf("\nEnter the number of sets you want to purchase :");
scanf("%d",&n);
if(n>10)
{
cost = p[1]*n;
printf("\nTotal cost of %d item set is %f\nTotal cost after 15%%
discount is %f",n,cost,cost-cost*15/100.0);
}
else
{
cost = p[1]*n;
printf("\nTotal cost of %d item set is %f\nTotal cost after 10%%
discount is %f",n,cost,cost-cost/10.0);
}
break;
case 3:
printf("\nEnter the number of dozens you want to purchase :");
scanf("%d",&n);
if(n>10)
{
cost = p[2]*n;
printf("\nTotal cost of %d dozens is %f\nTotal cost after 10%%
discount is %f",n,cost,cost-cost*10/100.0);
}
else
{
cost = p[2]*n;
printf("\nTotal cost of %d dozen is %f",n,cost);
}
break;
case 4:
printf("\nEnter the number of dozens you want to purchase :");
scanf("%d",&n);
if(n>10)
{
cost = p[3]*n;
printf("\nTotal cost of %d dozens is %f\nTotal cost after 10%%
discount is %f",n,cost,cost-cost*10/100.0);
}
else
{
cost = p[3]*n;
printf("\nTotal cost of %d dozens is %f",n,(float)cost);
}
break;
case 5:
printf("\nEnter the number of pen sets you want to purchase :");
scanf("%d",&n);
cost = p[4]*n;
printf("\nTotal cost of %d pen set is %f",n,(float)cost);
break;
default :
printf("\nInvalid chioce\n Try again :");

ByProf.AshokN.Kamthane

Copyright2012DorlingKindersleyIndiaPvt.Ltd.

ProgramminginCISBN:9788131760314

}
printf("\nDo you wish to continue(y/n) :");
scanf(" %c",&k);
}while(k=='y'||k=='Y');
getch();

Output:
Item code
Price
1.10th book set 850.000000
2.12th book set 1150.000000
NOTEBOOKS
3.100 pages/dozen75.000000
4.200 pages/dozen125.000000
5.Pen set
50.000000
Enter item number you want to purchase :4
Enter the number of dozens you want to purchase: 23
Total cost of 23 dozens is 2875.000000
Total cost after 10% discount is 2587.500000
Do you wish to continue(y/n): y
Item code
Price
1. 10th book set 850.000000
2. 12th book set 1150.000000
NOTEBOOKS
3. 100 pages/dozen 75.000000
4. 200 pages/dozen 125.000000
5. Pen set
50.000000
Enter item number you want to purchase: 3
Enter the number of dozens you want to purchase: 23
Total cost of 23 dozens is 1725.000000
Total cost after 10% discount is 1552.500000
Do you wish to continue(y/n): n

13. Refer the question number 13 from the book.


#include<conio.h>
#include<stdio.h>
void main()
{
int
item[15],i,n,p[16]={0,3000,4000,4000,8000,1000,2000,1000,1500,1500,3000
,400,740,200,3000,1500};
float price=0,flag=0;
clrscr();
printf("The code \t Part \t price of system :\n\n\n");
printf("1 MOTHER BOARD WITH PROCESSOR P3 600 MHz 3000 \n2 MOTHER BOARD
WITH PROCESSOR P4 1000 MHz 8000 \n3 HD (200 GB) 4000 \n4 HD (400 GB)

ByProf.AshokN.Kamthane

Copyright2012DorlingKindersleyIndiaPvt.Ltd.

ProgramminginCISBN:9788131760314
8000 \n5 RAM (1 GB) 1000 \n6 RAM (2 GB) 2000 \n7 CACHE (256 MB) 1000
\n8 CACHE (1 GB) 1500 \n9 FDD (1.44 MB) 1500 \n10 CD ROM DRIVE 3000
\n11 CABINET 400 \n12 KEYBOARD 750 \n13 MOUSE 200\n14 MULTIMEDIA KIT
3000\n15 MODEM 1500");
printf("\n\nEnter the number of items you want to purchase :");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\nEnter the item #%d code :",i+1);
scanf("%d",&item[i]);
price += p[item[i]];
}
printf("\nThe total price of the items purchased is :%f",price);
if(i>=5){
printf("\n\nDiscount assured :%f",price/10);
flag=1;
}
else
printf("\n\nDiscount assured :%f",0);
printf("\n\nPrice after offering discount is :%f",price flag*price/10);
getch();
}

Output:
The code

Part

price of system:

1 MOTHER BOARD WITH PROCESSOR P3 600 MHz 3000


2 MOTHER BOARD WITH PROCESSOR P4 1000 MHz 8000
3 HD (200 GB) 4000
4 HD (400 GB) 8000
5 RAM (1 GB) 1000
6 RAM (2 GB) 2000
7 CACHE (256 MB) 1000
8 CACHE (1 GB) 1500
9 FDD (1.44 MB) 1500
10 CD ROM DRIVE 3000
11 CABINET 400
12 KEYBOARD 750
13 MOUSE 200
14 MULTIMEDIA KIT 3000
15 MODEM 1500
Enter the number of items you want to purchase: 6
Enter the item #1 code :2
Enter the item #2 code :7
Enter the item #3 code :11
Enter the item #4 code :6
Enter the item #5 code :5

ByProf.AshokN.Kamthane

Copyright2012DorlingKindersleyIndiaPvt.Ltd.

ProgramminginCISBN:9788131760314

Enter the item #6 code :8


The total price of the items purchased is :9900.000000
Discount assured :990.000000
Price after offering discount is :8910.000000

14. Refer the question number 14 from the book.


#include<conio.h>
#include<stdio.h>
void main()
{
int i;
float sa[7],tot=0,emp[7
][4];
clrscr();
printf("\n\nCode\tPost\n");
printf("1 Manager\n2 Executive\n3 Senior Asst.\n4 Junior Asst.\n5
Steno\n6 Clerk\n7 Peon");
printf("\nEnter the salary details for all the above post.\n");
for(i=0;i<7;i++)
{
printf("Enter the details for code#%d as basic salary,DA and other
perks:\n",i+1);
scanf("%f%f%f%f",&emp[i][0],&emp[i][1],&emp[i][2],&emp[i][3]);
sa[i]=emp[i][1]+ emp[i][1]*emp[i][2]/100+emp[i][2]*emp[i][0]/100;
tot+=sa[i];
}
printf("\n\nThe salary of all the employee is given below :");
printf("\ncode\tsalary\n");
for(i=0;i<7;i++)
printf("%d\t %f\n",i+1,sa[i]);
printf("The total salry of the employees are : %f",tot);
getch();
}

Output:
Code
Post
1 Manager
2 Executive
3 Senior Asst.
4 Junior Asst.
5 Steno
6 Clerks
7 Peons
Enter the salary details for all the above post.
Enter the details for code#1 as basic salary,DA and other perks:
1 40000 50 15
Enter the details for code#2 as basic salary,DA and other perks:
2 20000 25 20

ByProf.AshokN.Kamthane

Copyright2012DorlingKindersleyIndiaPvt.Ltd.

ProgramminginCISBN:9788131760314
Enter the details
3 15000 25 20
Enter the details
4 10000 20 15
Enter the details
5 8000 20 10
Enter the details
6 5000 20 10
Enter the details
7 4000 20 10

for code#3 as basic salary,DA and other perks:


for code#4 as basic salary,DA and other perks:
for code#5 as basic salary,DA and other perks:
for code#6 as basic salary,DA and other perks:
for code#7 as basic salary,DA and other perks:

The salary of the entire employee is given below:


Code
salary
1
60000.500000
2
25000.500000
3
18750.750000
4
12000.799805
5
9601.000000
6
6001.200195
7
4801.399902
The total salaries of the employees are: 136156.156250

15. Refer the question number 15 from the book.


#include<conio.h>
#include<stdio.h>
void main()
{
int i,p[5]={5,8,12,20,35},tot=0,q,ch='y';
clrscr();
printf("Code No.\tWattage of the bulb\tPrice/bulb(Rs.)");
printf("\n1 \t\t\t15 W \t\t5\n2 \t\t\t40 W \t\t8\n3 \t\t\t60 W
\t\t12\n4 \t\t\t100 W \t\t20\n5 \t\t\t200 W \t\t35");
while(ch=='Y'||ch=='y')
{
printf("\nEnter the bulb item code and its quantity :");
scanf("%d%d",&i,&q);
fflush(stdout);
tot+=p[i-1]*q;
printf("Would you like to purchase another item(y/n) :");
scanf(" %c",&ch);
}
printf("\nThe total cost of items purchased are :%d",tot);
getch();
}

Output:
Code No.
1
2
3
4
5

Wattage of the bulb


15 W
40 W
60 W
100 W
200 W

Price/bulb (Rs.)
5
8
12
20
35

ByProf.AshokN.Kamthane

Copyright2012DorlingKindersleyIndiaPvt.Ltd.

ProgramminginCISBN:9788131760314
Enter the bulb item code and its quantity: 4 44
Would you like to purchase another item(y/n): y
Enter the bulb item code and its quantity: 3 23
Would you like to purchase another item(y/n): y
Enter the bulb item code and its quantity: 5 45
Would you like to purchase another item(y/n): n
The total cost items purchased are: 2731

16. Write a program to display the different parameters of 10 men in a table. The
different parameters to be entered are height, weight, age and chest in cm. Find the
number of men who satisfy the following condition.
(a) Age should be greater than 18 and less than 25.
(b) Height should be between 5.2 to 5.6 inches.
(c) Weight should be in between 45 to 60 kg.
(d) Chest should be greater than 45 cm.
#include<conio.h>
#include<alloc.h>
#include<stdio.h>
struct men
{
int a,c;
float w,h;
}m[10];
void main()
{
int i,count=0;
clrscr();
m=(struct men*)malloc(10*sizeof(struct men));
printf("\nEnter the age, weight, height(in inches) and chest(in cm)");
for(i=0;i<10;i++)
{
printf("\nMan #%d:",i+1);
scanf("%d%f%f%d",&m[i].a,&m[i].w,&m[i].h,&m[i].c);
if(m[i].a>18&&m[i].a<25&&m[i].w>44&&m[i].w<61&&m[i].h>=5.2&&m[i].h<5.6&
&m[i].c>45)
count++;
}
printf("Condition :\n1. Age should be greater than 18 and less than
25\n2. Height should be in between 5.2 to 5.6inches\n");
printf("3. Weight should be in between 45 to 60 kg\n4. Chest should be
greater than 45cms\n");
printf("\n\nThe number of men satifying the above condition are
%d",count);
getch();
}

ByProf.AshokN.Kamthane

Copyright2012DorlingKindersleyIndiaPvt.Ltd.

ProgramminginCISBN:9788131760314

17. Write a program to display the items that have been exported between 1995 and 2000.
The table should be displayed as follows. Input the amount of various items described as
under.
(a) Find the total and average export value of all the items.
(b) Find the year in which minimum and maximum export was made.
#include<conio.h>
#include<stdio.h>
void main()
{
int a[5][5],i,j;
float avg[5]={0},min,max,mx,mn;
char c[5][25]={"Tea","Coffee","Sugar","Milk Powder","zinger"};
clrscr();
printf("Enter the follwing details :\n");
for(i=0;i<5;i++)
{
printf("Enter the exported quantity of %s for the 5 consecutive
year",c[i]);
for(j=0;j<5;j++)
{
avg[i]=0;
scanf("%d",&a[i][j]);
}
}
for(i=0;i<5;i++)
for(j=0;j<5;j++)
avg[i]+=a[j][i];
max=min=avg[0];
mx=mn=0;
for(i=1;i<5;i++)
{
if(avg[i]>max){max=avg[i];mx=i+1;}
else if(avg[i]>max){min=avg[i];mn=i+1;}
}
printf("\n\n");
printf("items/yr\t1995-96\t1996-97\t1997-98\t1999-00\t2000-01");
for(i=0;i<5;i++)
{
printf("\n%s\t",c[i]);
for(j=0;j<5;j++)
printf("\t%d",a[i][j]);
printf("\n");
}
printf("===============================================================
============");
printf("\nTotal\t
");
for(i=0;i<5;i++)
printf("%6.2f ",avg[i]);
printf("\nAvg \t
");
for(i=0;i<5;i++)

ByProf.AshokN.Kamthane

Copyright2012DorlingKindersleyIndiaPvt.Ltd.

ProgramminginCISBN:9788131760314
printf("%6.2f ",(float)avg[i]/5);
i=1995+mx;
j=1995+mn;
printf("The minimum and maximum items exported was in the year %d-%d
and %d-%d",j,j+1,i,i+1);
getch();
}

Output:
Enter
Enter
3344
334
5444
5544
4555
Enter
6544
3455
445
5565
4455
Enter
6655
4455
655
5455
655
Enter
5655
665
556
676
566
Enter
5446
6679
989
8999
7766

the follwing details :


the exported quantity of Tea for the 5 consecutive year

the exported quantity of Coffee for the 5 consecutive year

the exported quantity of Sugar for the 5 consecutive year

the exported quantity of Milk Powder for the 5 consecutive year

the exported quantity of zinger for the 5 consecutive year

items/yr
Tea

1995-96 1996-97 1997-98


3344
334
5444

1999-00 2000-01
5544
4555

Coffee

6544

3455

445

5565

4455

Sugar

6655

4455

655

5455

655

Milk Powder

5655

665

556

676

566

zinger
5446
6679
989
8999
7766
=======================================================================
====
Total
27644.00 15588.00 8089.00 26239.00 17997.00
Avg
5528.80 3117.60 1617.80 5247.80
3599.40

ByProf.AshokN.Kamthane

Copyright2012DorlingKindersleyIndiaPvt.Ltd.

ProgramminginCISBN:9788131760314
The minimum and maximum items exported was in the year 1995-1996 and
1997-1998

18. Write a program to replace the zero with successive number in following arrays.
(a) int x[8] ={1,0,3,0,5,0,7,0}
(b) int y[8] = {-1,0,-3,0,-5,0,-7,0}
#include<conio.h>
#include<stdio.h>
void main()
{
int a[8],b[8],i,k=0;
clrscr();
printf("Enter the array 1 of 8 elements:\n");
for(i=0;i<8;i++)
scanf("%d",&a[i]);
printf("\nEnter the array 2 of 8 elements:\n");
for(i=0;i<8;i++)
scanf("%d",&b[i]);
k=a[0];
k++;
for(i=1;i<8;i++,k++)
{
if(a[i]==0)a[i]=k;
if(b[i]==0)b[i]=-1*k;
}
printf("After replacing zero with successive elements\nArray 'a[8]'
becomes : {");
for(i=0;i<8;i++)
printf("%d,",a[i]);
printf("\b}\nArray 'a[8]' becomes : {");
for(i=0;i<8;i++)
printf("%d,",b[i]);
printf("\b}");
getch();
}

Output:
Enter the array 1 of 8 elements:
1 0 3 0 5 0 7 0
Enter the array 2 of
-1 0 -3 0 -5 0 -7 0
After replacing zero
Array 'a[8]' becomes
Array 'a[8]' becomes

8 elements:
with successive elements
: {1,2,3,4,5,6,7,8}
: {-1,-2,-3,-4,-5,-6,-7,-8}

ByProf.AshokN.Kamthane

Copyright2012DorlingKindersleyIndiaPvt.Ltd.

ProgramminginCISBN:9788131760314

ByProf.AshokN.Kamthane

Copyright2012DorlingKindersleyIndiaPvt.Ltd.

ProgramminginCISBN:9788131760314

Chapter 8: Strings and Standard Functions


1. Write a program to arrange a set of fruit names given below in descending order
(reverse alphabetic) (mango, banana, apple, orange, graphs, coconut, water melon and
papaya).
#include<stdio.h>
void main()
{
char
str[8][15]={"mango","banana","apple","orange","graphs","coconut","water
melon","papaya"};
char temp[15];
int i,j;
for(i=0;i<7;i++)
{
for(j=i;j<8;j++)
{
if(str[i][0]<str[j][0])
{
strcpy(temp,str[i]);
strcpy(str[i],str[j]);
strcpy(str[j],temp);
}
}
}
printf("The given names of fruits in sorted order are:\n");
for(i=0;i<8;i++)
printf("%s\n",str[i]);
}

Output:
The given names of fruits in sorted order are:
water melon
papaya
orange
mango
graphs
coconut
banana
apple

2. Write a program to arrange the following names in the alphabetic order. The sorting is
to be done on the first three characters of the first name. (Ashok, Alok , Akash, Amit,
Amol, Anil, Ashish and Anand).
#include<stdio.h>
void main()
{

ByProf.AshokN.Kamthane

Copyright2012DorlingKindersleyIndiaPvt.Ltd.

ProgramminginCISBN:9788131760314
char
name[8][15]={"Ashok","Alok","Akash","Amit","Amol","Anil","Ashish","Anan
d"},temp[15];
int i,j;
for(i=0;i<7;i++)
{
for(j=i;j<8;j++)
{
if((name[i][0]>name[j][0])||((name[i][0]==name[j][0])&&(name[i][1
]>name[j][1]))||((name[i][0]==name[j][0])&&(name[i][1]==name[j][1])&&(n
ame[i][2]>name[j][2])))
{
strcpy(temp,name[i]);
strcpy(name[i],name[j]);
strcpy(name[j],temp);
}
}
}
printf("The given names in sorted order are:\n");
for(i=0;i<8;i++)
printf("%s\n",name[i]);
}

Output:Akash
Alok
Amit
Amol
Anand
Anil
Ashish
Ashok

3. Write a program to enter some text and display the text in reverse order. (Example: I
am happy will be displayed as happy am I).
#include<stdio.h>
void main()
{
char text[80];
int i,n,f,r;
printf("Enter the text\n");
scanf("%[^\n]",text);
n=strlen(text);
r=n-1;
while(text[r]==' ')
r--;
f=r;
while(f>=0)
{
while(text[f]!=' '&& f>=0)

ByProf.AshokN.Kamthane

Copyright2012DorlingKindersleyIndiaPvt.Ltd.

ProgramminginCISBN:9788131760314
f--;
for(i=f+1;i<=r;i++)
printf("%c",text[i]);
printf(" ");
r=f;
while(text[r]==' '&& r>=0)
r--;
f=r;

}
printf("\n");

Output:
Enter the text
I am happy
happy am I

3. Write a program to enter five full names of persons. Display their names, initials and
last names.
#include<stdio.h>
void main()
{
char name[5][3][10];
int i,f,r,n;
printf("Enter full names of five persons in the form First_name
Middle_Name Last_Name,enter each name on new line\n");
for(i=0;i<5;i++)
{
scanf("%s %s %s",name[i][0],name[i][1],name[i][2]);
}
printf("THe names with initials are\n");
for(i=0;i<5;i++)
{
printf("%c. %c.
%s\n",name[i][0][0],name[i][1][0],name[i][2]);
}
}

Output:
Enter full names of five persons in the form First_name Middle_Name
Last_Name,enter each name on new line
Shrikant S Polawar
Krishna P Arutwar
Suraj H Nilapalle
Harshad H Surana
Ashwin V Bagde
THe names with initials are
S. S. Polawar
K. P. Arutwar
S. H. Nilapalle
H. H. Surana
A. V. Bagde

ByProf.AshokN.Kamthane

Copyright2012DorlingKindersleyIndiaPvt.Ltd.

ProgramminginCISBN:9788131760314

4. Write a program to enter text through keyboard. Convert first character of each word
in capital and display the text.
#include<stdio.h>
void main()
{
char text[80];
int f,n;
printf("Enter the text at the end press enter\n");
scanf("%[^\n]",text);
n=strlen(text);
f=0;
while(f<n)
{
while(text[f]==' ' && f<n)
f++;
if(text[f]>=97 && text[f]<=122)
text[f]-=32;
while(text[f]!=' '&&f<n)
f++;
}
printf("%s\n",text);
}

Output:
Enter the text at the end press enter
i love my country
I Love My Country

6. Write a program to enter some text through the keyboard. Insert dot (.) after every
three words in the text. The first character after every dot should be converted to capital.
#include<stdio.h>
void main()
{
char text[80];
int f,n,count=0;
printf("Enter the text at the end press enter\n");
scanf("%[^\n]",text);
n=strlen(text);
f=0;
while(text[f]==' ' && f<n)
f++;
if(text[f]>=97 && text[f]<=122)
text[f]-=32;

ByProf.AshokN.Kamthane

Copyright2012DorlingKindersleyIndiaPvt.Ltd.

ProgramminginCISBN:9788131760314
while(f<n)
{
while(text[f]!=' ' && f<n)
f++;
count++;
while(text[f]==' ' && f<n)
//Removing the extra spaces
between two words
f++;
if(count%3==0)
{
if(text[f]>=97 && text[f]<=122)
text[f]-=32;
}
}
printf("Text aftre processing :\n%s",text);
}

Output:
Enter the text at the end press enter
c is one of the middle level languages, it is compiled language.
Text aftre processing :
C is one Of the middle Level languages, it Is compiled language.

7. Write a program to enter some text through the keyboard. Count the number of words
that starts from w. Display such words and count them.
#include<stdio.h>
void main()
{
char text[80];
int f,n,count;
printf("Enter the text at the end press enter\n");
scanf("%[^\n]",text);
n=strlen(text);
f=0;
while(f<n)
{
while(text[f]==' ' && f<n)
f++;
if(text[f]=='w'||text[f]=='W')
count++;
while(text[f]!=' '&&f<n)
f++;
}
printf("No. of words starting with w is :%d\n",count);
}

Output:
Enter the text at the end press enter
WHO that is world health organisation is the one which works for social
purpose
No. of words starting with w is :4

ByProf.AshokN.Kamthane

Copyright2012DorlingKindersleyIndiaPvt.Ltd.

ProgramminginCISBN:9788131760314

9. Write a program to encrypt the text INDIA.The output should be KPFKC. (A is to


be replaced with C, B with D and C with E and so on.)
#include<stdio.h>
void main()
{
char str[]="INDIA";
int n,i;
n=strlen(str);
for(i=0;i<n;i++)
str[i]+=2;
printf("Text after encryption\t:%s",str);
}

Output:
Text after encryption

:KPFKC

10. Write a program to dycrypt the text KPFKC to get original string INDIA.
#include<stdio.h>
void main()
{
char str[]="KPFKC";
int i,n;
printf("Decryting text using the encryption key used in above
algorithm\n");
printf("Text after decryption is:");
n=strlen(str);
for(i=0;i<n;i++)
{
str[i]=str[i]-2;
}
printf("%s",str);
}

Output:
Decryting text using the encryption key used in above algorithm
Text after decryption is:INDIA

ByProf.AshokN.Kamthane

Copyright2012DorlingKindersleyIndiaPvt.Ltd.

ProgramminginCISBN:9788131760314

Chapter 9: Pointers
1. Write a program to accept a string using character pointer and display it.
#include<stdio.h>
#include<conio.h>
void main()
{
char *ch;
clrscr();
printf("Enter the string : ");
scanf("%s",ch);
printf("\n\n\t\tYou entered : %s",ch);
getch();
}

Output:
Enter the string : Ashok
You entered : Ashok

2. Write a program to calculate square and cube of the entered number using pointer
of the variable containing entered number.
#include<stdio.h>
#include<conio.h>
void main()
{
int a,*p;
clrscr();
printf("Enter any number : ");
scanf("%d",&a);
p=&a;
printf("\n\nSquare of %d : %d\n\n Cube of %d : %d\n ", *p, *p**p,
*p, *p**p**p);
getch();
}

3. Write a program to display all the elements of an array using pointer.


#include<stdio.h>
#include<conio.h>
void main()
{

ByProf.AshokN.Kamthane

Copyright2012DorlingKindersleyIndiaPvt.Ltd.

ProgramminginCISBN:9788131760314

int arr[4]={1,5,8,3},*p,i;
clrscr();
p=arr;
printf("Showing array elements using pointer...\n");
for(i=0;i<4;i++)
printf("arr[%d] : %d\n",i,*p++);
getch();

Output:
Showing array elements using pointer...
arr[0] : 1
arr[1] : 5
arr[2] : 8
arr[3] : 3

4. Write a program to allocate memory for 10 integers.


#include <stdio.h>
#include <alloc.h>
#include <process.h>
void main()
{
int *ptr,i;
clrscr();
if ((ptr = (int *) malloc(10)) == \0)
{
printf("Not enough memory...\n");
exit(1);
}
else printf("Memory allocation successful for 10 integers..\n");
printf("Enter 10 integers..\n");
for(i=0;i<10;i++)
scanf("%d",&ptr[i]);
printf("\nEntered 10 integers are..\n");
for(i=0;i<10;i++)
printf("%d\t",ptr[i]);
free(ptr);
getch();
}

Output:
Memory allocation successful for 10 integers..
Enter 10 integers..
2 4 6 8 0 1 3 5 7 9
Entered 10 integers are..
2
4
6
8
9

5. Write a program to demonstrate the use of realloc().


#include <stdio.h>
#include <alloc.h>

ByProf.AshokN.Kamthane

Copyright2012DorlingKindersleyIndiaPvt.Ltd.

ProgramminginCISBN:9788131760314
#include <process.h>
void main()
{
char *str;
clrscr();
str = (char *) malloc(7);
strcpy(str, "Hello");
printf("String is %s\n Address is %u\n", str, str);

str = (char *) realloc(str,3);


strcpy(str, "Hi");
printf("New String is %s\n New address is %d\n", str, str);
free(str);
getch();

Output:
String is Hello
Address is 2342
New String is Hi
New address is 2358

ByProf.AshokN.Kamthane

Copyright2012DorlingKindersleyIndiaPvt.Ltd.

ProgramminginCISBN:9788131760314

Chapter 10: Functions


1. Write a program to display three different Metro names of India by using different
functions.
#include<stdio.h>
void del();
void kol();
void mum();
void main()
{
del();
mum();
kol();
}
void del()
{
printf("DELHI\n");
}
void mum()
{
printf("MUMBAI\n");
}
void kol()
{
printf("KOLKATA\n");
}

Output:
DELHI
MUMBAI
KOLKATA

2. Write a program with two functions and call one function from other.
#include<stdio.h>
void fun1();
void fun2();
void main()
{
printf("Calling fun1() from main()\n");
fun1();
printf("We are back in main now\n");
}
void fun1()
{
printf("We are in fun1()\n");
printf("Calling fun2() from fun1()...\n");
fun2();
printf("We are again in fun1()\n");

ByProf.AshokN.Kamthane

Copyright2012DorlingKindersleyIndiaPvt.Ltd.

ProgramminginCISBN:9788131760314
}
void fun2()
{
printf("We are in fun2 now\n");
}

Output:
Calling fun1() from main()
We are in fun1()
Calling fun2() from fun1()...
We are in fun2 now
We are again in fun1()
We are back in main now

3. Write a program which takes an int argument and calculates its cube.
#include<stdio.h>
int xcube(int x)
{
return(x*x*x);
}
void main()
{
int n;
printf("Enter a no.\n");
scanf("%d",&n);
printf("Cube of %d is %d\n",n,xcube(n));
}

Output:
Enter a no.
3
Cube of 3 is 27

4. Write a program to display the table of given number. Write different functions for
accepting the input, calculating the table and displaying the value.
#include<stdio.h>
int input();
void cal(int,int *);
void display(int *);
void main()
{
int n,tab[10];
n=input();
cal(n,tab);
display(tab);
}
int input()
{
int n;

ByProf.AshokN.Kamthane

Copyright2012DorlingKindersleyIndiaPvt.Ltd.

ProgramminginCISBN:9788131760314

printf("Enter a no.\n");
scanf("%d",&n);
return n;

void cal(int n ,int *tab)


{
int i;
for(i=1;i<=10;i++)
*(tab+i)=n*i;
}
void display(int *tab)
{
int i;
printf("Multiplication table of %d is as follows\n",*(tab+1));
for(i=1;i<=10;i++)
{
printf("%d\n",*(tab+i));
}
}

Output:
Enter a no.
5
Multiplication table of 5 is as follows
5
10
15
20
25
30
35
40
45
50

5. Write a program to calculate the sum of digits of a number. Use a function to return the
sum.
#include<stdio.h>
int calsum(int);
void main()
{
int n;
printf("Enter a no.\n");
scanf("%d",&n);
printf("Sum of digits of no. %d is %d \n",n,calsum(n));
}
int calsum(int n)
{
int t=0;

ByProf.AshokN.Kamthane

Copyright2012DorlingKindersleyIndiaPvt.Ltd.

ProgramminginCISBN:9788131760314

while(n)
{
t=t+n%10;
n=n/10;
}
return t;

Output:
Enter a no.
234
Sum of digits of no. 234 is 9

6. Write a program to swap the two variables present in one function to other function.
#include<stdio.h>
void swap(int *,int *);
void fun();
void main()
{
fun();
}
void fun()
{
int a,b;
printf("Enter two no.s\n");
scanf("%d%d",&a,&b);
printf("Before calling swap()\na=%d\tb=%d",a,b);
swap(&a,&b);
printf("\nAfter calling swap()\na=%d\tb=%d",a,b);
}
void swap(int *a,int *b)
{
int temp=*a;
*a=*b;
*b=temp;
}

Output:
Enter two no.s
10
20
Before calling swap()
a=10
b=20
After calling swap()
a=20
b=10

7. Write a program to sort an array (in descending order) in different function and return
it to the original function.
#include<stdio.h>
void sort(int ,int *);

ByProf.AshokN.Kamthane

Copyright2012DorlingKindersleyIndiaPvt.Ltd.

ProgramminginCISBN:9788131760314
void main()
{
int arr[10],n,i;
printf("Enter size of array\n");
scanf("%d",&n);
printf("Enter the array elements\n");
for(i=0;i<n;i++)
{
scanf("%d",&arr[i]);
}
sort(n,arr);
printf("Array after sorting is\n");
for(i=0;i<n;i++)
{
printf("%d\t",arr[i]);
}
}
void sort(int num,int *array)
{
int i,j,t;
for(i=0;i<num-1;i++)
{
for(j=i;j<num;j++)
{
if(array[i]>array[j])
{
t=array[i];
array[i]=array[j];
array[j]=t;
}
}
}
}

Output:
Enter
5
Enter
9 2 8
Array
0

size of array
the array elements
3 0
after sorting is
2
3
8

8. Write a program to display Hello! five times. Create a user-defined function


message().
#include<stdio.h>
void message()
{
int i;
for(i=0;i<5;i++)
printf("Hello!\n");
}
void main()
{

ByProf.AshokN.Kamthane

Copyright2012DorlingKindersleyIndiaPvt.Ltd.

ProgramminginCISBN:9788131760314

message();

Output:
Hello!
Hello!
Hello!
Hello!
Hello!

9. Write a program to calculate average marks of five subjects by using pass by value.
#include<stdio.h>
float avg(int *);
void main()
{
int arr[5],i;
float a;
printf("Enter the marks of five subjects\n");
for(i=0;i<5;i++)
{
scanf("%d",&arr[i]);
}
a=avg(arr);
printf("Average of the marks of five subjects is
}

:%f",a);

float avg(int *arr)


{
int t=0,i;
for(i=0;i<5;i++)
t=t+*(arr+i);
return(t/5.0);
}

Output:
Enter the marks of five subjects
69 74 83 78 82
Average of the marks of five subjects is

:77.199997

10. Write a user-defined function for performing the following tasks.


(a) Cube of a number
(b) Perimeter of a circle
(c) Conversion of binary to decimal
(d) Addition of individual digits of a given number
#include<stdio.h>
#include<stdlib.h>
void xcube();
void per();
void b2d();
void sod();
void main()
{
int n;

ByProf.AshokN.Kamthane

Copyright2012DorlingKindersleyIndiaPvt.Ltd.

ProgramminginCISBN:9788131760314
printf("1-To calculate cube of a no.\n2-To calculate perimeter of
a circle\n3-To convert binary to decimal\n4-To calculate sum of digits
of a no.\n5-Exit\n");
scanf("%d",&n);
switch(n)
{
case 1:
xcube();
break;
case 2:
per();
break;
case 3:
b2d();
break;
case 4:
sod();
break;
case 5:
exit(0);
default:
printf("Invalid choice\n");
}
}
void xcube()
{
int n;
printf("Enter a no.\n");
scanf("%d",&n);
printf("Cube of %d is %d",n,n*n*n);
}
void per()
{
int r;
printf("Enter the radius of the circle\n");
scanf("%d",&r);
printf("Perimeter of the circle with radius %d is
%f",r,(2*22.0*r)/7.0);
}
void b2d()
{
char str[10];
int n,d=0,i,t;
printf("Enter a binary no.\n");
scanf("%s",str);
n=strlen(str);
for(i=n-1;i>=0;i--)
{
t=pow(2,n-1-i);
d=d+t*(str[i]-48);
}
printf("Decimal equivalent of %s is %d",str,d);

ByProf.AshokN.Kamthane

Copyright2012DorlingKindersleyIndiaPvt.Ltd.

ProgramminginCISBN:9788131760314
}
void sod()
{
int n,s=0;
printf("Enter a no.\n");
scanf("%d",&n);
printf("Sum of digits of no. %d is :",n);
while(n)
{
s=s+n%10;
n=n/10;
}
printf("%d",s);
}

Output:
1-To calculate cube of a no.
2-To calculate perimeter of a circle
3-To convert binary to decimal
4-To calculate sum of digits of a no.
5-Exit
3
Enter a binary no.
1101
Decimal equivalent of 1101 is 13

11. Write a program to reverse the given number recursively.


#include<stdio.h>
int rev(int n,int t);
void main()
{
int n,t=0;
printf("Enter a no.\n");
scanf("%d",&n);
printf("Reverse of %d is %d",n,rev(n,t));
}
int rev(int n,int t)
{
if(n==0)
return ;
else
{
t=t*10+n%10;
return rev(n/10,t);
}
}

Output:
Enter a no.
3254
Reverse of 3254 is 4523

ByProf.AshokN.Kamthane

Copyright2012DorlingKindersleyIndiaPvt.Ltd.

ProgramminginCISBN:9788131760314

12. Write a program to add 1 to 10 numbers recursively.


#include<stdio.h>
int radd(int n);
void main()
{
printf("Summation of no.s from 1 to 10 is :%d",radd(10));
}
int radd(int n)
{
if(n==0)
return n;
else
return n+radd(n-1);
}

Output:
Summation of no.s from 1 to 10 is :55

ByProf.AshokN.Kamthane

Copyright2012DorlingKindersleyIndiaPvt.Ltd.

ProgramminginCISBN:9788131760314

Chapter 11: Storage Class


1. Write a program to calculate the sum of digits of the entered number. Create userdefined function sumd(). Use the static variable and calculate the sum of digits.
#include<conio.h>
#include<stdio.h>
float sumd(float t)
{
static sum;
sum += t;
return sum;
}
void main()
{
int n,i;
float t;
clrscr();
printf("Enter the number of variables to be added. :");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter the number #%d :",i+1);
scanf("%f",&t);
sumd(t);
}
printf("The sum of entered digits is : %f",sumd(0));
getch();
}

Output:
Enter the number of variables to be added. :5
Enter the number #1: 12
Enter the number #2: 33
Enter the number #3: 45
Enter the number #4: 6
Enter the number #5: 4
The sum of entered digits is: 100.000000

2. Write a program to call function main() recursively. The program should be


terminated when the main() function is called during fifth time. Take help of the
static variable.
#include<conio.h>
#include<stdio.h>
#include<process.h>
void main()
{
static i;
printf("\nCall number to main() #%d ",1+i);
if(i++==4)
exit(0);

ByProf.AshokN.Kamthane

Copyright2012DorlingKindersleyIndiaPvt.Ltd.

ProgramminginCISBN:9788131760314

main();

Output:
Call
Call
Call
Call
Call

number
number
number
number
number

to
to
to
to
to

main()
main()
main()
main()
main()

#1
#2
#3
#4
#5

3. Write a program to calculate the triangular number. Use the static variable with the
user defined function.
#include<conio.h>
#include<stdio.h>
void main()
{
int i,n,s=0;
clrscr();
printf("Enter the number for calculatig trianhular number.:");
scanf("%d",&n);
for(i=0;i<n;i++)
s+=i+1;
printf("\nThe triangular number is %d",s);
getch();
}

Output
Enter the number for calculating triangular number: 5
The triangular number is 15

4. Write a program to create the for loop from 1 to 10000000. Declare the loop variable
of class register and auto. Observe the time required for completing the loop for
both types of variables. Find out in which class the execution time is minimum.
#include
#include
#include
#include

<time.h>
<stdio.h>
<dos.h>
<conio.h>

int main(void)
{
time_t first, second;
float i;
register float j;
clrscr();
first = time(NULL);
for(i=0;i<10000000;i++);
second = time(NULL);
printf("The difference is: %f seconds\n",difftime(second,first));
first = time(NULL);

ByProf.AshokN.Kamthane

Copyright2012DorlingKindersleyIndiaPvt.Ltd.

ProgramminginCISBN:9788131760314
for(j=0;j<10000000;j++);
second = time(NULL);
printf("The difference is: %f seconds\n",difftime(second,first));
getch();
}

return 0;

Output:
The difference is: 0.000000 seconds
The difference is: 1.000000 seconds

ByProf.AshokN.Kamthane

Copyright2012DorlingKindersleyIndiaPvt.Ltd.

ProgramminginCISBN:9788131760314

Chapter 13: Structure and Union


1. Write a program to define a structure with tag state with fields state name, number of
districts and total population. Read and display the data.
#include <stdio.h>
#include <conio.h>
struct state
{
char state_name[20];
int no_of_districts;
long population;
};
void main()
{
struct state st1;
clrscr();
printf("Enter state information : \n");
printf("State name : ");
scanf("%s",st1.state_name);
printf("No. of Districts : ");
scanf("%d",&st1.no_of_districts);
printf("Population : ");
scanf("%ld",&st1.population);
printf("\n\tThe state information you entered is : \n");
printf(" State name : %s\n",st1.state_name);
printf("No. of Districts :%d \n ",st1.no_of_districts);
printf(" Population : %ld\n ",st1.population);
getch();
}

Output:
Enter state information :
State name : Maharashtra
No. of Districts : 35
Population : 96752247
The state information you entered is :
State name : Maharashtra
No. of Districts :35
Population : 96752247

2. Write the program (1)-using pointer to structure.


#include <stdio.h>
#include <conio.h>
struct state
{

ByProf.AshokN.Kamthane

Copyright2012DorlingKindersleyIndiaPvt.Ltd.

ProgramminginCISBN:9788131760314
char state_name[20];
int no_of_districts;
long population;
};
void main()
{
struct state *st;
clrscr();
printf("Enter state information : \n");
printf("State name : ");
scanf("%s",st->state_name);
printf("No. of Districts : ");
scanf("%d",&st->no_of_districts);
printf("Population : ");
scanf("%ld",&st->population);
printf("\n\tThe state information you entered is : \n");
printf(" State name : %s\n",st->state_name);
printf("No. of Districts :%d \n ",st->no_of_districts);
printf(" Population : %ld\n ",st->population);
getch();
}

Output:
Enter state information :
State name : Maharashtra
No. of Districts : 35
Population : 96752247
The state information you entered is :
State name : Maharashtra
No. of Districts :35
Population : 96752247

3. Define a structure with tag population with fields Men and Women. Create a structure
within a structure using state and population structure. Read and display the data.
#include <stdio.h>
#include <conio.h>
struct population
{
long men,women;
};
struct state
{
char state_name[20];
int no_of_districts;
struct population p1;
};
void main()
{
struct state st;
clrscr();
printf("Enter state information : \n");
printf("State name : ");

ByProf.AshokN.Kamthane

Copyright2012DorlingKindersleyIndiaPvt.Ltd.

ProgramminginCISBN:9788131760314

scanf("%s",st.state_name);
printf("No. of Districts : ");
scanf("%d",&st.no_of_districts);
printf("Population of Men: ");
scanf("%ld",&st.p1.men);
printf("Population of Women: ");
scanf("%ld",&st.p1.women);
printf("\n\tThe state information you entered is : \n");
printf(" State name : %s\n",st.state_name);
printf("No. of Districts :%d \n ",st.no_of_districts);
printf(" Population of Men : %ld\n ",st.p1.men);
printf(" Population of Women : %ld\n ",st.p1.women);
printf("Total population : %ld",st.p1.men+st.p1.women);
getch();

Output:
Enter state information :
State name : MH
No. of Districts : 35
Population of Men: 50400596
Population of Women: 46478137
The state information you entered is :
State name : MH
No. of Districts :35
Population of Men : 50400596
Population of Women : 46478137
Total population : 96878733

4. Modify the program developed using exercise (3). Create array of structure variables.
Read and display five records.
#include <stdio.h>
#include <conio.h>
struct population
{
long men,women;
};
struct state
{
char state_name[20];
int no_of_districts;
struct population p1;
};
void main()
{
struct state st[5];
int i;
clrscr();
for(i=0;i<5;i++)
{
printf("Record %d\nEnter state information : \n",i+1);
printf("State name : ");
scanf("%s",st[i].state_name);

ByProf.AshokN.Kamthane

Copyright2012DorlingKindersleyIndiaPvt.Ltd.

ProgramminginCISBN:9788131760314

printf("No. of Districts : ");


scanf("%d",&st[i].no_of_districts);
printf("Population of Men: ");
scanf("%ld",&st[i].p1.men);
printf("Population of Women: ");
scanf("%ld",&st[i].p1.women);
}
printf("\n\tThe state information you entered is : \n");
for(i=0;i<5;i++)
{
printf("Record %d\n State name : %s\n",i+1,st[i].state_name);
printf("No. of Districts :%d \n ",st[i].no_of_districts);
printf(" Population of Men : %ld\n ",st[i].p1.men);
printf(" Population of Women : %ld\n ",st[i].p1.women);
printf("Toatal population : %ld",st[i].p1.men+st[i].p1.women);
}
getch();

Output:
Record 1
State name : MP
No. of Districts :23
Population of Men : 34543545
Population of Women : 30456522
Record 2
State name : Keral
No. of Districts : 20
Population of Men: 30321312
Population of Women: 28344432
Record 3
Enter state information :
State name : AP
No. of Districts : 33
Population of Men: 45425676
Population of Women: 40321345
Record 4
Enter state information :
State name : UP
No. of Districts : 52
Population of Men: 70543422
Population of Women: 56533243
Record 5
Enter state information :
State name : GOA
No. of Districts : 6
Population of Men: 1323434
Population of Women: 1023498
The state information you entered is :

ByProf.AshokN.Kamthane

Copyright2012DorlingKindersleyIndiaPvt.Ltd.

ProgramminginCISBN:9788131760314
Record 1
State name : MP
No. of Districts :23
Population of Men : 34543545
Population of Women : 30456522
Toatal population : 65000067
Record 2
State name : Keral
No. of Districts :20
Population of Men : 30321312
Population of Women : 28344432
Toatal population : 58665744
Record 3
State name : AP
No. of Districts :33
Population of Men : 45425676
Population of Women : 40321345
Toatal population : 85747021
Record 4
State name : UP
No. of Districts :52
Population of Men : 70543422
Population of Women : 56533243
Toatal population : 127076665
Record 5
State name : GOA
No. of Districts :6
Population of Men : 1323434
Population of Women : 1023498
Toatal population : 2346932

5. Create user-defined data type equivalent to int. Declare three variables of its type.
Perform arithmetic operations using these variables.
#include <stdio.h>
#include <conio.h>
void main()
{

typedef int length;


length l1=5,l2=3,l3=4;
clrscr();
printf("l1=%d\nl2=%d\nl3=%d\nTotal length : %d",l1,l2,l3,l1+l2+l3);
getch();

Output:
l1=5
l2=3

ByProf.AshokN.Kamthane

Copyright2012DorlingKindersleyIndiaPvt.Ltd.

ProgramminginCISBN:9788131760314
l3=4
Total length : 12

6. Create enumerated data type logical with TRUE and FALSE values. Write a program
to check whether the entered number is positive or negative. If the number is positive
display 0 otherwise display 1. Use enumerated data type logical to display 0 and 1.
#include <stdio.h>
#include <conio.h>
#define TRUE 0
#define FALSE 1
void main()
{
int n;
typedef int logical;
logical l;
clrscr();
printf("Enter a Number : ");
scanf("%d",&n);
if(n>0)
{
l=TRUE;
printf("logical : %d -> %d is positive",l,n);
}
else if(n<0)
{
l=FALSE;
printf("logical : %d -> %d is negative",l,n);
}
}

getch();

Output:
1]
Enter a Number : -4
logical : 1 -> -4 is negative
2]
Enter a Number : 6
logical : 0 -> 6 is positive

7. Write a program to accept records of the different states using array of structures. The
structure should contain char state, int population, int literacy rate and int per capita
income. Assume suitable data. Display the state whose literacy rate is the highest and
whose per capita income is the highest.
#include <stdio.h>
#include <conio.h>

ByProf.AshokN.Kamthane

Copyright2012DorlingKindersleyIndiaPvt.Ltd.

ProgramminginCISBN:9788131760314
struct state
{
char state_name[20];
long int population;
int literacy_rate,per_capita_income;
};
void main()
{
struct state st[3];
int i;
int max;
clrscr();
for(i=0;i<3;i++)
{
printf("Record %d\nEnter state information : \n",i+1);
printf("State name : ");
scanf("%s",st[i].state_name);
printf("population : ");
scanf("%ld",&st[i].population);
printf("Literacy rate : ");
scanf("%d",&st[i].literacy_rate);
printf("Per capita income : ");
scanf("%d",&st[i].per_capita_income);
}
max=0;
for(i=1;i<3;i++)
{
if(st[i].literacy_rate>st[max].literacy_rate)
max=i;
}
printf("\nState with highest literacy : %s",st[max].state_name);
max=0;
for(i=1;i<3;i++)
{
if(st[i].per_capita_income>st[max].per_capita_income)
max=i;
}
printf("\nState with highest per capita income :
%s",st[max].state_name);
}

getch();

Output:
Record 1
Enter state information :
State name : AP
population : 56433423
Literacy rate : 78
Per capita income : 34643
Record 2
Enter state information :
State name : UP
population : 87234232

ByProf.AshokN.Kamthane

Copyright2012DorlingKindersleyIndiaPvt.Ltd.

ProgramminginCISBN:9788131760314
Literacy rate : 34
Per capita income : 12432
Record 3
Enter state information :
State name : Keral
population : 40243423
Literacy rate : 98
Per capita income : 40234
State with highest literacy : Keral
State with highest per capita income : Keral

8. Write a program to accept records of different states using array of structures. The
structure should contain char state and number of int engineering collages, int medical
collages, int management collages and int universities. Calculate the total collages and
display the state, which is having the highest number of collages.
#include <stdio.h>
#include <conio.h>
struct state
{
char state_name[20];
int engg,med,mang,univ;
int total_clg;
};
void main()
{
struct state st[3];
int i;
int max;
clrscr();
for(i=0;i<3;i++)
{
printf("Record %d\nEnter state information : \n",i+1);
printf("State name : ");
scanf("%s",st[i].state_name);
printf("No. of Engg. colleges : ");
scanf("%d",&st[i].engg);
printf("No. of Medical colleges : ");
scanf("%d",&st[i].med);
printf("No. of Management colleges : ");
scanf("%d",&st[i].mang);
printf("No. of Universities : ");
scanf("%d",&st[i].univ);
st[i].total_clg=st[i].engg+st[i].med+st[i].mang;
}
max=0;
for(i=1;i<3;i++)
{
if(st[i].total_clg>st[max].total_clg)
max=i;
}
printf("\nState with max colleges : %s",st[max].state_name);

ByProf.AshokN.Kamthane

Copyright2012DorlingKindersleyIndiaPvt.Ltd.

ProgramminginCISBN:9788131760314

getch();

Output:
Record 1
Enter state information :
State name : AP
No. of Engg. colleges : 432
No. of Medical colleges : 342
No. of Management colleges : 234
No. of Universities : 7
Record 2
Enter state information :
State name : MP
No. of Engg. colleges : 342
No. of Medical colleges : 234
No. of Management colleges : 78
No. of Universities : 13
Record 3
Enter state information :
State name : Maharashtra
No. of Engg. colleges : 543
No. of Medical colleges : 58
No. of Management colleges : 234
No. of Universities : 15
State with max colleges : AP

ByProf.AshokN.Kamthane

Copyright2012DorlingKindersleyIndiaPvt.Ltd.

ProgramminginCISBN:9788131760314

Chapter 14: Files


1. Write a program to generate a data file containing the list of cricket players, no. of
innings played, highest run score and no. of hayricks made by them. Use structure
variable to store the cricketers name, no. of innings played, highest run score and the
number of hayricks.
#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
struct cricket
{
char name[20];
int inn,hruns,hatrik;
};
void main()
{
FILE *fp;
int ch='y';
struct cricket c;
clrscr();
if((fp=fopen("List.txt","wb"))==NULL)
{
printf("Cannot open the file\n");
exit(0);
};
while(ch=='y'||ch=='Y')
{
printf("Enter the name of cricketer :");
scanf("%s",c.name);
printf("Enter the number of innings played :");
scanf("%d",&c.inn);
printf("Enter the highest run scored :");
scanf("%d",&c.hruns);
printf("Enter the number of times hatrik taken:");
scanf("%d",&c.hatrik);
fwrite(&c,sizeof(c),1,fp);
printf("Do you want to enter next players detail(y/n):");
scanf(" %c",&ch);
}
fclose(fp);
fp=fopen("List.txt","rb");
clrscr();
printf("Name\tHighest Runs\tNo. of innings\tNo. of htriks\n");
printf("===============================================================
======\n");
while(fread(&c,sizeof(c),1,fp)!=NULL)
{
printf("%s\t\t%d\t\t%d\t\t%d\n",c.name,c.hruns,c.inn,c.hatrik);
}

ByProf.AshokN.Kamthane

Copyright2012DorlingKindersleyIndiaPvt.Ltd.

ProgramminginCISBN:9788131760314
fclose(fp);
getch();
}

Output:
Enter the name of cricketer :Harshad
Enter the number of innings played :233
Enter the highest run scored :3233
Enter the number of times hatrik taken:5
Do you want to enter next players detail(y/n):y
Enter the name of cricketer :Shrikant
Enter the number of innings played :43
Enter the highest run scored :2322
Enter the number of times hatrik taken:5
Do you want to enter next players detail(y/n):n
Name
Highest Runs
No. of innings No. of htriks
=====================================================================
Harshad
3233
233
5
Shrikant
2322
43
5

2. Write a program to reposition the file to its 10th character.


#include<stdio.h>
#include<conio.h>
#include<process.h>
void main()
{
FILE *fp;
char name[20];
int i;
char ch;
clrscr();
printf("Enter the name of file. :");
scanf("%s",name);
fp=fopen(name,"r");
if(fp==NULL)
{
printf("File does not exists!");
exit(0);
}
printf("The first 15 characters from the file are \n\"");
i=1;
while(i<=15&&(ch=fgetc(fp))!=EOF)
{
printf("%c",ch);
i++;
}
printf("\"\n");
fseek(fp,9,SEEK_SET);
printf("The 10th character is '%c'",fgetc(fp));
getch();
}

ByProf.AshokN.Kamthane

Copyright2012DorlingKindersleyIndiaPvt.Ltd.

ProgramminginCISBN:9788131760314

Output:
Enter the name of file. :cricket.c
The first 15 characters from the file are
"#include<conio."
The 10th character is 'c'

3. Write a program to display contents of file on the screen. The program should ask for
file name. Display the contents in capital case.
#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
void main()
{
FILE* fp;
char name[20],ch;
clrscr();
printf("Enter the name of file to be opened.:");
scanf("%s",name);
if((fp=fopen(name,"r"))==NULL)
{
printf("File does not exits!");
exit(0);
}
while((ch=fgetc(fp))!=EOF)
{
if(islower(ch))ch=toupper(ch);
printf("%c",ch);
}
fclose(fp);
getch();
}

Output:
Enter the name of file to be opened.:b2.cpp
#INCLUDE<IOSTREAM.H>
#INCLUDE<CONIO.H>
CLASS SHAPE
{
INT SIZE;
PUBLIC :
SHAPE();
SHAPE(INT SIZE)
{
THIS.SIZE = SIZE;
}

ByProf.AshokN.Kamthane

Copyright2012DorlingKindersleyIndiaPvt.Ltd.

ProgramminginCISBN:9788131760314
SHAPE& OPERATOR <(SHAPE &P)
{
RETURN SIZE<P.SIZE ? P: *THIS;
}
INT GETSIZE()
{
RETURN SIZE;
}
};
VOID MAIN(){
SHAPE S1(20);
SHAPE S2(33);
SHAPE S;
S = S1<S2;
S1.LESSTHAN(S2);
COUT<<S.GETSIZE();
}

4. Write a program to find the size of the file.


#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
void main()
{
FILE* fp;
char name[20],ch;
int count;
clrscr();
printf("Enter the name of file to find its size :");
scanf("%s",name);
if((fp=fopen(name,"r"))==NULL)
{
printf("File does not exits!");
exit(0);
}
while((ch=fgetc(fp))!=EOF)
{
count++;
}
printf("Size of file %s is %d bytes",name,count);
fclose(fp);
getch();
}

Output:
Enter the name of file to find its size: b2.cpp
Size of file b2.cpp is 1674 bytes

5. Write a program to combine contents of two files in a third file. Add line number at the
beginning of each line.
ByProf.AshokN.Kamthane

Copyright2012DorlingKindersleyIndiaPvt.Ltd.

ProgramminginCISBN:9788131760314

#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
void main()
{
FILE *fp,*fp1,*f;
char s[20],s1[20];
char ch;
int i;
clrscr();
printf("Enter the name of 2 files to be merged :");
scanf("%s%s",s,s1);
if((fp=fopen(s,"r"))==NULL ||(fp1=fopen(s1,"r"))==NULL )
{
printf("File does not exists !");
exit(0);
}
f=fopen("Third.txt","w");
i=1;
fprintf(f,"%d ",i);
while((ch=fgetc(fp))!=EOF)
{
fputc(ch,f);
if(ch=='\n')
{
fprintf(f,"%d ",++i);
}
}
while((ch=fgetc(fp1))!=EOF)
{
fputc(ch,f);
if(ch=='\n')
{
fprintf(f,"%d ",++i);
}
}
printf("\nThe name new file is Third.txt");
getch();
}

Output:
Enter the name of 2 files to be merged: a.txt a1.txt
The name new file is Third.txt

6. Write a program to display numbers from 1 to 100. Re-direct the output of the program
to text file.
#include<conio.h>
#include<stdio.h>
void main()
{
int i=1;

ByProf.AshokN.Kamthane

Copyright2012DorlingKindersleyIndiaPvt.Ltd.

ProgramminginCISBN:9788131760314
clrscr();
for(;i<=100;i++)
printf("%d\n",i);
getch();
}

Output:
Note:
Type the command on command prompt as (assuming the file name of
program is e6.c)
C:> e6.exe > fileName

7. Write a program to write contents of one file in reverse into another file.
#include<conio.h>
#include<stdio.h>
#include<process.h>
void main()
{
FILE *fp,*fp1;
char ch,name[20];
int count=0,i;
printf("Enter the name of file whose contents to be reversed :");
scanf("%s",name);
if((fp=fopen(name,"r"))==NULL||(fp1=fopen("reverse.txt","w"))==NULL)
{printf("File can't be opened!!");
exit(0);
}
while((ch=fgetc(fp))!=EOF)
count++;
fseek(fp,0,SEEK_END);
for(i=1;i<=count;i++)
{
fseek(fp,-1*i,SEEK_END);
ch=fgetc(fp);
if(ch=='\n')
{
count++; i++;
fseek(fp,-1*i,SEEK_END);
ch=fgetc(fp);
}
fputc(ch,fp1);
}
printf("The reversed content of entered file is stored in
reverse.txt");
fclose(fp);
fclose(fp1);
getch();
}

Output:
Enter the name of file whose contents to be reversed: a.txt
The reversed content of entered file is stored in reverse.txt

ByProf.AshokN.Kamthane

Copyright2012DorlingKindersleyIndiaPvt.Ltd.

ProgramminginCISBN:9788131760314

8. Write a program to interchange contents of two files.


#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
void main()
{
FILE *fp,*fp1,*f;
char n[20],n1[20],ch;
clrscr();
printf("Enter the name of 2 files to be interchanged : ");
scanf("%s%s",n,n1);
fp=fopen(n,"r+");
fp1=fopen(n1,"r+");
f=fopen("temp.txt","w+");
if(fp==NULL||fp1==NULL)
{
printf("File can't be opened. :");
exit(0);
}
while((ch=fgetc(fp))!=EOF)
fputc(ch,f);
fp=freopen(n,"w",fp);
while((ch=fgetc(fp1))!=EOF)
fputc(ch,fp);
fp1=freopen(n1,"w",fp1);
fseek(f,0,SEEK_SET);
while((ch=fgetc(f))!=EOF)
fputc(ch,fp1);
fclose(f);
fclose(fp1);
fclose(fp);
printf("Files contents has been interchanged.");
getch();
}

Output:
Enter the name of 2 files to be interchanged: cric.txt hello.txt
Files contents have been interchanged.

ByProf.AshokN.Kamthane

Copyright2012DorlingKindersleyIndiaPvt.Ltd.

ProgramminginCISBN:9788131760314

Chapter 15: Graphics Programming


1. Write a program to draw two overlapping circles.
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
main()
{
int gd=DETECT,gm,x,y;
initgraph(&gd,&gm,"c:\turboc2\bgi");
x=getmaxx()/2;
y=getmaxy()/2;
circle(x,y,50);
circle(x,y,100);
getch();
closegraph();
restorecrtmode();
}

2. Write a program to fill overlapping circles with different colors.


#include<stdio.h>
#include<conio.h>
#include<graphics.h>
main()
{
int gd=DETECT,gm,x,y;
initgraph(&gd,&gm,"c:\turboc2\bgi");
x=getmaxx()/2;
y=getmaxy()/2;
setcolor(RED);
circle(x,y,50);
setcolor(GREEN);
circle(x,y,100);
getch();
closegraph();
restorecrtmode();
}

3. Write a program to draw 10 rectangles having similar size one after other in one
vertical line.
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
main()
{
int gd=DETECT,gm,x,y;
initgraph(&gd,&gm,"c:\turboc2\bgi");
for(x=40;x<=220;)

ByProf.AshokN.Kamthane

Copyright2012DorlingKindersleyIndiaPvt.Ltd.

ProgramminginCISBN:9788131760314
{

rectangle(x,50,x+20,200);
x+=20;

}
getch();
closegraph();
restorecrtmode();

4. Draw alternately bars and rectangles one after other.


#include<stdio.h>
#include<conio.h>
#include<graphics.h>
main()
{
int gd=DETECT,gm,x,y,i;
initgraph(&gd,&gm,"c:\turboc2\bgi");

for(x=40,i=0;x<=220,i<=10;i++)
{
if(i%2==0)
rectangle(x,50,x+20,200);
else
bar(x,50,x+20,200);
x+=20;
}
getch();
closegraph();
restorecrtmode();

5. Draw the different shapes of rectangles and fill them with different colors
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"c:\turboc2\bgi");

setfillstyle(1,2);
bar(20,20,80,40);
setfillstyle(2,4);
bar(20,50,50,100);
setfillstyle(4,5);
bar(80,60,150,100);
getch();
closegraph();
restorecrtmode();

ByProf.AshokN.Kamthane

Copyright2012DorlingKindersleyIndiaPvt.Ltd.

ProgramminginCISBN:9788131760314

Chapter 16: Dyanamic Memory Allocation


and Linked Lists
1. Write a program to display first five letters of alphabets using malloc().
#include<conio.h>
#include<stdio.h>
#include<alloc.h>
void main()
{
char *p;
char i,a='A';
clrscr();
p=(char *)malloc(sizeof(char));
p=&a;
printf("The First five alphabes are : {");
for(i=0;i<5;i++,a++)
printf("'%c',",*p);
printf("\b}");
getch();
}

Output:
The First five alphabes are: {'A','B','C','D','E'}

2. Write a program to enter integers using malloc()and search for the position of
entered number.
#include<conio.h>
#include<stdio.h>
#include<alloc.h>
void main()
{
int n, *p;
int i,key,flag;
clrscr();
printf("Enter the number of elements to be entered.");
scanf("%d",&n);
p=(int*)malloc(n*sizeof(int));
for(i=0;i<n;i++){
printf("Enter the element #%d :",i+1);
scanf("%d",&p[i]);
}
printf("Enter the element to be searched. : ");
scanf("%d",&key);
flag=0;
for(i=0;i<n;i++){
if(p[i]==key){

ByProf.AshokN.Kamthane

Copyright2012DorlingKindersleyIndiaPvt.Ltd.

ProgramminginCISBN:9788131760314
printf("Element found at position %d",i+1);
flag=1;
break;
}
}
if(flag==0)
printf("Element not found.");
getch();
}

Output:
Enter the number of elements to be entered. 5
Enter the element #1: 43
Enter the element #2: 4
Enter the element #3: 332
Enter the element #4: 3
Enter the element #5: 565
Enter the element to be searched.: 4
Element found at position 2

3. Write a program to find maximum number amongst the float array and allocate the
float memory using calloc().
#include<conio.h>
#include<stdio.h>
#include<alloc.h>
void main()
{
float key,*p;
int i,n;
clrscr();
printf("Enter the number of elements to be entered.");
scanf("%d",&n);
p=(float*)calloc(n,sizeof(float));
for(i=0;i<n;i++){
printf("Enter the element #%d :",i+1);
scanf("%f",&p[i]);
}
key=p[0];
for(i=1;i<n;i++)
if(p[i]>key)key=p[i];
printf("The largest number is %f",key);
getch();
}

Output:
Enter the number of elements to be entered.5
Enter the element #1: 12
Enter the element #2: 34
Enter the element #3: 54
Enter the element #4: 3
Enter the element #5: 456
The largest number is 456.000000

ByProf.AshokN.Kamthane

Copyright2012DorlingKindersleyIndiaPvt.Ltd.

ProgramminginCISBN:9788131760314

4. Write a program a program to allocate initially memory with malloc()and then with
calloc()and check out the difference between them.
#include<conio.h>
#include<stdio.h>
#include<alloc.h>
void main()
{
int *p;
int n,i;
clrscr();
printf("Enter the number of elements of an array :");
scanf("%d",&n);
p=(int *)malloc(n*sizeof(int));
printf("The defult values of an array when memory is allocated using
malloc() are :\n");
for(i=0;i<n;i++)
printf("%d\n",p[i]);
printf("The defult values of an array when memory is allocated
using calloc() are :\n");
p=(int *)calloc(n,sizeof(int));
for(i=0;i<n;i++)
printf("%d\n",p[i]);
getch();
}

Output:
Enter the number of elements of an array :5
The default values of an array when memory is allocated using malloc()
are:
-10773
-31954
11070
30
6516
The default values of an array when memory is allocated using calloc()
are:
0
0
0
0
0

5. Write a program for singly linked list to display few elements and search in decreasing
order.
#include<conio.h>
#include<stdio.h>
#include<process.h>
struct node
{
int a;
struct node * next;
};

ByProf.AshokN.Kamthane

Copyright2012DorlingKindersleyIndiaPvt.Ltd.

ProgramminginCISBN:9788131760314
struct node * create()
{
return (struct node*)malloc(sizeof(struct node));
}
void main()
{
struct node *head,*first,*root;
int n,t;
char ch;
clrscr();
printf("Enter the number :");
scanf("%d",&n);
first=create();
first->a=n;
first->next=NULL;
head=first;
root=first;
printf("Do you want to add more nodes. :");
scanf(" %c",&ch);
while(ch=='y'||ch=='Y')
{
printf("Enter the number :");
scanf("%d",&n);
head->next=create();
head=head->next;
head->a=n;
head->next=NULL;
printf("Do you want to add more nodes. :");
scanf(" %c",&ch);
}
head=first;
printf("\n\nThe elements of the linked list are :");
while(first!=NULL)
{
printf("\n%d",first->a);
first=first->next;
}
first=head;
for(head;head!=NULL;head=head->next)
{
for(root=head->next;root!=NULL;root=root->next)
if(head->a<root->a)
{
t=head->a;
head->a=root->a;
root->a=t;
}
}
printf("\n\nThe elements of singly linked list in decreasing order are
:");
while(first!=NULL)
{
printf("\n%d",first->a);
first=first->next;
}

ByProf.AshokN.Kamthane

Copyright2012DorlingKindersleyIndiaPvt.Ltd.

ProgramminginCISBN:9788131760314
getch();
}

Output:
Enter the number:123
Do you want to add more
Enter the number:3434
Do you want to add more
Enter the number:343
Do you want to add more
Enter the number:5
Do you want to add more
Enter the number:3423
Do you want to add more

nodes.:y
nodes.:y
nodes.:y
nodes.:y
nodes.:n

The elements of the linked list are:


123
3434
343
5
3423
The elements of singly linked list in decreasing order are:
3434
3423
343
123
5

ByProf.AshokN.Kamthane

Copyright2012DorlingKindersleyIndiaPvt.Ltd.

You might also like