Subscribe

RSS Feed (xml)

Friday, April 16, 2010

If a five digit number is input through keyboard ,write a program to print a new number by adding one to each of tis digits.

Note: If a five digit number is input through keyboard ,write a program to print a new number by adding one to each of tis digits for example a number is input is 12391 then the output should be displayed as 23502 .

#include< stdio.h>
#include< conio.h>
int main()
{
long int no,sum=0;
int rem,check=0;
clrscr( );
printf("Enter the required number:");
scanf("%ld",&no);
printf("\nGiven Number: %d",no);
while(no>0) // to store the number in reverse by adding one to each digit
{
rem=no%10;
if(rem!=9)
{
if(check==0) // imp , try to take few minutes here
sum=(10*sum)+(rem+1);
else{
sum=(10*sum)+(rem+2);
check=0;
}
} //if closed
else{
sum=(10*sum)+0;
check=1;
}
no=no/10;
} // while closed

// Afftr adding , again to print the reverse number in origianl form.

no=sum; sum=0;
while(no>0)
{
rem=no%10;
sum=(10*sum)+rem;
no=no/10;
}
printf("\nAfter Adding one: %ld",sum);
getch( );
return 0;
}

Output:
Enter the reuired Number: 12391
Given Number: 12391
After Adding one to each of its digits: 23502

Monday, March 8, 2010

Write a C program to find the greatest common divisor (GCD) of the two given positive integers.

#include< stido.h>
#include< conio.h>
int main( )
{
int a, b, res;
clrscr( );
printf("Enter the two integer values:");
scanf("%d%d", &a, &b);

for( ; ; ) //This is empty for loop.
{
res = a%b;
if( res = = 0) break;
a = b;
b = res;
}
printf("\n The GCF is : %d", res);
getch( );
return 0;
}

Write a C program that prints a given positive integer in reverse order and also sum of the individual digits involved in the given integer.

#include< stdio.h>
#include< conio.h>
int main( )
{
int no, rev=0, rem, sum=0;
clrscr( );
printf("Enter the required integer:");
scanf("%d", &no);
while(no>0)
{
rem = no%10;
rev = (10*rev)+rem;
sum = sum+rem;
no=no/10;
}
printf("\nReverse Number : %d", rev);
printf("\nSum of the individual digits : %d", sum);
getch( );

return 0;
}

Note: If any compile errors, please mail to me..

Write a C program to find the commission on a salesman's total sales

Full Question:
                      The commission on a salesman’s total sales is as follows:
a) If sales <100, then there is no commission.

b) If 100>= sales <=500, then commission = 10% of sales.

c) If sales > 500, then commission = 100+8% of sales above 500

Solution:-

#include< stdio.h>
#include< conio.h>
int main( )
{
int sales;
float comm;
clrscr( );
printf("Enter the total sales:");
scanf("%d", &sales);
if(sales<100)
printf("\nSorry no Commission, please do more sales");
else if(sales>=100 && sales<=500)
printf("\nCommission : %f ", (sales*0.1));
else
{
comm=(sales-500)*0.08;
printf("\nCommission : %f ", (comm+100));
}
getch( );
return 0;
}

Wednesday, February 17, 2010

Write a C program to find the exponential series of 1+x+x2/2!+x3/3!+.......+xn/n!

#include< math.h>
void main( )
{
int x, n, fact, i, j;
float sum=1;
clrscr( );
printf("Enter the 'x' value:");
scanf("%d",&x);
printf("\nEnter the 'n' value:");
scanf("%d",&n);
for(i=1; i< =n ; i++)
{
fact=1;
for( j=i ; j >=1; j--)
fact=fact*j;

sum=sum+(pow(x,i )/ fact);
}
printf("\nSum of the series : %f ",sum);
getch( );
}

Note:- If any errors,, plz mail to me.

Sunday, February 14, 2010

Write a C program to display all Even numbers upto the given range.

#include< stdio.h>
#include< conio.h>
int main( )
{
int x, r;
clrscr( );
printf("Enter the range:");
scanf("%d",&r);
for(x=1; x <=r; x++)
{
if(x%2==0)
printf("%5d", x);
}
getch( );
return 0;
}

Output:-
Enter the range:10
2 4 6 8 10

Tuesday, February 9, 2010

Write a program to compute the sum of the terms 1+3+5+7+9+--------- until the sum>500

#include< stdio.h>
#include< conio.h>
int main()
{
long int i,sum=0;
clrscr();
for(i=1; ;i++)
{
if(i%2!=0)
sum=sum+i;
if(sum>500)
break;
}
printf("\nSum of the 1+3+5+---- until the sum>500 is : %ld",sum);
getch( );
return 0;
}

Write a C program to compute the sum of the terms 1+2+3+.....upto N terms

#include< stdio.h>
#include< conio.h>
int main()
{
int n,i,sum=0;
clrscr();
printf("Enter the 'n' value:");
scanf("%d",&n);
for(i=1; i<=n; i++)
{
sum=sum+i;
}
printf("\nSum of the 'n' terms: %d",sum);
getch();
return 0;
}

Monday, January 11, 2010

Write a C program that adds first seven terms of the following series 1/1! +2/2! + 3/3! + ..........

#include< stdio.h>
#include< conio.h>
int main( )
{
int n,i,j,fact;
float sum=0;
clrscr();
for(i=1; i<=7; i++) { fact=1; for(j=i; j>=1; j--) // to find the factorial
fact=fact*j;

sum=sum+(float)i/fact;
}
printf("\nSum : %f",sum);
getch();
return 0;
}

Write a C program to find a peculiar two digit number which is three times the sum of its digits?

example:- 27
=3(2+7)
=3(9)
=27

#include< stdio.h>
#include< conio.h>
int main()
{
int n,sum=0,rem,temp;
clrscr();
printf("Enter any two digit number:");
scanf("%d",&n);
for(temp=n;temp>0;temp=temp/10)
{
rem=temp%10;
sum=sum+rem;
}
if(n==(sum*3))
printf("\nIts a Peculiar two digit number");
else
printf("\nNot a Peculiar two digit number");
getch();
return 0;
}