Single Post

Header

Sunday, September 30, 2012

C Programs

1. Factorial of a given number using recursion and non recursion
#include"stdio.h"
void main()
{
int i,n,f=1,r;
printf("\n enter a number to find factorial");
scanf("%d",&n);
for(i=n;i>=2;i--)
f=f*i;
printf("\n the factorial of given number without recursion is %d",f);
/* Factorial of a number using recursion */
r=fact(n);
printf("\n Factorial of a given number using recursion is %d",r);
}
fact(int n)
{
int d=1;
if(n==0 || n==1)
return d;
else
d=n*fact(n-1);
return d;
}
2.Swapping of Two Numbers without using third variable , swapping through call by value and call by reference
#include"stdio.h"
void main()
{
int a,b;
printf("\n enter numbers which u want to swap without 3rd variable");
scanf("%d %d",&a,&b);
printf("\n the numbers before swap %d\t%d",a,b);
a=a+b;
b=a-b;
a=a-b;
printf("\n the numbers after swap %d\t%d",a,b);
/* swapping two variables through call by value */
swap1(a,b);
printf("\n the swapped numbers after call by value %d\t%d",a,b);
/* swapping two variables through call by reference */
swap2(&a,&b);
printf("\n the swapped numbers after call by reference %d\t%d\n",a,b);
}
swap1(int x,int y)
{
int temp;
temp=x;
x=y;
y=temp;
}
swap2(int *x, int *y)
{
int temp;
temp=*x;
*x=*y;
*y=temp;
}
3.Check whether the string is palindrome or not., String Reverse with and without using another character array
#include"stdio.h"
void main()
{
char a[20],i,j,k,temp,b[20],c[20];
/* Reverse of a string without using another array */
printf("\n enter a string which u want to reverse without using another string\n");
scanf("%s",a);
strcpy(c,a);
printf("\n the entered string is %s",a);
for(i=0;a[i]!='\0';i++);
printf("\n the length of the string is %d",i);
for(k=0,j=i-1;j>i/2;j--,k++)
{
temp=a[k];
a[k]=a[j];
a[j]=temp;
}
printf("\n the reverse string is %s \n",a);
/* Reverse of a string using another array */
for(k=0,j=i-1;j>=0;j--,k++)
b[k]=c[j];
printf("\n the reverse string again\t");
puts(b);
/*Check whether the string is palindrome or not */
for(k=0,j=i-1;j>=i/2;j--,k++)
{
if(a[k]==a[j]);
if(a[k]!=a[j])
{
printf("\n the given string %s is not a palindrome",c);
break;
}
if(j==i/2)
{
printf("\n the given string %s is a palindrome",c);
break;
}
}
}
4.Fibonacci Series using recursion and non recursion
#include"stdio.h"
void main()
{
int f0=0,f1=1,f2=f0+f1,n;
printf("\n enter number upto which u want in fibanacci series");
scanf("%d",&n);
printf("\n fibanacci series without recursion");
printf("%d\t%d ",f0,f1);
while(f2<=n)
{
printf("\t%d",f2);
f0=f1;
f1=f2;
f2=f0+f1;
}
printf("\n fibonacci series using recrsion");
f0=0;
f1=1;
f2=f0+f1;
printf("%d\t%d",f0,f1);
fibnaci(f0,f1,f2,n);
}
fibnaci(int f0,int f1,int f2,int n)
{
printf("\t%d",f2);
f0=f1;
f1=f2;
f2=f0+f1;
if(f2<=n)
fibnaci(f0,f1,f2,n);
else
return;
}

5.Check whether the given number is palindrome or not

#include"stdio.h"
void main()
{
int a,c,r=0,d,n;
printf("\n enter number which u want to check it for palindrome");
scanf("%d",&n);
c=n;
while(n!=0)
{
d=n%10;
r=r*10+d;
n=n/10;
}
if(c==r)
printf("\n the given number %d is palindrome",c);
else
printf("\n the given number %d is not a palindrome",c);
}
6.File Operations
#include
void main()
{
FILE *p1,*p2,*p3;
int c,d;
char s1[30],s2[30];
p1=fopen("first.txt","r");
if(p1==NULL)
printf("we can't open file");
else
printf("we can open file");
p2=fopen("second.txt","r");
p3=fopen("third.txt","w");
while(fgets(s1,29,p1)!=NULL)
{
c=0;
printf("the string is %s",s1);
while(fgets(s2,29,p2)!=NULL)
{
d=strcmp(s1,s2);
if(d==0)
break;
c++;
}
if(c==2)
fputs(s1,p3);
p2=fopen("second.txt","r");
}
fclose(p1);
fclose(p2);
fclose(p3);
}
7.Addition and Deletion of an element at anywhere into the given array
#include"stdio.h"
#define max 20
int i,n,a[max];
void main()
{
int choice;
printf("\n enter number of array elements");
scanf("%d",&n);
printf("\nenter array elements");
for(i=0;i
scanf("%d",&a[i]);
printf("\nthe array elements are");
for(i=0;i
printf("\t%d",a[i]);
while(1)
{
printf("\n 1.add an element to array");
printf("\n 2.delete an element from array");
printf("\n 3.exit from program");
printf("\n enter ur choice");
scanf("%d",&choice);
switch(choice)
{
case 1: addition();
break;
case 2: deletion();
break;
case 3: exit(0);
break;
default:printf("\n u chose wrong choice");
break;
}
}
}
addition()
{
int pos,temp,e;
/*Adding an element into an array at any position */
printf("\n enter element which u want to add and it's position");
scanf("%d %d",&e,&pos);
pos=pos-1;
temp=n;
while(pos)
{
a[temp]=a[temp-1];
temp--;
}
a[pos]=e;
n++;
printf("\nAfter adding element the array elemetns are");
for(i=0;i
printf("\t%d",a[i]);
}
deletion()
{
int i=0,pos,e;
printf("enter number which u want to delete");
scanf("%d",&e);
while(i
{
if(a[i]==e)
{
pos=i;
break;
}
if(i==n)
{
printf("the searching element %d is not found",e);
break;
}
i++;
}
while(pos)
a[pos]=a[++pos];
n--;
printf("After deleting the element the array elements are");
for(i=0;i
printf("\t%d",a[i]);
}

No comments:

Post a Comment