By Prof. Ashok N. Kamthane
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()
{
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:
By Prof. Ashok N. Kamthane
Enter a number (N):7 N mod 2=1
N mod 3=1 N mod 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=17. 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=C8. 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:
By Prof. Ashok N. Kamthane
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
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=11113. 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.
By Prof. Ashok N. Kamthane #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();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 from 0 to 9 with ASCII values: Numbers ASCII values
0 48 1 49 3 51 4 52 5 53 6 54 7 55 8 56 9 57
16. Write a program to evaluate the following expressions and display their results.
(a) x
2+(2*x
3)*(2*x)
(b) x
1+y
2+z
3assume 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
By Prof. Ashok N. Kamthane
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
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
By Prof. Ashok N. Kamthane #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=7204. 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
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. Square Cube
4 16 64 5 25 125 6 36 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.
By Prof. Ashok N. Kamthane
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();
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>
By Prof. Ashok N. Kamthane
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 gm13. 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()
{ 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 = 105014. 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>
By Prof. Ashok N. Kamthane
#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
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
By Prof. Ashok N. Kamthane
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
ifstatements 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
ifstatement.
#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:
Enter age of person to vote:45 Eligible.
3. Write a program to calculate bill of a job work done as follows. Use
if-elsestatement.
(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",©); 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;
By Prof. Ashok N. Kamthane
clrscr();
printf("Enter no. of copies: "); scanf("%d",©); 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
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.
By Prof. Ashok N. Kamthane
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
OZONE
OXYGEN
PETROL
IRON
ICE
GOLD
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: 68. 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:
By Prof. Ashok N. Kamthane
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 za2. 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(); }
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 second’s pointer is to be
shown with any symbol. Also print the total number of revolution made by second’s
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:
By Prof. Ashok N. Kamthane
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 + 10 = 11 2 + 9 = 11 3 + 8 = 11 4 + 7 = 11 5 + 6 = 115. 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++;
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 your vote:A Enter your vote:A Enter your vote:B Enter your vote:B Enter your vote:B Enter your vote:B Enter your vote:A Enter your vote:B Enter your vote:B Enter your vote:A
Total votes for 'A' = 4 Total 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(); }
By Prof. Ashok N. Kamthane
Output:
Vote to pass resolution (Y/N): Enter vote: Y Enter vote: N Enter vote: N Enter vote: Y Enter vote: Y Resolution is passed.
7. Assume that there are 99 voters voting to a person for selecting chairman’s
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
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++; }
By Prof. Ashok N. Kamthane } 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(); }
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();
By Prof. Ashok N. Kamthane
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 9913. Create an infinite for loop. Check each value of the
forloop. 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
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");By Prof. Ashok N. Kamthane x++; y++; } getch(); }
Output:
Z YZY XYZYX WXYZYXW XYZYX YZY Z15. 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 zero."); else if(num>0) printf("Number is positive."); else printf("Number is negative."); } getch(); }
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: -345Number 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 017. Is it possible to create a loop using the
gotostatement? 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++;
By Prof. Ashok N. Kamthane if(num<=10) goto label; getch(); }
Output:
1 2 3 4 5 6 7 8 9 1018. 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-whileloop.
#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-whileloop. 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 {
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 CHARACTERS1 ☺ Press 'Y' to contunue or 'N' to terminate:Y 2 ☻ Press 'Y' to contunue or 'N' to terminate:Y 3 ♥ Press 'Y' to contunue or 'N' to terminate:Y 4 ♦ Press 'Y' to contunue or 'N' to terminate:Y 5 ♣ Press 'Y' to contunue or 'N' to terminate:Y 6 ♠ Press 'Y' to contunue or 'N' to 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.
By Prof. Ashok N. Kamthane
5 < 7 so 74.
Enter two numbers: 8 6 8 > 6 so 728.
Enter two numbers: 7 2 7 > 2 so 351.
Enter two numbers: 6 3 6 > 3 so 243.
21. Evaluate the following series. Use
do-whileloop.
(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<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) {
By Prof. Ashok N. Kamthane 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-whileloop.
(a) a+1+b+2+c+3. . . .n, where n is an integer.
(b) z+y+x. . .+a.
(c) za+yb+xc. . .+az.
(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(); }By Prof. Ashok N. Kamthane
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-whileloop.
#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 {
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
By Prof. Ashok N. Kamthane
2 1
24. Enter text through the keyboard and display it in the reverse order. Use
do-whileloop.
#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:
ayirp25. 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("* "); }
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>
By Prof. Ashok N. Kamthane #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 528. Write a program to enter two integer values through the keyboard. Using
whileloop,
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:
By Prof. Ashok N. Kamthane 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>
#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(); }
By Prof. Ashok N. Kamthane
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:
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 the elements of the array
Enter number #1 : 23 Enter number #2 : 55 Enter number #3 : 3 Enter number #4 : 456 Enter number #5 : 7 Enter number #6 : 8989 Enter number #7 : 4 Enter number #8 : 356 Enter number #9 : 6 Enter number #10 : 4 The smallest and largest numbers are 3 and 8989 respectively.
By Prof. Ashok N. Kamthane
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);
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()
By Prof. Ashok N. Kamthane 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:
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.
By Prof. Ashok N. Kamthane #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);
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 s5 sum 756 88 67 87 66 65 373 98 67 98 87 76 45 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.
By Prof. Ashok N. Kamthane #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 the number of elements of the array: 5 Enter the elements of the array.
Enter element #1: 23 Enter element #2: 54 Enter element #3: 3 Enter element #4: 4 Enter 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>
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);
By Prof. Ashok N. Kamthane
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]); }
}
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 purchase :"); scanf("%d",&ch);
switch(ch) {
case 1:
printf("\nEnter the number of sets you want to purchase :"); scanf("%d",&n);
if(n>10) {
cost = p[0]*n;
printf("\nTotal cost of %d item set is %f\nTotal cost after 15%% discount is %f",n,cost,cost*15/100.0);
} else {
cost = p[0]*n;
printf("\nTotal cost of %d item set is %f\nTotal cost after 10%% discount is %f",n,cost,cost-cost/10.0);
By Prof. Ashok N. Kamthane
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 :