|
|
[H] Write C programs for the following:
(a) Ramesh’s basic salary is input through the keyboard. His dearness allowance is 40% of basic salary, and house rent allowance is 20% of basic salary. Write a program to calculate his gross salary.
#include<conio.h>
#include<stdio.h>
main()
{
int bs; // bs=Basic Salary
float a1,a2,gs; // a1= Dearness Allowance, a2= House rent Allowance, gs=Gross Salary
clrscr();
printf("Enter Ramesh's Basic Salary : ");
scanf("%d",&bs);
a1=bs*0.4;
a2=bs*0.2;
gs=bs-a1-a2;
printf("Gross Salary of Ramesh is %f",gs);
getch();
}
#include<stdio.h>
main()
{
int bs; // bs=Basic Salary
float a1,a2,gs; // a1= Dearness Allowance, a2= House rent Allowance, gs=Gross Salary
clrscr();
printf("Enter Ramesh's Basic Salary : ");
scanf("%d",&bs);
a1=bs*0.4;
a2=bs*0.2;
gs=bs-a1-a2;
printf("Gross Salary of Ramesh is %f",gs);
getch();
}
(b) The distance between two cities (in km.) is input through the keyboard. Write a program to convert and print this distance in meters, feet, inches and centimeters.
#include<stdio.h>
#include<conio.h>
main()
{
float dkm,dm,df,di,dc;
/* dkm=kilometer distance,dm=meter distance,df=feet distance,di=inch distance,dc=centimeter
distance */
clrscr();
printf("Enter distance between two cities : ");
scanf("%f",&dkm);
dm=dkm*1000; // 1km=1000 meters
df=dkm*3280.8399; // 1km=3280.8399 feets
di=dkm*39370.078; // 1km=39370.078 inches
dc=dkm*100000; // 1km=100000 meters
printf("\nDistance in\nMeters = %f\nFeets = %f\nInches = %f\nCentimeters =
%f",dm,df,di,dc);
getch();
}
#include<conio.h>
main()
{
float dkm,dm,df,di,dc;
/* dkm=kilometer distance,dm=meter distance,df=feet distance,di=inch distance,dc=centimeter
distance */
clrscr();
printf("Enter distance between two cities : ");
scanf("%f",&dkm);
dm=dkm*1000; // 1km=1000 meters
df=dkm*3280.8399; // 1km=3280.8399 feets
di=dkm*39370.078; // 1km=39370.078 inches
dc=dkm*100000; // 1km=100000 meters
printf("\nDistance in\nMeters = %f\nFeets = %f\nInches = %f\nCentimeters =
%f",dm,df,di,dc);
getch();
}
(c) If the marks obtained by a student in five different subjects are input through the keyboard, find out the aggregate marks and percentage marks obtained by the student. Assume that the maximum marks that can be obtained by a student in each subject is 100.
#include<stdio.h>
#include<conio.h>
main()
{
int m1,m2,m3,m4,m5,agg;
float per; // m1=Marks of 1st subjects,agg=Aggregate marks,per=Percentage
clrscr();
printf("Input marks of 5 subjects obtained by student:\n"); // "\n" is used for new line
scanf("%d%d%d%d%d",&m1,&m2,&m3,&m4,&m5);
agg=m1+m2+m3+m4+m5;
per=agg*(100.0/500.0);
printf("Aggregate marks obtained by student = %d\n",agg);
printf("Percentage of student = %f%",per);
getch();
}
#include<conio.h>
main()
{
int m1,m2,m3,m4,m5,agg;
float per; // m1=Marks of 1st subjects,agg=Aggregate marks,per=Percentage
clrscr();
printf("Input marks of 5 subjects obtained by student:\n"); // "\n" is used for new line
scanf("%d%d%d%d%d",&m1,&m2,&m3,&m4,&m5);
agg=m1+m2+m3+m4+m5;
per=agg*(100.0/500.0);
printf("Aggregate marks obtained by student = %d\n",agg);
printf("Percentage of student = %f%",per);
getch();
}
(d) Temperature of a city in Fahrenheit degrees is input through the keyboard. Write a program to convert this temperature into Centigrade degrees.
#include<stdio.h>
#include<conio.h>
main()
{
float ftemp, ctemp; //ftemp=Fahrenheit Temperature,ctemp=Centigrade Temperature
clrscr();
printf("Enter temperature of a city in Fahrenheit : ");
scanf("%f", &ftemp);
ctemp=(5.0/9.0)*(ftemp-32);
printf("The given temperature in Centigrade is %f",ctemp);
getch();}
#include<conio.h>
main()
{
float ftemp, ctemp; //ftemp=Fahrenheit Temperature,ctemp=Centigrade Temperature
clrscr();
printf("Enter temperature of a city in Fahrenheit : ");
scanf("%f", &ftemp);
ctemp=(5.0/9.0)*(ftemp-32);
printf("The given temperature in Centigrade is %f",ctemp);
getch();}
(e) The length & breadth of a rectangle and radius of a circle are input through the keyboard. Write a program to calculate the area & perimeter of the rectangle, and the area & circumference of the circle.
#include<stdio.h>
#include<conio.h>
#define pi 3.14
main()
{
float rl,rb,ra,rp,cr,ca,cc;
/* rl=Rectangle Length,rb=Rectange Breadth,ra=Rectange Area,rp=Rectangle Perimeter*/
/* cr=Circle Radius,ca=Circle Area,cc=Circle Circumference*/
clrscr();
printf("Enter Length of Rectangle:");
scanf("%f",&rl);
printf("Enter Breadth of Rectangle:");
scanf("%f",&rb);
printf("Enter Radius of Cirlce:");
scanf("%f",&cr);
ra=rl*rb;
rp=2*(rl+rb);
ca=pi*cr*cr;
cc=2*pi*cr;
printf("\nArea of Rectangle = %f\n", ra);
printf("Perimeter of Rectangle = %f\n", rp);
printf("Area of Cirlce = %f\n", ca);
printf("Circumference of Circle = %f\n", cc);
getch();
}
#include<conio.h>
#define pi 3.14
main()
{
float rl,rb,ra,rp,cr,ca,cc;
/* rl=Rectangle Length,rb=Rectange Breadth,ra=Rectange Area,rp=Rectangle Perimeter*/
/* cr=Circle Radius,ca=Circle Area,cc=Circle Circumference*/
clrscr();
printf("Enter Length of Rectangle:");
scanf("%f",&rl);
printf("Enter Breadth of Rectangle:");
scanf("%f",&rb);
printf("Enter Radius of Cirlce:");
scanf("%f",&cr);
ra=rl*rb;
rp=2*(rl+rb);
ca=pi*cr*cr;
cc=2*pi*cr;
printf("\nArea of Rectangle = %f\n", ra);
printf("Perimeter of Rectangle = %f\n", rp);
printf("Area of Cirlce = %f\n", ca);
printf("Circumference of Circle = %f\n", cc);
getch();
}
(f) Two numbers are input through the keyboard into two locations C and D. Write a program to interchange the contents of C and D.
#include<stdio.h>
#include<conio.h>
main()
{
int C,D;
clrscr();
printf("Enter value of C : ");
scanf("%d",&C);
printf("Enter value of D : ");
scanf("%d",&D);
printf("C = %d\nD = %d",D,C);
getch();
}
#include<conio.h>
main()
{
int C,D;
clrscr();
printf("Enter value of C : ");
scanf("%d",&C);
printf("Enter value of D : ");
scanf("%d",&D);
printf("C = %d\nD = %d",D,C);
getch();
}
(g) If a five-digit number is input through the keyboard, write a program to calculate the sum of its digits.
(Hint: Use the modulus operator ‘%’)
#include<stdio.h>
#include<conio.h>
main()
{
int num,sum,a,b,c,d,e,d1,d2,d3,d4,d5;
/*num=entered number,sum=sum of all digits,d1=1st digit,d2=2nd digit,d3=3rd digit,d4=4th digit,d5=5th digit*/
clrscr();
printf("Enter any five digit number : ");
scanf("%d",&num);
a=num/10;
d5=num%10;
b=a/10;
d4=a%10;
c=b/10;
d3=b%10;
d=c/10;
d2=c%10;
e=d/10;
d1=d%10;
sum=d1+d2+d3+d4+d5;
printf("\nSum of digits of given number is %d",sum);
getch();
}
(h) If a five-digit number is input through the keyboard, write a program to reverse the number
#include<stdio.h>
#include<conio.h>
main()
{
int num,a,b,c,d,e,d1,d2,d3,d4,d5;
//num=entered number,d1=1st digit,d2=2nd digit,d3=3rd digit,d4=4th digit,d5=5th digit
clrscr();
printf("Enter any five digit number : ");
scanf("%d",&num);
a=num/10;
d5=num%10;
b=a/10;
d4=a%10;
c=b/10;
d3=b%10;
d=c/10;
d2=c%10;
e=d/10;
d1=d%10;
printf("\nReverse number = %d%d%d%d%d",d5,d4,d3,d2,d1);
getch();
}
#include<conio.h>
main()
{
int num,a,b,c,d,e,d1,d2,d3,d4,d5;
//num=entered number,d1=1st digit,d2=2nd digit,d3=3rd digit,d4=4th digit,d5=5th digit
clrscr();
printf("Enter any five digit number : ");
scanf("%d",&num);
a=num/10;
d5=num%10;
b=a/10;
d4=a%10;
c=b/10;
d3=b%10;
d=c/10;
d2=c%10;
e=d/10;
d1=d%10;
printf("\nReverse number = %d%d%d%d%d",d5,d4,d3,d2,d1);
getch();
}
(i) If a four-digit number is input through the keyboard, write a program to obtain the sum of the first and last digit of this number.
#include<stdio.h>
#include<conio.h>
main()
{
int num,sum,a,b,c,d,d1,d2,d3,d4;
/* num=entered number,sum=sum of 1st & last digit,d1=1st digit,d2=2nd digit,d3=3rd digit,d4=4th digit*/
clrscr();
printf("Enter any four digit number : ");
scanf("%d",&num);
a=num/10;
d4=num%10;
b=a/10;
d3=a%10;
c=b/10;
d2=b%10;
d=c/10;
d1=c%10;
sum=d1+d4;
printf("Sum of 1st and 4th digit = %d",sum);
getch();
}
#include<conio.h>
main()
{
int num,sum,a,b,c,d,d1,d2,d3,d4;
/* num=entered number,sum=sum of 1st & last digit,d1=1st digit,d2=2nd digit,d3=3rd digit,d4=4th digit*/
clrscr();
printf("Enter any four digit number : ");
scanf("%d",&num);
a=num/10;
d4=num%10;
b=a/10;
d3=a%10;
c=b/10;
d2=b%10;
d=c/10;
d1=c%10;
sum=d1+d4;
printf("Sum of 1st and 4th digit = %d",sum);
getch();
}
(j) In a town, the percentage of men is 52. The percentage of total literacy is 48. If total percentage of literate men is 35 of the total population, write a program to find the total number of illiterate men and women if the population of the town is 80,000.
#include<stdio.h>
#include<conio.h>
main()
{
long double mp,wp,tlp,mlp,im,iw,timw;
/*mp=men %,tlp=Total literacy %,mlp=man literacy %,wp=women %,
im=no of illeterate men,iw=no of illeterate women*/
long int pop=80000;
//pop=Total population
clrscr();
mp=pop*(52.0/100.0);
wp=pop-mp;
tlp=pop*(48.0/100.0);
mlp=pop*(35.0/100.0);
im=mp-mlp;
iw=wp-(tlp-mlp);
printf("%.1Lf %.1Lf",im,iw);
getch();
}
#include<conio.h>
main()
{
long double mp,wp,tlp,mlp,im,iw,timw;
/*mp=men %,tlp=Total literacy %,mlp=man literacy %,wp=women %,
im=no of illeterate men,iw=no of illeterate women*/
long int pop=80000;
//pop=Total population
clrscr();
mp=pop*(52.0/100.0);
wp=pop-mp;
tlp=pop*(48.0/100.0);
mlp=pop*(35.0/100.0);
im=mp-mlp;
iw=wp-(tlp-mlp);
printf("%.1Lf %.1Lf",im,iw);
getch();
}
(k) A cashier has currency notes of denominations 10, 50 and 100. If the amount to be withdrawn is input through the keyboard in hundreds, find the total number of currency notes of each denomination the cashier will have to give to the withdrawer.
#include<stdio.h>
#include<conio.h>
main()
{
int amt,ten,fif,hun;
/* amt=amount,ten=currency notes of ten,
fif=currency notes of fifty,hun=current notes of hundred*/
clrscr();
printf("Enter amount to be withdrawn : ");
scanf("%d",&amt);
ten=amt/10;
fif=amt/50;
hun=amt/100;
printf("The cashier will give you\n%d Ten notes\nOR\n%d Fifty notes\nOR\n%d Hundred notes",ten,fif,hun);
getch();
}
#include<conio.h>
main()
{
int amt,ten,fif,hun;
/* amt=amount,ten=currency notes of ten,
fif=currency notes of fifty,hun=current notes of hundred*/
clrscr();
printf("Enter amount to be withdrawn : ");
scanf("%d",&amt);
ten=amt/10;
fif=amt/50;
hun=amt/100;
printf("The cashier will give you\n%d Ten notes\nOR\n%d Fifty notes\nOR\n%d Hundred notes",ten,fif,hun);
getch();
}
(l) If the total selling price of 15 items and the total profit earned on them is input through the keyboard, write a program to find the cost price of one item.
#include<stdio.h>
#include<conio.h>
main()
{
int sp,tp,cp,cp1;
/*sp=selling price of 15 items,tp=total profit,
cp=cost price of 1 item,cp1=cost price of 1 item*/
clrscr();
printf("Enter total selling price of 15 items : ");
scanf("%d",&sp);
printf("Enter profit on selling 15 items : ");
scanf("%d",&tp);
cp=sp-tp;
cp1=cp/15;
printf("Cost price of one item is %d",cp1);
getch();
}
#include<conio.h>
main()
{
int sp,tp,cp,cp1;
/*sp=selling price of 15 items,tp=total profit,
cp=cost price of 1 item,cp1=cost price of 1 item*/
clrscr();
printf("Enter total selling price of 15 items : ");
scanf("%d",&sp);
printf("Enter profit on selling 15 items : ");
scanf("%d",&tp);
cp=sp-tp;
cp1=cp/15;
printf("Cost price of one item is %d",cp1);
getch();
}
(m) If a five-digit number is input through the keyboard, write a program to print a new number by adding one to each of its digits. For example if the number that is input is 12391 then the output should be displayed as 23502.
#include<stdio.h>
#include<conio.h>
main()
{
int num,res;
//num=entered number,res=result
clrscr();
printf("Enter any five digit number : ");
scanf("%d",&num);
res=num+11111;
printf("Output is %d",res);
getch();
}
#include<conio.h>
main()
{
int num,res;
//num=entered number,res=result
clrscr();
printf("Enter any five digit number : ");
scanf("%d",&num);
res=num+11111;
printf("Output is %d",res);
getch();
}