Basic c programs

Program1:
//This program determine the factorial of the given number
//with recursive functions

#include
#include
int fact(int);
void main()
{
int n;
printf("enter a number to find the factorioal\n");
scanf("%d",&n);
printf("The factorial of the given number is : %d",fact(n));
getch();
}
int fact(int n)
{
if(n==1)
return 1;
else
return n*fact(n-1);
}























Program2:

//This program finds the factorial of the given number with out recursion

#include
#include

int fact(int);
void main()
{
int n;
printf("enter a number\n");
scanf("%d",&n);
printf("factorial is : %d",fact(n));
getch();
}

int fact(int n)
{
int f=1;
while(n>1)
{
f*=n;
n--;
}
return f;
}



















Program3:

//This program prints the fibonacci series for the given length
//with out using recursion

#include
#include

void fib(int);

void main()
{

int n;
printf("enter the length of the fibonacci series you want\n");
scanf("%d",&n);
fib(n);

getch();
}

void fib(int n)
{
int f0=0,f1=1,f2,i;
for(i=0;i
#include

void fib(int,int,int);

void main()
{

int n,i;
printf("enter the length of the fibonacci series you want\n");
scanf("%d",&n);
fib(n,0,1);

getch();
}

void fib(int n,int f0,int f1)
{
int f2;
static int i=0;
printf("%d ",f2=f1+f0);
i++;
if(i
#include
#include

void main()
{
int i,l,a[10];

printf("enter 10 numbers\n");
for(i=0;i<10;i++) scanf("%d",&a[i]); l=a[0]; for(i=1;i<10;i++) if(l
#include

void main()
{
int a[20],b[20],c[40];
int na,nb,i,j;
int temp,k=0;

printf("enter no.of elements to be entered into a\n");
scanf("%d",&na);
printf("enter the elements for a \n");
for(i=0;ic[j])
{
temp=c[i];
c[i]=c[j];
c[j]=temp;
}
}
}
printf("merged and sorted elements are\n");
for(i=0;i<(nb+na);i++) printf("%d ",c[i]); getch(); } Program7: //no.of spaces in the given string #include
#include
#include

void main()
{
int s=0;
int i,l;
char str[40];

printf("enter a string\n");
gets(str);
l=strlen(str);
for(i=0;i
#include
#include

void main()
{
int s=0;
int l,i;
char vow[10]={'a','A','e','E','i','I','o','O','u','U'};
char *str,str1[20];

printf("enter a string\n");
gets(str1);
str=str1;
for(i=0;i
#include

void print(int);
void tensPrint(int);
void tenPrint(int);
void hundredsPrint(int);

void main()
{
int n;
printf("enter a number\n");
scanf("%d",&n);
if(n>=10000)
printf("Please give the below 10000 only");
if(n>=1000&&n<10000) { print(n/1000); printf("thousand "); hundredsPrint(n%1000); } else if(n>=100&&n<1000) { print(n/100); printf("hundred "); if(n%100!=0) { printf("and "); if(n%100>10)
tensPrint(n%100);
else
print(n%100);
}
}
else if(n<100&&n>=10)
tensPrint(n);
else if(n<10&&n>0)
print(n);
else if(n==0)
printf("zero");
getch();
}

void print(int n)
{
switch(n)
{
case 1:
printf("one ");
break;
case 2:
printf("two ");
break;
case 3:
printf("three ");
break;
case 4:
printf("four ");
break;
case 5:
printf("five ");
break;
case 6:
printf("six ");
break;
case 7:
printf("seven ");
break;
case 8:
printf("eight ");
break;
case 9:
printf("nine ");
break;
}

}

void hundredsPrint(int n)
{

if(n/100>0)
{
print(n/100);
printf("hundred ");
if(n%100!=0)
{
printf("and ");

}
}
if(n%100>=10)
tensPrint(n%100);
else
print(n%100);
}

void tensPrint(int n)
{
int m;
int i=n%10;
if(n/10==1)
tenPrint(n);
else
{
m=n/10;
switch(m)
{
case 2:
printf("twenty ");
print(i);
break;
case 3:
printf("thirty ");
print(i);
break;
case 4:
printf("fourty ");
print(i);
break;
case 5:
printf("fifty ");
print(i);
break;
case 6:
printf("sixty ");
print(i);
break;
case 7:
printf("seventy ");
print(i);
break;
case 8:
printf("eightty ");
print(i);
break;
case 9:
printf("ninety ");
print(i);
break;
}

}
}

void tenPrint(int n)
{
switch(n%10)
{
case 0:
printf("ten");
break;
case 1:
printf("eleven ");
break;
case 2:
printf("twelve ");
break;
case 3:
printf("thirteen ");
break;
case 4:
printf("fourteen ");
break;
case 5:
printf("fifteen ");
break;
case 6:
printf("sixteen ");
break;
case 7:
printf("seventeen ");
break;
case 8:
printf("eightteen ");
break;
case 9:
printf("nineteen ");
break;
}
}
















Program10:

//this program checks whether the given string is palindrome or not
//By using the string functions

#include
#include
#include

void main()
{
int i,l,k=0;
char str[30],str1[30];

printf("enter a string\n");
gets(str);

strcpy(str1,str);
strrev(str1);

printf("\ngiven string is %s\n",str);
printf("reversed string is %s\n",str1);

if(!strcmp(str,str1))
printf("the given string is a palindrome");
else
printf("the given string is not a palindrome");

getch();
}
















Program11:
//This program chether the given string is palindrome or not
//with out using string functions

#include
#include
#include

void main()
{
int i,l;
char str[30];
printf("enter a string\n");
gets(str);
l=strlen(str);
for(i=0;i
#include

void main()
{
int n,p=1;
int i;
printf("enter the number\n");
scanf("%d",&n);
if(n==1)
printf("1 is neither prime nor non prime");
else
{
for(i=2;i<=n/2;i++) { if(n%i==0) { p=0; break; } } if(p==0) printf("The given number is not a prime number\n"); else printf("The given number is a prime number\n"); } getch(); } Program13: //Remove duplicate values in array #include
#include

void main()
{
int a[20],b[20];
int i,n,j=0,l=0,s=0;
printf("enter no of values to be entered\n");
scanf("%d",&n);
printf("enter %d values into the array\n",n);
for(i=0;i
#include

void main()
{
int n,rev=0,rem;
printf("enter a number\n");
scanf("%d",&n);
while(n!=0)
{
rem=n%10;
n=n/10;
rev=rev*10+rem;
}
printf("the reversed number is %d",rev);
getch();
}
// rotate matrix in 90 degrees
#include
#include

void main()
{
int i,j,m,n,k=0,l=0;
int a[20][20],b[20][20];
printf("enter size of the matrix i*j\n");
scanf("%d%d",&m,&n);
printf("enter %d values into the array\n",m*n);
for(i=0;i=0;j--)
{
b[k][l++]=a[j][i];
}
k++;
}
printf("The rotated matrix is :\n");
for(i=0;i
#include
#include

void main()
{
int i,l;
char str[30];
char temp;
printf("enter a string\n");
gets(str);
l=strlen(str);
for(i=0;i
#include
#include

void main()
{
char str[30];
int i,l,j=0;

printf("enter a string\n");
gets(str);
l=strlen(str);
printf("the length of the string with out trim is %d\n",strlen(str));
for(i=0;str[i]==' ';i++);
while(str[i]!='\0')
{
str[j]=str[i];
i++;
j++;
}
str[j]='\0';
l=strlen(str);
while(str[l-1]==' ')
{
l--;
}
str[l]='\0';
printf("the trimmed string is %s\n",str);
printf("the length of the trimmed string is %d",strlen(str));
getch();
}