Monday, 1 October 2012

Sample Programs





 

1.  write a program to find sum of first n odd and even numbers.

# include<stdio.h>
main()
{
int i,j,n,o=0,e=0;
clrscr();
printf("enter n value");
scanf("%d",&n);
for(i=0;i<=n;i++)
{
if (i%2==0)
e+=i;
else
o+=i;
}
printf ("even no:%d",e);
printf ("odd no:%d",o);
getch();
}

2. write a program to find the sum of individual digits.

#include <stdio.h>
main()
{
     int x,n,r,sum=0;
     clrscr();
     printf("enter n value ");
     scanf("%d",&n);
     x=n;
     while(n>0)
                {
               r=n%10;
               sum=sum+r;
               n=n/10;
                 }
    printf("the sum of individual digits %d is %d",x,sum);
    getch();
      }


3. write a program to find weather the given no. is perfect or not.

 # include <stdio.h>
main()
{
int n,i,s=0;
clrscr();
printf("enter value n :");
scanf("%d",&n);
for(i=1;i<n;i++)
{
if(n%i==0)
s=s+i;
}
if(s==n)
printf("given no is perfect");
else
printf("given no is not perfect");
getch();
}

4. write a program to print fibonacci series.

#include<stdio.h>
 main()
{
      int n,f=0,f1=0,f2=1,i;
      clrscr();
      printf("enter n value :");
      scanf("%d",&n);
      printf("%d %d",f1,f2);
      for(i=1;i<=n;i++)
                 {
              f=f1+f2;
              f1=f2;
              f2=f;
              printf("%d",f);
                 }
      getch();
}

5. Write a program to find the roots of the Quadratic equation..

 # include<stdio.h>
main()
{
int root1,root2,a,b,c,d;
clrscr();
printf("enter the values of a,b,c\n");
scanf("%d%d%d",&a,&b,&c);

d=b*b-4*a*c;
if(d==0)
{
printf("the roots are equal\n");
root1=-b/2*a;
root2=-b/2*a;
printf("root1=%d\nroot2=%d\n",root1,root2);
}
else
{
if(d>0)
{
printf("the roots are real and differant\n");
root1=-b+sqrt(d)/2*a;
root2=-b-sqrt(d)/2*a;
printf("root1=%d\nroot2=%d\n",root1,root2);
}
else
printf("the roots are imaginary");
}

6. write a program to find ncr values.

# include <stdio.h>
main()
{
      int r,n,i,f1=1,f2=1,f3=1;
      float ncr;
      clrscr();
      printf("enter the n,r value");
      scanf("%d %d",&n,&r);
      for(i=2;i<=n;i++)
                 {
                 f1=f1*i;
                  }
      for(i=2;i<=r;i++)
                 {
                f2=f2*i;
                 }
      for(i=2;i<=n-r;i++)
                 {
     f3=f3*i;
                 }
     ncr=f1/(f2*f3);
     printf("the ncr value is:%f",ncr);
     getch();
            }



7. write a program to find weather the given no. is prime or not.

#include<stdio.h>
main()
{
      int n,i,m=0;
      clrscr();
      printf("enter n value");
      scanf("%d",&n);
      for(i=2;i<=n-1;i++)
                 {
                if(n%i==0)
                m=1;
                 }
     if(m==0)
     printf("Given no. is prime");
     else
     printf("Given no.is not prime");
     getch();
}

8. write a program to find l.c.m of the given no. using function.

#include<stdio.h>
main()
{
int x,y;
clrscr();
printf("enter any value");
scanf("%d %d",&x,&y);
printf("the L.C.M is=%d",lcm(x,y));
getch();
}
lcm(x,y)
int x,y;
{
int r=1,lc,l=x*y;
r=x%y;
while(r!=0)
{
x=y;
y=r;
r=x%y;
}
lc=l/y;
return(lc);
}


9. write a program to find gcd of given two numbers.

#include<stdio.h>
main()
{
int n,m,r;
clrscr();
printf("enter m,n value");
scanf("%d %d",&m,&n);
r=m%n;
while(r!=0)
{
m=n;
n=r;
r=m%n;
}
printf("gcd ofgiven no=%d",n);
getch();
}

10. write a program to perform the arthematic operators.

#include<stdio.h>
main()
{
int a,b,d,a1;
char c;
clrscr();
printf("enter a,b no:");
scanf("%d %d",&a,&b);
printf("enter the value of +,-,*,/ \n");
scanf("%s",&c);
a1=c;
switch(c)
{
case '+'             :           d=a+b;
             break;
case '-'              :           d=a-b;
             break;
case '*'             :            d=a*b;
             break;
case '/'              :           d=a/b;
             break;
default             :           d=a%b;
}
printf("the result of a and b is= %d %c %d=%d",a,a1,b,d);
getch();
            }


11. write a program to find the reverse order of the given number  and chech if it is polindrome or not.

#include<stdio.h>
main()
{
int n,x,r,s=0;
clrscr();
printf("enter a no:");
scanf("%d",&n);
x=n;
while(n>0)
{
r=n%10;
s=s*10+r;
n=n/10;
}
printf("%d",s);
if(s==x)
printf("the given no is polindrum");
else
printf("the given no is not polindrum");
getch();
}

12. write a program to find factorial of given number.

#include<stdio.h>
main()
{
     int i,n,s=1;
     clrscr();
     printf("enter n value");
     scanf("%d",&n);
     for(i=1;i<=n;i++)
    {
              s=s*i;
     }
     printf("factorial is:%d",s);
     getch();
}

13. write a program to find the factors of the given number.

#include<stdio.h>
main()
{
     int n,i;
     clrscr();
     printf("enter n value:");

     scanf("%d",&n);
     printf(" the factors of the given no:");
     for(i=1;i<=n;i++)
    {
                if(n%i==0)
                printf("%d ",i);
     }
    getch();
}

14. Write a program to print the given number to words.

#include <stdio.h>
main()
{
int m=0,n;
printf("enter the value of n\n");
scanf("%d",&n);
while (n!=0)
{
m=m*10+n%10;
n/=10;
}
while (m!=0)
{
switch(m%10)
{
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;
default: printf("zero ");
break;
}
m/=10;
}
getch();
}

15. write a program to find  to print stars in altrenate lines.

#include<stdio.h>
main()
{
int i,j,k,n,x=0;
clrscr();
printf("enter any no:");
scanf("%d",&n);
x=n+2;
for(i=1;i<=n;i++)
{
for(j=1;j<=x;j++)
printf(" ");
for(k=1;k<=2*i-1;k++)
printf("*");
x=x-1;
printf("\n");
}
}

16. write a program to find the largest element of given list.

#include<stdio.h>
main()
{
int a[10],n,i,lar;
clrscr();
printf("enter the value of n  :");
scanf("%d",&n);
printf("enter the elements in array\n");
for(i=0;i<=n-1;i++)
{
scanf("%d",&a[i]);
}
lar=a[0];
for(i=1;i<n;i++)
{
if(lar<a[i])
lar=a[i];
}
printf("largest of the element=%d",lar);
getch();
}


17. write a program to find the smallest element of given list.

#include<stdio.h>
main()
{
int a[10],n,i,small;
clrscr();
printf("enter the value of n  :");
scanf("%d",&n);
printf("enter the elements in array\n");
for(i=0;i<=n-1;i++)
{
scanf("%d",&a[i]);
}
small=a[0];
for(i=1;i<n;i++)
{
if(small>a[i])
small=a[i];
}
printf("smallest of the element=%d",small);
getch();
}

18. write a program to find the addition of two matrix.

#include<stdio.h>
main() 
{
int i,j,r,c,x[10][10],y[10][10],z[10][10];
clrscr();
printf("enter 1st matrix row & column");
scanf(" %d  %d",&r,&c);
for(i=1;i<=r;i++)
{
for(j=1;j<=c;j++)
{
gotoxy(11*i,2*j);
scanf("%d", &x[i][j]);
}
printf("\n");
}
printf("enter elmentes of 2nd matrix \n");
for(i=1;i<=r;i++)
{
for(j=1;j<=c;j++)
{
gotoxy(11*i,9*j);
scanf("%d",&y[i][j]);
}
}
for(i=1;i<=r;i++)
{
for(j=1;j<=c;j++)
{
z[i][j]=(x[i][j]+y[i][j]);
printf("\n");
                                    }
}
printf("addition matrix\n");
for(i=1;i<=r;i++)
{
for(j=1;j<=c;j++)
printf("%5d",z[i][j]);
printf("\n");
}
getch();
}

19. write a program to find the substraction of two matrix.

#include<stdio.h>
main()
{
int i,j,r,c,x[10][10],y[10][10],z[10][10];
clrscr();
printf("enter 1st matrix row & column");
scanf(" %d  %d",&r,&c);
for(i=1;i<=r;i++)
{
for(j=1;j<=c;j++)
{
gotoxy(11*i,2*j);
scanf("%d",& x[i][j]);
}
printf("\n");
}
printf("enter 2nd matrix row & column");
for(i=1;i<=r;i++)
{
for(j=1;j<=c;j++)
{
gotoxy(11*i,9*j);
scanf("%d",& y[i][j]);
}
printf("\n");
                        }
for(i=1;i<=r;i++)
{
for(j=1;j<=c;j++)
{
z[i][j]=(x[i][j]-y[i][j]);
}
printf("\n");
}
printf("substraction matrix\n");
for(i=1;i<=r;i++)
{
for(j=1;j<=c;j++)
printf("%5d",z[i][j]);
printf("\n");
}
getch();
}

20. write a program to find the multiplication of two matrix.

#include<stdio.h>
main()
{
int i,j,r,n,n1,m,p,k,c,x[10][10],y[10][10],z[10][10];
clrscr();
printf("enter 1st matrix row & column size");
scanf(" %d  %d",&r,&c);
for(i=1;i<=r;i++)
{
for(j=1;j<=c;j++)
{
gotoxy(11*i,2*j);
scanf("%d",& x[i][j]);
}
printf("\n");
}
printf("enter 2nd matrix row & column size");
scanf(" %d  %d",&r,&c);
for(i=1;i<=r;i++)
{
for(j=1;j<=c;j++)
{
gotoxy(11*i,9*j);
scanf("%d",& y[i][j]);
}
printf("\n");
}
if(c==r)
{
for(i=1;i<=r;i++)
{
for(j=1;j<=c;j++)
{
z[i][j]=0;
for(k=1;k<=r;k++)
{
z[i][j]=z[i][j]+(x[i][j]*y[i][j]);
}
}
}
}
else
{
printf("multiplication matrix is not possible");
exit();
}
printf("multiplication matrix\n");
for(i=1;i<=r;i++)
{
for(j=1;j<=c;j++)
{
printf("%5d",z[i][j]);
}
printf("\n");
}
getch();
}

21. write a program to find the transpose of the given matrix.

#include<stdio.h>
main()
{
int x[10][10],y[10][10],i,j,r,c;
clrscr();
printf("enter rows & columns");
scanf("%d %d",&r,&c);
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
gotoxy(10+3*j,2+2*i);
scanf("%d",&x[i][j]);
}
}
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
y[j][i]=x[i][j];
}
}
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
gotoxy(40+3*j,2+2*i);
printf("%d",y[i][j]);
}
}
getch();
}

22. write a program to find the element by binary search.

# include <stdio.h>
main()
{
int a[80],n,i,key,low,high,mid,loc;
clrscr();
printf("enter the no. of elements in the list");
scanf("%d",&n);
high=n;
low=1;
mid=(low+high)/2;
printf("enter the elements in the list\n");
for (i=1;i<=n;i++)
{
scanf("%d",&a[i]);
}
printf("enter the key elements");
scanf("%d",&key);
while((a[mid]!=key)&&(low<=high))
{
if(a[mid]<key)
low=mid+1;
else
high=mid-1;
mid=(low+high)/2;
}
if(a[mid]==key)
loc=mid;
else
loc=0;
if(loc==0)
printf("the searching is unsuccessful");
else
printf("the searching is successful");
getch();
}



23. write a program to find the element by linear search.

# include <stdio.h>
main()
{
int a[80],n,i,key;
clrscr();
printf("enter the no. of elements in the list");
scanf("%d",&n);
printf("enter the elements in the list\n");
for (i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("enter the key elements");
scanf("%d",&key);
for(i=0;i<n;i++)
{
if(a[i]==key)
{
printf("the searching is successful");
goto abc;
}
}
printf("the searching is unsuccessful");
abc:
getch();
}

24. write a program to sort the given elements in ascending order.

#include<stdio.h>
main()
{
int a[10],t,n,i,j;
clrscr();
printf("enter the elements in an array \n");
scanf("%d",&n);
printf("enter the elements of array\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n-1;i++)
                        {
for(j=i+1;j<n;j++)
{
if(a[i]>a[j])
{
t=a[i];
a[i]=a[j];
a[j]=t;
}
}
}
printf("the ascending order is...\n");
for(i=0;i<n;i++)
printf("%d\n",a[i]);
getch();
}

25. write a program to sort the elements in descending order.

#include<stdio.h>
main()
{
int a[40],i,j,n,t;
clrscr();
printf("enter the elements in an array\n");
scanf("%d",&n);
printf("enter the elements of array\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
if(a[i]<a[j])
{
t=a[i];
a[i]=a[j];
a[j]=t;
}
}
}
printf("the decending order is .....\n");
for(i=0;i<n;i++)
printf("%d\n",a[i]);
getch();
}

26. write a program to find ascending order in bubble sort.

# include <stdio.h>
main()
{
int a[80],n,i,j,temp;
clrscr();
printf("enter the no. of elements in the list");
scanf("%d",&n);
printf("enter the elements in the list\n");
for (i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
printf("the buble sort is=\n");
for(i=0;i<n;i++)
{
printf("%d\n",a[i]);
}
getch();
}

27. write a program to find string length of given string.

#include<stdio.h>
#include<string.h>
main()
{
int n;
char s[80];
clrscr();
printf("enter the string :");
gets(s);
n=strlen(s);
printf("string length  is : %d",n);
getch();
}

28. write a program to concordinate two strings.

            #include<stdio.h>
#include<string.h>
main()
{
char x[80],y[80];
clrscr();
printf("enter 1st string");
gets(x);
printf("enter 2nd string");
gets(y);
strcat(x,y);
printf("after concordinate x & y the result is=%s",x);
getch();
}

29. write a program to compare the two strings.

            #include<stdio.h>
            #include<string.h>
main()
{
char x[10],y[10];
int z;
clrscr();
printf(" enter the first string  :");
gets(x);
printf(" enter the second string :");
gets(y);
z=strcmp(x,y);
if(z==0)
printf("the string are same");
else
if(z>0)
printf("first string>second string");
else
printf("first string<second string");
getch();
}

30. write a program to find to check weather the given character is in the given string.

#include<stdio.h>
main()
{
char ch,x[10];
int i;
clrscr();
printf("enter the string :");
scanf("%s",x);
printf("enter the charater :");
scanf("%s",&ch);
for(i=0;i<=26;i++)
{
if(ch==x[i])
{
printf("the charater is found  ");
}
}
getch(); }

31. write a program to copy one string into another.

#include<stdio.h>
main()
{
char x[80],y[80];
clrscr();
printf("enter 1st string");
gets(x);
printf("enter 2nd string");
gets(y);
strcpy(x,y);
printf("after copying x & y the result is=%s",x);
getch();
}

32. write a program to find the reverse of given number by funtion.

#include<stdio.h>
main()
{
int n,fact();
clrscr();
printf("enter the n value:");
scanf("%d",&n);
printf("the reverse of given no=%d",reverse(n));
getch();
}
reverse(n)
int n;
{
int m=0;
while(m!=0)
{
m=m*10+m%10;
m/=10;
}
return(m);
}

33. write a program to find to enter employee details using structers.

#include<stdio.h>
struct emp
{
char name[100];
int age;
float b;
}e;
main()
{
char another='y';
FILE *fp;
clrscr();
fp=fopen("emp","w");
while(another== 'y')
{
printf("\n\n enter name,age,basic\n");
scanf("%s %d %f",e.name,&e.age,&e.b);
fprintf(fp,"%s %d  %f",e.name,e.age,e.b);
printf("add another record y/n  :");
fflush(stdin);
another=getche();
}
fclose(fp);
}

34. Write a program to create a file and enter data into it and display data on screen.

#include<stdio.h>
main()
{
FILE *fp1;
int n,i,num;
fp1=fopen("DATA.DAT" ,"w");
printf("enter the no. of file componants\n");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
scanf("%d",&num);
putw(num,fp1);
}
fclose(fp1);
fp1=fopen("DATA.DAT","r");
while(getw(fp1)!=FEOF)
{
num=getw(fp1);
printf("the file componant is =%d\n",num);
}
fclose(fp1);
   }

35. Write a program to create a file named as “DATA” having file components belongs  to the integer type ,to read file components from the file if it is odd number put into file name “ODD”,otherwise put into file name “EVEN”.

# include<STDIO.H>
main()
{
FILE *fp1,*fp2,*fp3;
int n,i,num;
clrscr();
fp1=fopen("data","w");
printf("enter the no of elements in the file\n");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
printf("enter the number\n");
scanf("%d",&num);    
putw(num,fp1);
}
fclose(fp1);
fp1=fopen("data","r");
fp2=fopen("odd","w");
fp3=fopen("even","w");
while((num=getw(fp1))!=EOF)
{
if(num%2==0)
putw(num,fp3);
else
putw(num,fp2);
}
fclose(fp1);
fclose(fp2);
fclose(fp3);
fp1=fopen("data","r");
fp2=fopen("odd","r");
fp3=fopen("even","r");
while((num=getw(fp1))!=EOF)
printf("\n%d",num);
while((num=getw(fp3))!=EOF)
printf("\n%d\n",num);
while((num=getw(fp2))!=EOF)
printf("\n%d",num);
fclose(fp1);
fclose(fp2);
fclose(fp3);
getch();
}

36 . Write  a program to count no.of charectors,no of tabs,no.of spaces,no.of new lines.

# include<stdio.h>
main()
{
FILE *fp;
char ch;
int nt=0,nl=0,nc=0,nb=0;
fp=fopen("mura3.c","r");
clrscr();
while(1)
{
ch=getc(fp);
if (ch==EOF)
break;
nc++;
if(ch== ' ')
++nb;
if(ch== '\n')
++nl;
if(ch== '\t')
++nt;
}
fclose(fp);
printf("the no of charectors=%d\n",nc);
printf("the no of blankspaces=%d\n",nb);
printf("the no of new lines=%d\n",nl);
printf("the no of tabspaces=%d\n",nt);
}

37. write a simple program on graphics.

#include<stdio.h>
#include<graphics.h>
main()
{
int i,j;
clrscr();
detectgraph(&i,&j);
initgraph(&i,&j," ");
for(i=160;i<170;i++)
circle(i,100,90);
for(i=270;i<280;i++)
circle(i,100,90);
for(i=380;i<390;i++)
circle(i,100,90);
for(i=190;i<200;i++)
circle(i,190,90);
for(i=350;i<360;i++)
circle(i,190,90);
settextstyle(4,HORIZ_DIR,9);
outtextxy(30,300,"OLYMPICS");
getch();
}

38. write a program on graphics to form a love symbol.

#include<graphics.h>
#include<conio.h>
main()
{
int d=VGA,m=VGAHI;
initgraph(&d,&m," ");
clrscr();
arc(getmaxx()/2+1,getmaxy()/2,-370,180,41);
arc(getmaxx()/2+9,getmaxy()/2,-370,180,50);
arc(getmaxx()/2-80,getmaxy()/2,-370,180,40);
line(200,240,272,360);
line(360,248,272,360);
line(376,249,272,360);
line(175,340,235,300);
line(getmaxx()/2-40,280,400,210);
line(375,210,400,210);
line(390,230,400,210);
circle(200,175,10);
textcolor(6);
setbkcolor(4);
outtextxy(196,175,"S");
circle(240,175,10);
outtextxy(236,175,"H");
circle(280,175,10);
outtextxy(276,175,"I");
circle(320,175,10);
outtextxy(316,175,"V");
circle(360,175,10);
outtextxy(356,175,"A");
circle(400,240,10);
outtextxy(396,240,"L");
circle(400,270,10);
outtextxy(396,270,"O");
circle(400,300,10);
outtextxy(396,300,"V");
circle(400,330,10);
outtextxy(396,330,"E");
circle(400,360,10);
outtextxy(396,360,"S");
circle(440,375,10);
outtextxy(436,375,"U");
getch();
}


39. write a program on graphics to generate a puzzle.

#include<stdio.h>
main()
{
int x[3][3]={4,7,1,3,6,5,2,8,0};
int col=30,row=12;
int i,j,c;
clrscr();
for(i=0;i<3;++i)
{
for(j=0;j<3;++j)
{
gotoxy(col+3*j,row+2*i);
if(x[i][j]!=0)
printf("%d",x[i][j]);
}
}
i=2;
j=2;
for(;;)
{
c=bioskey(0);
if(c==20480 && i>0)
{
--i;
x[i+1][j]=x[i][j];
x[i][j]=0;
gotoxy(col+3*j,row+2*i);
printf(" ");
gotoxy(col+3*j,row+2*(i+1));
printf("%d",x[i+1][j]);
gotoxy(col+3*j,row+2*i);
}
else
if(c==18432 && i<2)
{
++i;
                                                            x[i-1][j]=x[i][j];
x[i][j]=0;
gotoxy(col+3*j,row+2*i);
printf(" ");
gotoxy(col+3*j,row+2*(i-1));
printf("%d",x[i-1][j]);
gotoxy(col+3*j,row+2*i);
}
else
if(c==19712 && j>0)
{
--j;
x[i][j+i]=x[i][j];
x[i][j]=0;
gotoxy(col+3*j,row+2*i);
printf(" ");
gotoxy(col+3*(j+1),row+2*i);
printf("%d",x[i][j+1]);
gotoxy(col+3*j,row+2*i);
}
else
if(c==19200 && j<2)
{
++j;
x[i][j-i]=x[i][j];
x[i][j]=0;
gotoxy(col+3*j,row+2*i);
printf(" ");
gotoxy(col+3*(j-1),row+2*i);
printf("%d",x[i][j-1]);
gotoxy(col+3*j,row+2*i);
}
}
}

40.    Write a program to generate a scrolling marquee.

#include<stdio.h>
#include<graphics.h>
#include<alloc.h>
#include<conio.h>
#include<time.h>
#include<dos.h>
main()
/*WELCOME()*/
{
int gm,gd=DETECT;
int x=640,y=210,j=1;
initgraph(&gd,&gm,"");
settextstyle(7,0,8);
while(!kbhit())
{
outtextxy(x,y,"WELCOME TO MY PERSONAL BOOK OF C");
setcolor(LIGHTRED);
outtextxy(x-1,y,"WELCOME TO MY PERSONAL BOOK OF C");
setcolor(BLACK);
if(x<=-785)
{
x=640;
}
x=x-1;
}
return 0;
}




No comments:

Post a Comment