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