programmania
Thursday, 4 April 2013
Factorial of a number using resursion
#include<stdio.h>
#include<conio.h>
int fact(int);
main()
{int y,n;
printf("Enter a number to find its factorial\n");
scanf("%d",&n);
y=fact(n);
printf("factorial = %d",y);
getch();
}
int fact(int x)
{
if(x>1)
return x*fact(x-1);
else
return 1;
}
Program to print pyramid
program to print following pattern
#include<stdio.h>
#include<conio.h>
int abs(int);
main()
{
int n,x=0,i,j,k=0,t=0;
printf("Enter number of lines");
scanf("%d",&n);
for(i=0;i<n;i++)
{k=(-1)*t;
for(j=n-i;j>0;j--)
{
printf(" ");
}
while(k<=t)
{
printf("%d",abs(k));
k++;
}
t++;
printf("\n");
}
getch();
}
int abs(int x)
{
if(x<0)
return (-1)*x;
else
return x;
}
program to print following pattern
#include<stdio.h>
#include<conio.h>
main()
{
int i,j,k,l,n,counter;
printf("enter number of rows");
scanf("%d",&n);
l=n;
for(j=1;j<=n;j++){
for(i=1;i<l;i++)
printf(" ");
for(counter=0;counter<2*j;counter++)
{
if(counter%2==0)
printf("*");
else
printf(" ");
}
l--;
printf("\n");
}
getch();
}
Newer Posts
Older Posts
Home
Subscribe to:
Posts (Atom)