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");
}