Saturday, January 30, 2010

CHAP 1[H]m Incrementing by 1 for every digit of a number

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 23402.

void main()
{
float a,b,c,d,e,t,s;
int a1,b1,c1,d1,e1,a2,b2,c2,d2,e2;
printf("\nInput a 5 digit number");
scanf("%f",&t);

a=t/10000.0;
a1=a;
b=(t-(a1*10000.0))/1000;
b1=b;
c=(t-(a1*10000.0)-(b1*1000))/100;
c1=c;
d=(t-(a1*10000.0)-(b1*1000)-(c1*100))/10;
d1=d;
e=(t-(a1*10000.0)-(b1*1000)-(c1*100)-(d1*10))/1;
e1=e;


a2=a1+1;
b2=b1+1;
c2=c1+1;
d2=d1+1;
e2=e1+1;

if (a2==10)
a2=0;
if (b2==10)
b2=0;
if (c2==10)
c2=0;
if (d2==10)
d2=0;
if (e2==10)
e2=0;


s=(a2*10000.0)+(b2*1000.0)+(c2*100.0)+(d2*10.0)+(e2);


printf("\nThe required result is %f",s);
}

CHAP 1[H]L Finding cost price

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.

void main()
{
float sp,tp,cp,cps;
printf("\nInput the total selling price of 15 itmes and total profit earned respectively");
scanf("%f%f",&sp,&tp);
cps=sp-tp;
printf("\nThe cost price of one item is %f",cp=cps/15.0);

}

CHAP 1[H]k Currency denomination problem

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.

Note: So if its $900 the input value is 9 and if its $1970 input is 19.7

void main()
{
float t,_10,_50,_100;
int _11,_51,_101;
printf("\nInput notes in hundreds");
scanf("%f",&t);

_100=t/100;
_101=_100;
_50=(t-_101*100.0)/50;
_51=_50;
_10=(t-(_101*100.0+_51*50.0))/10;
_11=_10;
printf("\n_100 is %f\n_101 is %d",_100,_101);
printf("\nThe no of $100 notes is %d\nThe no of $50 notes is %d\nThe no of $10 notes is %d",_101,_51,_11);
}

CHAP 1[H]i Sum of first and last digit

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.

void main()
{
float a,d,t;
int a1,d1,t1,u;
printf("\nInput a 4 digit number");
scanf("%f",&t);
a=t/1000.0;
u=t;
a1=a;
d=u%10;
d1=d;

t1=a1+d1;

printf("\nThe Sum f %d",t1);
}

CHAP 1[H]h Reversing number

If a five-digit number is input through the keyboard, write a program to reverse the number.

void main()
{
float a,b,c,d,e,t,a2,b2,c2,d2,e2,s;
int a1,b1,c1,d1,e1;
printf("\nInput a 5 digit number");
scanf("%f",&t);
a=t/10000.0;
a1=a;
b=(t-(a1*10000.0))/1000;
b1=b;
c=(t-(a1*10000.0)-(b1*1000))/100;
c1=c;
d=(t-(a1*10000.0)-(b1*1000)-(c1*100))/10;
d1=d;
e=t-(a1*10000.0)-(b1*1000)-(c1*100)-(d1*10);
e1=e;

a2=e1*10000.0;
b2=d1*1000.0;
c2=c1*100.0;
d2=b1*10.0;
e2=a1*1.0;

s=a2+b2+c2+d2+e2;

printf("\nThe required result is %f",s);

}

CHAP 1[H]g Caculating sum of digits

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 ‘%’)


void main()
{
float number,a,b,c,d,e;
int a1,b1,c1,d1,e1,t;
printf("\nKey in a five digit number");
scanf("%f",&number);

a=number/10000.0;
a1=a/1;

b=(number-(a1*10000.0))/1000;
b1=b/1;

c=(number-(a1*10000.0)-(b1*1000))/100;
c1=c/1;

d=(number-(a1*10000.0)-(b1*1000)-(c1*100))/10;
d1=d/1;

e=(number-(a1*10000.0)-(b1*1000)-(c1*100)-(d1*10))/1;
e1=e/1;

t=a1+b1+c1+d1+e1;
printf("\nThe sum of the five digits is %d",t);

}

CHAP 1[H]f Interchanging values

Two numbers are input through the keyboard into two locations C and D. Write a program to interchange the contents of C and D.

void main()
{
float c,d,e,f;
printf("\nInput values of C and D");
scanf("%f%f",&c,&d);
e=c;
f=d;
c=f;
d=e;
printf("\nNow C is %f\nD is %f",c,d);
}

CHAP 1[H]e Finding area , perimeter of circle and rectangle

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.

void main()
{
float l,b,r,arear,perimeterr,areac,perimeterc;
printf("\nInput the length and breadth of rectangle and radius of circle respectively");
scanf("%f%f%f",&l,&b,&r);
arear=l*b;
perimeterr=2*l+2*b;
areac=3.141592654*r*r;
perimeterc=2*3.141592654*r;
printf("\nThe area of rectangle is %f\nThe perimeter of rectangle is %f\nThe area of circle is %f\nThe circumference of the circle is %f",arear,perimeterr,areac,perimeterc);

}

CHAP 1[H]d Converting Fahrenheit to Centigrade degrees

Temperature of a city in Fahrenheit degrees is input through the keyboard. Write a program to convert this temperature into Centigrade degrees.

void main()
{
float c,f;
printf("\nInput the temperate(Fahrenheit)");
scanf("%f",&f);
c= (f-32)*(100.0/180.0);
printf("\nThe temperature in Celsius is %f",c);
}

CHAP 1[H]c Finding aggregate and percentage marks

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.

void main()
{
float a,b,c,d,e,t,am,pm;
printf("\nInput the 5 test marks followed by total obtainable marks");
scanf("%f%f%f%f%f%f",&a,&b,&c,&d,&e,&t);
am=(a+b+c+d+e)/5;
pm=(am/t)*100.0;
printf("\nThe aggregate marks is %f\nThe percentage marks is %f",am,pm);
}

CHAP 1[H]b Converting distance in km to m,inches,cm

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.

void main()
{
float distance,distancem,distancefeet,distanceinches,distancecm;
printf("\nInput distance in km");
scanf("%f",&distance);
distancem= distance*1000.0;
distancefeet= distancem*3.2808;
distanceinches= (distance*100000.0)/2.54;
distancecm = distance*100000.0;
printf("\n Distance in metres = %fm\n Distance in feet = %fft\n Distance in inches = %finches\n Distance in cm = %fcm",distancem,distancefeet,distanceinches,distancecm);
}

CHAP 1[H]a Calculating gross salary

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.

void main()
{
float bs,gs;
printf("\nInput Basic Salary");
scanf("%f",&bs);
gs=bs*(16.0/10.0);
printf("\nGross Salary is %f",gs);
}

CHAP 2[C]L Checking if coordinate lies on,x,y axis or on origin

Given a point (x, y), write a program to find out if it lies on the x-axis, y-axis or at the origin, viz. (0, 0).

void main()
{
float x,y;
printf("\nInput the(x,y) values");
scanf("%f%f",&x,&y);
if(x==0&&y==0)
printf("\nThe point lies on the origin");
else if(x==0&&y!=0)
printf("\nThe point lies on the y-axis");
else if(x!=0&&y==0)
printf("\nThe point lies on the x-axis");
else
printf("\nThe point does not lie on any axis or origin");
}

CHAP 2[C]j Checking if three coordinates form a straight line

Given three points (x1, y1), (x2, y2) and (x3, y3), write a program to check if all the three points fall on one straight line.

void main()
{
float x1,x2,x3,y1,y2,y3;
printf("\nEnter the three coordinates (x1,y1),(x2,y2),(x3,y3)");
scanf("%f%f%f%f%f%f",&x1,&y1,&x2,&y2,&x3,&y3);
if ((y2-y1)/(x2-x1)==(y3-y2)/(x3-x2))
printf("\nThe three points fall on the same line");
else
printf("\nThe three points are not collinear");
}

CHAP 2[C]i Checking if area is bigger than perimeter of rectangle

Given the length and breadth of a rectangle, write a program to find whether the area of the rectangle is greater than its perimeter. For example, the area of the rectangle with length = 5 and breadth = 4 is greater than its perimeter.

void main()
{
float l,b,a,p;
printf("Input length and breadth of rectangle");
scanf("%f%f",&l,&b);
a=l*b;
p=2.0*l+2.0*b;
(a>p?printf("\nArea is larger than perimeter"):printf("\nArea is smaller than perimeter"));
}

CHAP 2[C]g Checking for valid triangle(using sum of angles)

Write a program to check whether a triangle is valid or not, when the three angles of the triangle are entered through the keyboard. A triangle is valid if the sum of all the three angles is equal to 180 degrees.

void main()
{
while (1)
{

float a,b,c;
int s;

printf("\nInput the number of sides the figure has");
scanf("%d",&s);

if(s==3)
printf("\nThe figure is 3 sided");
else
{
printf("\nThe figure is not 3 sided");
break;
}

printf("\nInput the three angles of the triangle");
scanf("%f%f%f",&a,&b,&c);
if(a+b+c==180)
printf("\nThe figure is a triangle");
else
printf("\nThe figure is not a triangle");
break;
}
}

CHAP 2[C]f Determining youngest age

If the ages of Ram, Shyam and Ajay are input through the keyboard, write a program to determine the youngest of the three.

void main()
{
int r,s,a,small;
printf("\nInput the ages of Ram,Shyam and Ajay");
scanf("%d%d%d",&r,&s,&a);
small=(r>s?(s>a?a:s):(r>a?a:r));
if(small==r)
printf("\nThe youngest is Ram with age %d",r);
if(small==s)
printf("\nThe youngest is Shyam with age %d",s);
if(small==a)

printf("\nThe youngest is Ajay with age %d",a);

}

CHAP 2[C]e Reverse the number input

A five-digit number is entered through the keyboard. Write a program to obtain the reversed number and to determine whether the original and reversed numbers are equal or not.

void main()
{
float number,a,b,c,d,e,a2,b2,c2,d2,e2,t;
int a1,b1,c1,d1,e1;
printf("\nInput a 5 digit number");
scanf("%f",&number);
a=number/10000;
a1=a;
b=(number-(10000.0*a1))/1000;
b1=b;
c=(number-(10000.0*a1)-(1000*b1))/100;
c1=c;
d=(number-(10000.0*a1)-(1000*b1)-(100*c1))/10;
d1=d;
e=(number-(10000.0*a1)-(1000*b1)-(100*c1)-(10*d1));
e1=e;

a2=e1*10000.0;
b2=d1*1000.0;
c2=c1*100.0;
d2=b1*10.0;
e2=a1*1.0;

t=a2+b2+c2+d2+e2;

printf("\nThe reversed number is %f",t);
if(t!=number)
printf("\nThe original number and reversed number are not equal");
}

CHAP 2[C]d Finding day on first of Jan on a particular year

According to the Gregorian calendar, it was Monday on the date 01/01/1900. If any year is input through the keyboard write a program to find out what is the day on 1st January of this year.

void main()
{
float days;
int year,diff,leap,type;
long int days1;
printf("\nInput the year");
scanf("%d",&year);
year=year-1;
diff=year-1900;

/* The line year=year-1 was written because we are finding the days before
that particular year not that full year as the required date is 01/01/year.

In days... 365 was added because the year 1900 has to be taken into account
as 1900-1904 is not 1904-1900=4years but is 5 years. 1900 is not a leap year.

In days... addition of one was added because the first day of the year
(1st of Jan) has to be accounted for*/

if(diff<100)
{
leap =diff/4;
days=(366.0*leap)+((diff-leap)*365+365+1);
days1=days;
type=days1%7;
}

if(diff>=100)
{
leap = (diff/4)-(diff/100)+1+((year-2000)/400);
days=(366.0*leap)+((diff-leap)*365+365+1);
days1=days;
type=days1%7;
}

if(type==0)
printf("Sunday");
if(type==1)
printf("Monday");
if(type==2)
printf("Tuesday");
if(type==3)
printf("Wednesday");
if(type==4)
printf("Thursday");
if(type==5)
printf("Friday");
if(type==6)
printf("Saturday");

printf("\nThe leap is %d\nThe days is %f\nThe type is %d",leap,days,type);

}

CHAP 2[C]c Checking for leap year

Any year is input through the keyboard. Write a program to determine whether the year is a leap year or not.
(Hint: Use the % (modulus) operator)


void main()
{
int year;
printf("\nInput the year");
scanf("%d",&year);

if((year%4==0&&year%100!=0)||year%400==0)

printf("\nThe Year is a leap year");

else
printf("\nThe Year is not a leap year");
}

CHAP 2[C]b Checking for odd or even number

Any integer is input through the keyboard. Write a program to find out whether it is an odd number or even number.

void main()
{
float b;
int a;
printf("\nInput an Integer");
scanf("%d",&a);
b=a%2;
if(b==0)
printf("\nInteger is an even number");
else
printf("\nInteger is an odd number");
}

CHAP 2[C]a Determine profit from cost and sell price

If cost price and selling price of an item is input through the keyboard, write a program to determine whether the seller has made profit or incurred loss. Also determine how much profit he made or loss he incurred.

void main()
{
float cp,sp,p;
printf("\nInput the cost price and selling price of an item");
scanf("%f%f",&cp,&sp);
p=sp-cp;
if(p>0)
printf("\nSeller has made a profit of %f",p);
else if(p<0)
printf("\nSeller has made a loss of %f",p=0-p);
else
printf("\nSeller has neither profit nor suffer a loss");
}

CHAP 2[F]j Checking customers order

The policy followed by a company to process customer orders is given by the following rules:
(a) If a customer order is less than or equal to that in stock and has credit is OK, supply has requirement.
(b) If has credit is not OK do not supply. Send him intimation.
(c) If has credit is Ok but the item in stock is less than has order, supply what is in stock. Intimate to him data the balance will be shipped.
Write a C program to implement the company policy.



void main()
{
int order,stock,credit;
printf("\nInput order,stock,credit(1 or0)");
scanf("%d%d%d",&order,&stock,&credit);

if(credit==1)
{
if(order<=stock)
printf("\nSupply");

else
printf("\nNot Enough");
}

else
printf("\nCredit not ok");
}

CHAP 2[F]i Qualifying for degree problem

A university has the following rules for a student to qualify for a degree with A as the main subject and B as the subsidiary subject:
(a) He should get 55 percent or more in A and 45 percent or more in B.
(b) If he gets than 55 percent in A he should get 55 percent or more in B. However, he should get at least 45 percent in A.
(c) If he gets less than 45 percent in B and 65 percent or more in A he is allowed to reappear in an examination in B to qualify.
(d) In all other cases he is declared to have failed.
Write a program to receive marks in A and B and Output whether the student has passed, failed or is allowed to reappear in B.



void main()
{
float a,b;
printf("\nInput the marks for A & B");
scanf("%f%f",&a,&b);

if(a>=55&&b>=45)
printf("\nPassed");

else if ((a<55&&a>=45)&&b>=55)
printf("\nPassed");

else if (a>=65&&b<45)
printf("\nReattempt examination B");

else
printf("\nFail");

}

CHAP 2[F]h Checking efficiency of worker

In a company, worker efficiency is determined on the basis of the time required for a worker to complete a particular job. If the time taken by the worker is between 2 – 3 hours, then the worker is said to be highly efficient. If the time required by the worker is between 3 – 4 hours, then the worker is ordered to improve speed. If the time taken is between 4 – 5 hours, the worker is given training to improve his speed, and if the time taken by the worker is more than 5 hours, then the worker has to leave the company. If the time taken by the worker is input through the keyboard, find the efficiency of the worker.

void main()
{
float t;
printf("\nInput the time taken by worker");
scanf("%f",&t);

if(t>=2&&t<3)
printf("\nThe worker is highly efficient");

else if(t>=3&&t<4)
printf("\nThe worker is ordered to improve speed");

else if(t>=4&&t<=5)
printf("\nThe worker is given training to improve speed");

else if(t>5)
printf("\nThe worker is asked to leave the company");
}

CHAP 2[F]f Checking whether triangle is valid

If the three sides of a triangle are entered through the keyboard, write a program to check whether the triangle is valid or not. The triangle is valid if the sum of two sides is greater than the largest of the three sides.

void main()
{
float a,b,c,biggest;
printf("\nEnter the length of 3 sides of the triangle");
scanf("%f%f%f",&a,&b,&c);

biggest=(a>b?(a>c?a:c):(b>c?b:c));

if (biggest==a)
{
if(b+c>a)
printf("\nThe triangle is valid");
else
printf("\nThe triangle is invalid");
}

else if (biggest==b)
{
if(a+c>b)
printf("\nThe triangle is valid");
else
printf("\nThe triangle is invalid");
}

else
{
if(a+b>c)
printf("\nThe triangle is valid");
else
printf("\nThe triangle is invalid");
}
}

CHAP 2[F]e Library fine

A library charges a fine for every book returned late. For first 5 days the fine is 50 paise, for 6-10 days fine is one rupee and above 10 days fine is 5 rupees. If you return the book after 30 days your membership will be cancelled. Write a program to accept the number of days the member is late to return the book and display the fine or the appropriate message.


void main()
{
float fine;
int d;
printf("\nInput the number of days overdue");
scanf("%d",&d);

if(d<=5)
printf("\nThe fine is %f",fine=1.0*d);

else if(d>5&&d<=10)
printf("\nThe fine is%f",fine=(1.0*5)+(2.0*(d-5)));

else if(d>10&&d<30)
printf("\nThe fine is %f",fine=(1.0*5)+(2*5)+3*(d-10));

else if(d>=30)
printf("\nMembership cancelled and a fine of %f imposed",fine=(1.0*5)+(2.0*5)+3*(d-10));
}

CHAP 2[F]d Grade of steel

A certain grade of steel is graded according to the following conditions:
(i) Hardness must be greater than 50
(ii) Carbon content must be less than 0.7
(iii) Tensile strength must be greater than 5600
The grades are as follows:
Grade is 10 if all three conditions are met
Grade is 9 if conditions (i) and (ii) are met
Grade is 8 if conditions (ii) and (iii) are met
Grade is 7 if conditions (i) and (iii) are met
Grade is 6 if only one condition is met
Grade is 5 if none of the conditions are met
Write a program, which will require the user to give values of hardness, carbon content and tensile strength of the steel under consideration and output the grade of the steel.



void main()
{
float h,c,t;
printf("\nInput the hardness,carbon content and tensile strength respectively");
scanf("%f%f%f",&h,&c,&t);

if(h>50&&c<0.7&&t>5600)
printf("\nGrade10");

else if(h>50&&c<0.7&&t<5600)
printf("\nGrade 9");

else if(c<0.7&&t>5600&&h<=50)
printf("\nGrade 8");

else if(h>50&&t>5600&&c>=0.7)
printf("\nGrade 7");

else if(h>50||t>5600||c<0.7)
printf("\nGrade 6");

else
printf("\nGrade 5");
}

CHAP 2[F]b Detecting capital,small letter ,digit or symbol

Any character is entered through the keyboard, write a program to determine whether the character entered is a capital letter, a small case letter, a digit or a special symbol.
The following table shows the range of ASCII values for various characters.










void main()
{
char a;
printf("\nEnter any character\n");
scanf("%c",&a);

if(a>=65&&a<=90)
printf("\nThe character is a capital letter");

else if(a>=97&&a<=122)
printf("\nThe character is a small case letter");

else if(a>=48&&a<=57)
printf("\nThe character is a number");

else
printf("\nThe character is a special symbol");
}

CHAP 3[E](m) Approximating natural log

The natural logarithm can be approximated by the following series.






If x is input through the keyboard, write a program to calculate the sum of first seven terms of this series.


void main()
{
float v,w,x,y,z;
v=0;
w=1;
printf("\nInput the value of x for which the it takes on the natural log");
scanf("%f",&x);
y=(x-1)/x;

for(z=1;z<=7;z++)
{
w*=y;
v+=0.5*w;
}

printf("\nThe sum of the first seven terms is %f",v+0.5*y);
}

CHAP 3[E]L Calculating total amount after interest

When interest compounds q times per year at an annual rate of r % for n years, the principle p compounds to an amount a as per the following formula







Write a program to read 10 sets of p, r, n & q and calculate the corresponding as.


void main()
{
float a,p,r,q,n,x,y,z,w;


a=1;

for(z=1;z<=2;z++)
{
printf("\nEnter the values of p,r,n &q");
scanf("%f%f%f%f",&p,&r,&n,&q);
x=n*q;

for(y=1,a=1;y<=x;y++)
{
a=a*(1+r/q);
}

w=a*p;
printf("\nThe value of a is %f",w);
}

}

CHAP 3[E]k Finding optimum life of machine

A machine is purchased which will produce earning of Rs. 1000 per year while it lasts. The machine costs Rs. 6000 and will have a salvage of Rs. 2000 when it is condemned. If 12 percent per annum can be earned on alternate investments what would be the minimum life of the machine to make it a more attractive investment compared to alternative investment?

void main()
{
int y=0,pm,pa;

while(pa>pm)
{
y+=1;
pa=120*y;
pm=(1000*y)-4000;

}

printf("The minimum year is %d",y);
}

CHAP 3[E]i Print a number triangle pattern

Write a program to produce the following output:











void main()
{
int storec=0,x=0,storex=1,counter,count;

for(counter=1;counter>storec;counter++)
{
storec=counter;
printf("\n");

for(x=storex,count=1;count<=storec;x++,count++)
{
if(x==2)
printf(" ");
if(x==4)
printf(" ");
if(x==1)
printf(" ");

if(x==3||x==5||x==8||x==6||x==9||x==10)
printf(" %d",x);
else
printf("%d",x);
storex=x+1;
}

if(storex==11)
storec=999;
}
}

CHAP 3[E]h Printing multiplication tables

Write a program to print the multiplication table of the number entered by the user. The table should get displayed in the following form.
29 * 1 = 29
29 * 2 = 58


void main()
{
int x;
float n,r;
printf("Enter a number to have the multiplication table displayed");
scanf("%f",&n);

for(x=1;x<=25;x++)
{
r=n*x;
printf("\n%f*%d=%f",n,x,r);
}
}

CHAP 3[E]g Printing diamond and hearts

Write a program to fill the entire screen with diamond and heart alternatively. The ASCII value for heart is 3 and that of diamond is 4.

void main()
{
float d=1;
while(d<9999)
{
printf("%c%c",3,4);
d++;
}
}

CHAP 3[E]f Making alphabet patterns

Write a program to produce the following output:













void main()
{

int d,a,b,e,f;
printf("\nInput a value from 66 to 90\n");
scanf("%d",&b);
f=b-65;
for(a=0;a<=f;a++)
{
printf("\n");

for(d=65;d<=b;d++)
printf("%c",d);


if(a>0)
{
e=2*a-1;
for(;e>0;e--)
printf("%c",0);
}

if(a==0)
b=b-1;

for(d=b;d>=65;d--)
printf("%c",d);

if(a==0)
b=b+1;

b=b-1;
}
}

CHAP 3[E]d Permutations of a 3-digit number

Write a program to generate all combinations of 1, 2 and 3 using for loop.

void main()
{
int x,y,z,a,b,c;
printf("Input 3 digits");
scanf("%d%d%d",&x,&y,&z);
a=0;
b=0;
c=0;




for(a=1;(a==x||a==y||a==z)&&a<=9;b++)
{
for(b=1;(b==x||b==y||b==z)&&b<=9;b++)
{
if(a==b)
continue;

for(c=1;(c==x||c==y||c==z)&&c<=9;c++)
{
if(c==a)
continue;

if(c==b)
continue;

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

break;
}
}
}


}

CHAP 3[E]c Adding first seven terms of some series

Write a program to add first seven terms of the following series using a for loop:







void main()
{
float d,a,b,c,e;
printf("\nEnter a value\n");
scanf("%f",&e);

a=0;

for(d=1;d<=e;d++)
{
for(b=d,c=1;b>0;b--)
c=c*b;

a=a+d/c;
}

printf("\nThe first %f terms of the following series is %f",e,a);
}

CHAP 3[E]b Printing smiling faces

Write a program to fill the entire screen with a smiling face. The smiling face has an ASCII value 1.

void main()
{
char x =1;
float f=10004;
while(f>0)
{
printf("%c",x);
f--;
}
}

CHAP 3[E]a Printing prime numbers from 1 to 300

Write a program to print all prime numbers from 1 to 300. (Hint: Use nested loops, break and continue)

void main()
{
int d,f,e;
printf("\Input a number to print all the prime numbers from 1 to that value\n");
scanf("%d",&e);
printf("The prime numbers are 2");

for(d=1;d<=e;d++)
{
for(f=2;f {
if(d%f==0)
break;
if(f==d-1)
printf(" %d",d);
}
}
}

CHAP 3[B]f Playing with matchsticks

Write a program for a matchstick game being played between the computer and a user. Your program should ensure that the computer always wins. Rules for the game are as follows:
− There are 21 matchsticks.
− The computer asks the player to pick 1, 2, 3, or 4 matchsticks.
− After the person picks, the computer does its picking.
− Whoever is forced to pick up the last matchstick loses the game.


void main()
{
char x;
int d=21,a,p;

printf("\nThere are 21 matchsticks.\nThe player will start first.\nThe Player and Computer is allowed to pick 1,2,3 or 4 matchsticks.\nWhoever picks up the last matchstick loses the game");

while(1)
{
if(d==1)
{
printf("\nThere is only %d matchstick left.\nInput the number of matchstick to withdraw",d);
scanf("%d",&a);

if(a!=1)
{
printf("\nYou entered an invalid value");
continue;
}

d-=a;
printf("\nThere are 0 matchsticks left");
}


if(d>1)
{
printf("\nThere are %d matchsticks.\nInput the number of matchsticks to withdraw",d);
scanf("%d",&a);

if(a<1||a>4)
{
printf("\nYou entered an invalid value");
continue;
}

d-=a;
printf("\nThere are %d matchsticks left",d);

}

if(d>16)
{
p=d-16;
d=16;
printf("\nThe computer picks %d matchsticks, there are 16 matchsticks left",p);
}

else if(d>11)
{
p=d-11;
d=11;
printf("\nThe computer picks %d matchsticks, there are 11 matchsticks left",p);
}

else if(d>6)
{
p=d-6;
d=6;
printf("\nThe computer picks %d matchsticks, there are 6 matchsticks left",p);
}

else if(d>1)
{
p=d-1;
d=1;
printf("\nThe computer picks %d matchsticks, there is only 1 matchstick left",p);
}

else if(d==0)
{
printf("\nYou picked the last matchstick, you lost! HAHAHA!!!");
printf("\nPress Q to quit");
scanf("%c",&x);

}

if(x==113)
break;

}
}

CHAP 3[B]e Printing armstrong numbers

Write a program to print out all Armstrong numbers between 1 and 500. If sum of cubes of each digit of the number is equal to the number itself, then the number is called an Armstrong number. For example, 153 = ( 1 * 1 * 1 ) + ( 5 * 5 * 5 ) + ( 3 * 3 * 3 )

void main()
{
int a,b,c,d;
d=2;
printf("\nThe program will print out Armstrong numbers between 1 to 500");

while(d<500)
{
a=d/100;
b=(d/10)-(a*10);
c=(d/1)-(a*100)-(b*10);

a=a*a*a;
b=b*b*b;
c=c*c*c;

if(a+b+c==d)
printf("\n%d\n",d);

d++;
}
}

CHAP 3[B]d Printing ASCII characters using while loop

Write a program to print all the ASCII values and their equivalent characters using a while loop. The ASCII values vary from 0 to 255.

void main()
{
int x;
x=0;
while(x<=255)
{
printf("\nASCII value %d Character %c",x,x);
x++;
}
}

CHAP 3[B]c Finding power of a value

Two numbers are entered through the keyboard. Write a program to find the value of one number raised to the power of another.

void main()
{
float a,p,t;
printf("\nInput two number, first is digit second is power");
scanf("%f%f",&a,&p);
t=1;
while(p>0)
{

t=t*a;
p--;
}

printf("\nThe answer is %f",t);
}

CHAP 3[B]b Finding Factorial

Write a program to find the factorial value of any number entered through the keyboard.

void main()
{
float d,t;
t=1;
printf("\nEnter a number to find its factorial");
scanf("%f",&d);

while(d>0)
{
t*=d;
d--;
}

printf("\nThe factorial of the number is %f",t);

}

CHAP 3[B](a) Calculating overtime pay

Write a program to calculate overtime pay of 10 employees. Overtime is paid at the rate of Rs. 12.00 per hour for every hour worked above 40 hours. Assume that employees do not work for fractional part of an hour.

void main()
{
float d,p;
int i=1;
while(i<=10)
{
printf("\nInput the total hours worked for the week");
scanf("%f",&d);

if(d<=40)
{
printf("\nNo overtime pay");
break;
}

else
p=12.00*(d-40);

printf("\nThe overtime pay is Rs. %f",p);
i++;
}
}

CHAP 4[D] Case Control Structure (using switch)

Write a program which to find the grace marks for a student using switch. The user should enter the class obtained by the student and the number of subjects he has failed in.
− If the student gets first class and the number of subjects he failed in is greater than 3, then he does not get any grace. If the number of subjects he failed in is less than or equal to 3 then the grace is of 5 marks per subject.
− If the student gets second class and the number of subjects he failed in is greater than 2, then he does not get any grace. If the number of subjects he failed in is less than or equal to 2 then the grace is of 4 marks per subject.
− If the student gets third class and the number of subjects he failed in is greater than 1, then he does not get any grace. If the number of subjects he failed in is equal to 1 then the grace is of 5 marks per subject


void main()
{
int c,s;
printf("\nEnter the class obtained by the student and the number of subjects he has failed in");
scanf("%d%d",&c,&s);

switch(c)
{
case 1:
switch(s>3)
{
case 1:
printf("\nNo grace");
break;
case 0:
printf("\nGrace of 5 marks per subject");
break;
}
break;

case 2:
switch(s>2)
{
case 1:
printf("\nNo grace");
break;
case 0:
printf("\nGrace of 4 marks per subject");
break;
}
break;

case 3:
switch(s>1)
{
case 1:
printf("\nNo grace");
break;
case 0:
printf("\nGrace of 5 marks per subject");
break;
}
break;
}
}

CHAP 14[A](b) Newspaper Problem (Using bits and FILE I/O)

A company planning to launch a new newspaper in market conducts a survey. The various parameters considered in the survey were, the economic status (upper, middle, and lower class) the languages readers prefer (English, Hindi, Regional language) and category of paper (daily, supplement, tabloid). Write a program, which reads data of 10 respondents through keyboard, and stores the information in an array of integers. The bit-wise information to be stored in an integer is given below:

Bit Number Information

0 Upper class
1 Middle class
2 Lower class
3 English
4 Hindi
5 Regional Language
6 Daily
7 Supplement
8 Tabloid

At the end give the statistical data for number of persons who read English daily, number of upper class people who read tabloid and number of regional language readers.


/*Note: if your input is Upper,English,Tabloid the program does not work as intended*/
#include
void main()
{
FILE *fs,*ft;
struct res
{
char name[25];
char a;
};
struct res resp;
int i,choice,a,b,c;
char another='y',name2[25],j,ch;
fs=fopen("c:\\ctxt\\AAA.txt","r+");
clrscr();

while(1)
{
clrscr();
a=0,b=0,c=0;
printf("1)Add new respondent");
printf("\n2)Delete respondent");
printf("\n3)Display all the respondent's name");
printf("\n4)Give statistical data");
printf("\n5)Exit\n");
scanf("%d",&choice);

switch(choice)
{
case 1:

fseek(fs,0,SEEK_END);
while(another=='y')
{
resp.name[0]='\0';
clrscr();
printf("Key in respondent's name\n");
scanf("%s",resp.name);
clrscr();
printf("Choose your social status");
printf("\n1)Upper class\n2)Middle class\n3)Lower class\n");
scanf("%d",&a);
clrscr();
printf("Choose which language you prefer");
printf("\n1)English\n2)Hindi\n3)Regional Language\n");
scanf("%d",&b);
clrscr();
printf("Choose category of paper");
printf("\n1)Daily\n2)Supplement\n3)Tabloid\n");
scanf("%d",&c);
resp.a=funcgen(a,b,c);
fprintf(fs,"%s\t%c\n",resp.name,resp.a);
printf("\nAdd another record (Y/N)?\n");
fflush(stdin);
another=getche();
}
break;

case 2:

fseek(fs,0,SEEK_SET);
while(another=='y')
{
clrscr();
printf("\nKey in respondent's name to delete it's data\n");
scanf("%s",name2);
ft=fopen("c:\\ctxt\\AAA2.txt","w");
while(fscanf(fs,"%s %c",resp.name,&resp.a)!=EOF)
{
if(strcmp(resp.name,name2)!=0)
fprintf(ft,"%s %c\n",resp.name,resp.a);


}
fclose(fs);
fclose(ft);
remove("c:\\ctxt\\\AAA.txt");
rename("c:\\ctxt\\AAA2.txt","c:\\ctxt\\AAA.txt");
fs=fopen("c:\\ctxt\\AAA.txt","r+");
printf("Delete another record?(Y/N)");
fflush(stdin);
another=getche();
}
break;

case 3:

fseek(fs,0,SEEK_SET);
while(1)
{
i=0;
clrscr();
ch=0;
fseek(fs,0,SEEK_SET);
while(ch!=EOF)
{
resp.name[0]='\0';
ch=fscanf(fs,"%s %c",resp.name,&resp.a);
fflush(stdin);
if(ch==EOF)
continue;
i++;
printf("\n%d) %s",i,resp.name);
printf("\n");
printf(" Bit number is ");
showbits(resp.a);
}
fflush(stdin);
printf("\nKey q to quit\n");
another=getche();
if(another=='q')
break;
}
break;

case 4:

fseek(fs,0,SEEK_SET);
while(1)
{
clrscr();
a=0,b=0,c=0;
while(fscanf(fs,"%s %c",resp.name,&resp.a)!=EOF)
{
j=funcget(resp.a);
if(j==1)
a++;
if(j==2)
b++;
if(j==3)
c++;
}
printf("\nThere are %d people who read English daily\nThere are %d upper class people reading tabloid\nThere are %d regional readers",a,b,c);

printf("\nKey q to quit\n");
fflush(stdin);
another=getche();
if(another=='q')
break;
}
break;

case 5:

fclose(fs);
exit();
}

}
}

funcgen(a,b,c)
{
int d,e=0;
d=1<<(a-1);
e|=d;
d=8<<(b-1);
e|=d;
d=64<<(c-1);
e|=d;
return(e);
}

funcget(char r)
{
char d;
char mask1,mask2,mask3;
mask1=72;
mask2=257;
mask3=32;
d=mask1&r;
if(d==72)
return(1);
d=mask2&r;
if(d==257)
return(2);
d=mask3&r;
if(d==32)
return(3);
else
return(0);
}

showbits(char a)
{
int i;
char b,mask;

for(i=8;i>=0;i--)
{
mask=1< b=a&mask;
b==0?printf("0"):printf("1");
}
}

CHAP 14[A]a Rainbow problem

The information about colors is to be stored in bits of a char variable called color. The bit number 0 to 6, each represent 7 colors of a rainbow, i.e. bit 0 represents violet, 1 represents indigo, and so on. Write a program that asks the user to enter a number and based on this number it reports which colors in the rainbow does the number represents.

Colours of rainbow are in order of : Red, Orange, Yellow, Green, Blue, Indigo, Violet

void main()
{
int i,input,mask,j;
printf("\nKey in a number\n");
scanf("%d",&input);

for(i=6;i>=0;i--)
{
mask=1< j=mask&input;
if(i==6&&j==64)
printf("Red");
if(i==5&&j==32)
printf(" Orange");
if(i==4&&j==16)
printf(" Yellow");
if(i==3&&j==8)
printf(" Green");
if(i==2&&j==4)
printf(" Blue");
if(i==1&&j==2)
printf(" Indigo");
if(i==0&&j==1)
printf(" Violet");

}
}