8.29.2011

Find out Factioral value


Ans.

#include<stdio.h>
#include<conio.h>
void main()
{
 int fact=1,num;
 clrscr();
 printf("\nEnter Number : ");
 scanf("%d",&num);
 while(num>=1)
 {
  fact=fact*num;
  num--;
 }
 printf("\nFactorial value of %d is %d",num,fact);
}

       Output of above program : 
Enter Number : 5
Factorial value of 5 is 120
 

operation's number(sum,reverse,store in other variable)

Q. Write a program to insert a number and do following tasks:
 a.Display in reverse order?
 b.Reverse in another variable and display it?
 c.Display sum of its all digit?

Ans.

#include<stdio.h>
#include<conio.h>
void main()
{
 int x,num,rem,sum=0,var=0;
 clrscr();
 printf("\nPress Number 1 for reverse number");
 printf("\nPress Number 2 for reverse number in another variable");
 printf("\nPress Number 3 for sum of entered number");
 scanf("%d",&x);
 switch(x)
 {
 case 1 :
  printf("\nEnter Number : ");
  scanf("%d",&num);
  while(num>=1)
  {
   rem=num%10;
   printf("Reverse number : %d",rem);
   num=num/10;
  }
  break;
 case 2 :
  printf("Enter Number : ");
  scanf("%d",&num);
  while(n>=1)
  {
   rem=n%10;
   var=var*10+rem;
   n=n/10;
  }
  printf("%d",var);
  break;
 case 3 :
  printf("\nEnter Number : ");
  scanf("%d",&num);
  while(num>=1)
  {
   rem=num%10;
   sum=sum+rem;
   num=num/10;
  }
  printf("Sum of %d is %d",num,sum);
  break;
 default :
   printf("You enter wrong number!!!");
   break;
 }
}

       Output of above program : 

Press Number 1 for reverse number
Press Number 2 for reverse number in another variable
Press Number 3 for sum of entered number

1
Enter Number : 4812
Reverse number : 2184
 

Print table in following format

Q. Write a program to printout following table:
 (where num=5)
       
         5*1=5
         5*2=10
         5*3=15
         5*4=20
         5*5=25
         5*6=30
         5*7=35
         5*8=40
         5*9=45
         5*10=50

Ans.
 
#include<stdio.h>
#include<conio.h>
int main()
{
 int x=1,num,res;
 printf("Enter Number : ");
 scanf("%d",&num);
 while(x<=10)
 {
   res=num*x;
   printf("\n%d*%d=%d",num,x,res);
   x++;
 }
 getch();
 return 0;
}

       Output of above program : 
Enter Number : 5

         5*1=5
         5*2=10
         5*3=15
         5*4=20
         5*5=25
         5*6=30
         5*7=35
         5*8=40
         5*9=45
         5*10=50

Print one to ten using for loop

Q. Write a program to printout one to ten counting using for loop?

Ans.

#include<stdio.h>
#include<conio.h>
int main()
{
  int x;
  for(x=1; x<=10; x++)
    printf("\n%d",x);
  getch();
  return 0;
}

     Output of above program : 
  1
  2
  3
  4
  5
  6
  7
  8
  9
  10

Print one to ten program

Q. Write a program to printout one to ten counting using while loop?
Ans.

#include<stdio.h>
#include<conio.h>
int main()
{
 int x=1;
 while(x<=10)
 {
   printf("\n%d",x);
   x++;
 }
 getch();
 return 0;
}

       output of above program : 

  1
  2
  3
  4
  5
  6
  7
  8
  9
  10

comparison of three numbers

Q. Write a program to compare three numbers which is greatest,middle and lowest?

Ans.

#include<stdio.h>
#include<conio.h>
int main()
{
 int x,y,z;
 printf("Enter values of x, y and z : ");
 scnaf("%d%d%d",&x,&y,&z);
 if(x>=y && x>=z)
   printf("\nx=%d is greatest",a);
 else if(y>=z)
   printf("\ny=%d is greatest",y);
 else
   printf("\nz=%d is greatest",z);
 getch();
 return 0;
}

      Output of above program :
Enter values of x,y and z : 30
20
100

z=100 is greatest

8.28.2011

find greatest number using conditional operator

Q. Using conditional operators write a program to find out in three Numbers which is greatest number?
Ans.

#include<stdio.h>
#include<conio.h>
int main()
{
 int n1,n2,n3;
 prinft("Enter Numbers n1, n2, n3 : ");
 scanf("%d"%d%d",n1,n2,n3);
 printf("Greatest=%d",n1>n2?n1>n3?x:z:y>z?y:z);
 getch();
 return 0;
}


       Output of above program : 
Enter Numbers n1, n2, n3 :10
20
30

Greatest=30

Simple Swapping C Program

Q. Write a simple program to insert two values through keyboard and interchange then values?
Example:if x=10
           y=20
        then after interchange
          x=20
          y=10
       
Ans.

#include<stdio.h>
#include<conio.h>
int main()
{
 int x,y,z;
 printf("Enter Values x and y : ");
 scnaf("%d%d",&x,&y);
 z=y;
 y=x;
 x=z;
 printf("\nValue of x=%d",x);
 printf("\nValue of y=%d",y);
 getch();
 return 0;
}

        Output of above program : 
Enter Values x and y : 25
35
Value of x= 35
Value of y= 25

Add two numbers

Q. Write a program to add two numbers?

Ans.

#include<stdio.h>
#include<conio.h>
void main()
{
 int a,b,sum;
 clrscr();
 printf("Enter Numbers a and b : ");
 scnaf("%d%d",&a,&b);
 sum=a+b;
 printf("\nsum(a+b)=%d",sum);
 getch();
}

     Output of above program : 

Enter Numbers a and b : 100
200

sum(a+b)=300

Sample C Program

Q. Write a simple C program,in which user entered his name,age through keyboard and display on console?

Ans.

/*c program to accept name & age from user and display it*/
#include<stdio.h>
#include<conio.h>
int main()
{
 char nam[20];
 int age;
 printf("Enter Your Name : ");
 scnaf("%s",&nam); // gets(nam)
 printf("Enter Your Age : ");
 scanf("%d",&age);
 printf("\nUser Name is %s",nam); 
 /*You also use puts(nam)*/
 printf("\nAnd %s age is %d",nam,age);
 getch();
 return 0;
}

       Output of above program : 

Enter your name : JamesBond
Enter your age : 35

And JamesBond age is 35