Friday, January 25, 2013

Sample C Programs


Basic C Programs for Technical Interviews

1. Program to show swap of two no’s without using third variable.

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
clrscr();
printf("enter value for a & b: ");
scanf("%d%d",&a,&b);
a=a+b;
b=a-b;
a=a-b;
printf("after swapping the value of a & b: %d %d",a,b);
getch();
}


2. Program to reverse a given number:

#include<stdio.h>
#include<conio.h>
void main()
{
int n,a,r=0;
clrscr();
printf("enter any no to get its reverse: ");
scanf("%d",&n);
while(n>=1)
{
a=n%10;
r=r*10+a;
n=n/10;
}
printf("reverse=%d",r);
getch();
}


3. Program to find greatest in 3 numbers.

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
clrscr();
printf("enter value of a, b & c: ");
scanf("%d%d%d",&a,&b,&c);
if((a>b)&&(a>c))
printf("a is greatest");
if((b>c)&&(b>a))
printf("b is greatest");
if((c>a)&&(c>b))
printf("c is greatest");
getch();
}


4. Program to find whether given no is even or odd.

#include<stdio.h>
#include<conio.h>
void main()
{
int n;
clrscr();
printf("enter any no: ");
scanf("%d",&n);
if(n%2==0)
printf("no is even");
else
printf("no is odd");
getch();
}


5. Program to display first 10 natural no & their sum.

#include<stdio.h>
#include<conio.h>
void main()
{
int i,sum=0;
clrscr();
for(i=1;i<=10;i++)
{
printf("%d no is= %d\n",i,I);
sum=sum+i;
}
printf("sum =%d",sum);
getch();
}


6. Program to find factorial of a number.

#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,fact=1;
clrscr();
printf("Enter any no: ");
scanf("%d",&n);
for(i=n;i>=1;i--)
{
fact=fact*i;
}
printf("Factorial=%d",fact);
getch();
}


7. Program to find whether given no is a prime no or not.

#include<stdio.h>
#include<conio.h>
void main()
{
int i,n,r=0;
clrscr();
printf("Enter any no: ");
scanf("%d",&n);
for(i=2;i<=n-1;i++)
{
if(n%i==0)
r=1;
break;
}
if(r==0)
printf("prime no");
else
printf("Not prime");
getch();
}


8. Program to add two number using pointers.

#include<stdio.h>
#include<conio.h>
void main()
{
int *p1,*p2,sum;
clrscr();
printf("enter two no's: ");
scanf("%d%d",&*p1,&*p2);
sum=*p1+*p2;
printf("sum=%d",sum);
getch();
}


9. Program to find multiplication of two matrices.

#include<stdio.h>
#include<conio.h>
void main()
{
int a[3][2],b[3][2],c[3][2],i,j;
clrscr();
printf("enter value for 1 matrix: ");
for(i=0;i<3;i++)
{
for(j=0;j<2;j++)
scanf("%d",&a[i][j]);
}
printf("enter value for 2 matrix: ");
for(i=0;i<3;i++)
{
for(j=0;j<2;j++)
scanf("%d",&b[i][j]);
}
for(i=0;i<3;i++)
{
for(j=0;j<2;j++)
c[i][j]=a[i][j]*b[i][j];
}
printf("matrix is\n");
for(i=0;i<3;i++)
{
for(j=0;j<2;j++)
{
printf(" %d ",c[i][j]);
}
printf("\n");
}
getch();
}


10. Program to reverse a number using pointer.

#include<stdio.h>
#include<conio.h>
void main()
{
int *n,a,r=0;
clrscr();
printf("enter any no to get its reverse: ");
scanf("%d",&*n);
while(*n>=1)
{
a=*n%10;
r=r*10+a;
*n=*n/10;
}
printf("reverse=%d",r);
getch();
}


11. Program to swap two numbers using functions.

#include<stdio.h>
#include<conio.h>
void main()
{
void swap(int,int);
int a,b,r;
clrscr();
printf("enter value for a&b: ");
scanf("%d%d",&a,&b);
swap(a,b);
getch();
}
void swap(int a,int b)
{
int temp;
temp=a;
a=b;
b=temp;
printf("after swapping the value for a & b is : %d %d",a,b);
}


12. Program to find factorial of a number using functions.

#include<stdio.h>
#include<conio.h>
void main()
{
int a,f;
int fact(int);
clrscr();
printf("enter a no: ");
scanf("%d",&a);
f=fact(a);
printf("factorial= %d",f);
getch();
}
int fact(int x)
{
int fac=1,i;
for(i=x;i>=1;i--)
fac=fac*i;
return(fac);
}


13. Program to show call by value..

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,swap();
clrscr();
a=5;
b=10;
printf("value of a=%d & value of b=%d before swap ",a,b);
swap(a,b);
printf("\nvalue of a =%d & b=%d after swap",a,b);
getch();
}
int swap(int x,int y)
{
int temp;
temp=x;
x=y;
y=temp;
}


14. Program to show call by reference.

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,*aa,*bb,swap();
clrscr();
a=5;
b=10;
aa=&a;
bb=&b;
printf("value of a= %d & value of b=%d before swap",a,b);
swap(aa,bb);
printf("\nvalue of a=%d & b=%d after swap",a,b);
getch();
}
int swap(int *x,int *y)
{
int temp;
temp=*x;
*x=*y;
*y=temp;
}


15. Program to find factorial of a number using recursion.

#include<stdio.h>
#include<conio.h>
void main()
{
int n;
clrscr();
printf("enter number: ");
scanf("%d",&n);
if(n<0)
printf("invalid number");
else
printf("%d!=%d",n,fact(n));
getch();
}
int fact(int x)
{
if(x==0)
return 1;
else
return(x*fact(x-1));
}


16. Program to find whether a string is palindrome or not.

#include<stdio.h>
#include<conio.h>
void main()
{
char s1[20],s2[20];
clrscr();
printf("enter a string: ");
scanf("%s",s1);
strcpy(s2,s1);
strrev(s2);
if(strcmp(s1,s2)==0)
printf("string is a palindrome");
else
printf("not a palindrome string");
getch();
}



No comments:

Post a Comment