Saturday, March 10, 2012

C Programs Lists With their output

Write a C program to check whether a given number is palindrome or not.

#include 
#include 
void main()
{
 long int n, num, rev = 0, dig;
 printf("\n Enter a number...: ");
scanf("%ld", &num);
 n = num;
while(num>0)
{
dig = num % 10;
 rev = rev * 10 + dig;
 num = num / 10;
 }
 if (n == rev)
 printf("\nGiven number is a palindrome");
 else
 printf("\n Given number not a palindrome");
 getch();
}

Write a program to check whether a given number is armstrong or not.




#include  
#include  
void main()
{
 int a,b,y,m,q;
printf(“Enter any no. \n”);
scanf(“%d”,&a);
 b=a;
y=0;
while(b!=0)
{
 m=b%10;
 q=b/10;
 y=y+(m*m*m);
 b=q;
}
if(y==a)
{
printf(“ The no. is Armstrong\n”); printf("%d\n",a);
}
else
{


printf(“ The no. is not Armstrong\n”); printf("%d\n",a);
}
getch();

}

Write a C program to find prime numbers from 1 to n.

#include
#include
void main()
{
int i,j,n;
clrscr();
printf("Enter the no: ");
scanf("%d",&n);
printf("\n");
for(i=1;i<=n;i++)
{
for(j=2;j<=i-1;j++)
{
if(i%j==0)
break;
}
if(j==i)
printf("%d ",j);
}
getch();
}

Write a C program to find the sum of first n natural numbers.

#include  
#include
void main()
{
 int i, n, sum = 0;
clrscr();
printf(“Enter an integer number\n”);
scanf (“%d”, &n);
for (i=1; i <= n; i++)
{
 sum = sum + i;
}
printf (“Sum of first %d natural numbers = %d\n”, n, sum);

getch();
}

Write a program to find the factorial of a number using loop.

#include
#include
void main()
{
int a,b,fact=1;
clrscr();
printf("Enter number to find factorial");
scanf("%d",&b);
for(a=b;a>0;a--)
{
fact=fact*a;
}
printf("%d ",fact);
getch();
}

Write a C program to find the factorial of a number using recursion.

#include
#include
int fact(int);
void main()
{
int num,fact1;
printf("Enter a value of num");
scanf("%d",&num);
fact1=fact(num);
printf("factorial=%d",fact1);
getch();
}
int fact(int n)
{
int f=1;
if(n==0)
{
return 1;
}
else
{
f=n*fact(n-1);
return(f);
}
}

Write a program to find fibonacci series in c using loop.

#include
#include
void fiab(int n)
{
int a=0,b=1,add=0;
printf("%d %d ",a,b);
while(add
{
add=a+b;
a=b;
b=add;
printf("%d ",a);
}
}

void main()
{
int i,j,a,b;
clrscr();
printf("\n Enter number to print fibonacci number:\n");
scanf("%d",&a);
printf("\n Fiabonacci series:\n");
fiab(a);
getch();
}

Write a program to find fibonacci series in c using recursion.

#include
#include
int fiab(int);

void main()
{
int i,n;
clrscr();
printf("How many terms do you wish to find?\n");
scanf("%d",&n);

printf("\nThe Series is : \n");

for(i=1;i<=n;i++)
{
printf(" %d \n",fiab(i));
}

getch();
}

int fiab(int n)
{
if(n==1||n==2){
return 1;}
else{
return(fiab(n-1)+fiab(n-2));}
return 0;
}
 

Write a C program to print the following Pattern

A) 1      
     1 2
     1 2 3
     1 2 3 4 


#include
#include
void main()
{
int i,j;
clrscr();
for(i=0;i<5;i++)
{
for(j=1;j<=i;j++)
{
printf("%d ", j);
}
printf("\n");
}
getch();
}


B)  1
      2 2
      3 3 3
      4 4 4 4 

#include
#include
void main()
{
int i,j;
clrscr();
for(i=0;i<5;i++)
{
for(j=1;j<=i;j++)
{
printf("%d ", i);
}
printf("\n");
}
getch();
}





Sociable