C - EXAMPLE PROGRAMS
1. Program to print text
main()
{
clrscr();
printf(“HELLO WELCOME TO VIDYARTHI COMPUTERS”); printf(“Hanamkonda Warangal phone : 0870-2574900, 9849103344”); getch();
}
2. Program To Read Two Numbers And Print The Sum Of Given Two Numbers.
main()
{
int a,b, sum;
clrscr ();
printf (“ENTER VALUE FOR A ; “);
scanf (“%d”,&a);
printf(“ENTER VALUE FOR B ;”);
scanf(“%d”,&b);
sum=a+b;
printf(“Sum Of Given Two Numbers are %d”, sum);
getch();
}
3. Program To Accept Student Roll No, Marks in 3 Subjects and Calculate Total, Average and Print it.
main()
{ int r,b,c,d, tot, avg;
clrscr();
printf (“ENTER STUDENT RNO ; “);
scanf (“%d”,&r);
printf(“ENTER FIRST SUBJECT MARKS ;”);
scanf(“%d”,&b);
printf(“ENTER SECOND SUBJECT MARKS;”);
scanf(“%d”,&c);
printf(“ENTER THIRD SUBJECT MARKS ;”);
scanf(“%d”,&d); tot=b+c+d; avg=tot/3;
printf(“\n\n\t\t VIDYARTHI COMPUTERS – HANAMAKONDA \n\n”);
printf(“\t STUDENT RNO ; %d “,r);
printf(“\t FIRST SUBJECT MARKS ;%d “,b); printf(“\t SECOND SUBJECT MARKS ;%d “,C); printf(“\t THIRD SUBJECT MARKS ;%d “,d); printf(“\t AVERAGE MARKS ; %d”, avg);
getch();
}
4. Program To Read Three Numbers And Print The Biggest Of Given Three Numbers
main( )
{
int a,b,c,big=0;
clrscr( );
printf(“ENTER VALUE FOR A:”);
scanf(“%d”,&a);
printf(“ENTER VALUE FOR B:”);
scanf(“%d”,&b);
print(“ENTER VALUE FOR C:”);
scanf(“%d”,&c);
if (a>big) big=a ; if(b>big) big=b;
if (c>big)
big=c;
printf (“BIGGEST OF ABOVE GIVEN THREE NUMBER IS %d”,big)
getch( );
}
5. Program To Read A Number And Find Whether The Given Number Is Even Or Odd.
main()
{
int n,r;
clrscr();
printf(“ENTER A NUMBER ;”);
scanf(“%d”, &n);
r=n%2;
if(r= = 0)
printf(“the above given number is even number”);
else
printf(“the above given number is odd number”);
getch();
}
6. Program to accept a year and check whether the given year IS leap year or not.
main( )
{
int y;
clrscr( );
printf(“enter a year:”);
scanf(“%d”,&y);
if(y%4==0& &y%100!=0|| y%400==0); printf(“the above given year IS a leap year”); else
printf(“the above given year IS not a leap year”);
getch();
}
7. Individual Digits
main( )
{
int a,b,c,d;
clrscr( );
printf ( “ Enter a two digit number :”);
scanf (“ %d”, &a);
b=a/10; c=a%10; d=b+c;
printf (“sum of individual digits of given numbers id %”, d);
getch( );
}
8. Program to accept a three digit number and print the sum of individual digits.
main( )
{
int a,b,c,n, sum;
clrscr( );
printf (“ Enter a Three Digit Number:“);
scanf (“%d”,&n);
a=n/100;
b=( (n%100)/10); c=n%10; sum=a+b+c;
printf (“ Sum of Individual Digits of Given Numbers is %d”, Sum);
getch( );
}
9. Program to accept a number and check the given number is Armstrong or not.
main( )
{
int n, a, b, c, d;
clrscr( );
printf (“ Enter a Three Digit Number: “);
scanf (“%d”, &n); a=n/100; b=((n/10)%10); c=n%10;
d=a*a*a*+b*b*b +c*c*c;
if (n= =d)
printf (“The Given Number is Armstrong number”);
else
printf (“The Given Number is Not Armstrong number”);
getch( );
}
10. Program to print ODD numbers from 1 to 10
main( )
{
int i;
clrscr( );
for (i=1; i<=10; i+=2) printf(“%d\n”,i); getch( );
}
11. Program to print natural numbers from 1 to 10 in Reverse
main( )
{
int i;
clrscr( );
for (i=10; i>=1; i--) printf(“%d\n”,i); getch( );
}
12. Program to print sum of the natural numbers from 1 to 10.
main( )
{
int n,sum=0,i;
clrscr( );
for (i=1; i<=10; i++)
sum=sum+i;
printf(“sum of natural numbers from 1 to 10 is %d\n”,sum);
getch( );
}
13. Program to accept a number and print mathematical table of the given no.
main( )
{
int i,t;
clrscr( );
printf(“which table u want:”);
scanf(“%d”,&t);
for (i=1; i<=10; i++)
printf(“\n%d*%d=%d”,t,i,i*t);
getch( );
}
14. Program to print 1 to 10 mathematical tables .
main( )
{
int i,j;
clrscr( );
for (i=1; i<=10; i++) for(j=1;j<=10;j++) printf(“\n%d*%d=%d”,i,j,i*j); getch( );
}
15. Program to print fibonacci series .
main( )
{
int a=0,b=1,c=0,i; clrscr( ); printf(“%d”,a); printf(“\n%d”,b);
for (i=1; i<=10; i++)
{ c=a+b; printf(“\n%d”,c); a=b;
b=c;
}
getch( );
}
16. Program to print numeric pyramid
main()
{
int i,j; clrscr( ); for(i=1;i<=5;i++)
{ for(j=1;j<=i;j++) printf(“%d”,j); printf(“\n”);
}
getch();
}
17. Program to print numerical pyramid.
main( )
{
int i,j ,l,k=40; clrscr( ); for(i=1;i<=9;i+=2)
{
for(l=1;l<=k;l++) printf(“ “ ); for(j=1;j<=i;j++); printf(“%d”,j); printf(“\n”);
k=k-2;
}getch( );}
18. Program to print numerical diamond.
main( )
{
int i,j,l,n,s,k=40; clrscr( ); for(i=1;i<=9;i+=2)
{
for(l=1;l<=k;l++) printf(“ “); for(j=1;j<=i;j++) printf(“\n”);
k=k-2;
} k=k+4; for(n=7;n>=1;n-=2)
{ for(i=1;i<=k;i++) printf(“ “); for(s=1;s<n;s++) printf(“%d”,s); printf(“\n”); k=k+2;
}
getch( );
}
19. Program to print character pyramid.
main( )
{
char i,j; clrscr(); for(i=65;i<=70;i++)
{
for(j=65;j<=i;j++) printf(“%c”,j); printf(“\n”);
}
getch( );
}
20. Program to print character diamond.
main( )
{
char i,j,n,r; int s,sp=40; clrscr( );
for(i=65;i<=75;i+=2)
{ for(s=1;s<=sp;s++) printf(“ “); for(j=65;j<i;j++) printf(“%c”,j); printf(“\n”);
sp=sp-2;
} sp=sp+4; for(n=73;n>=65;n-=2)
{ for(s=1;s<=sp;s++) printf(“ “); for(r=65;r<=n;r++) printf(“%c”,r); sp=sp+2;
}
getch( );
}
21. Program to find biggest of two no by using ternary numbers
main( )
{
int a,b,big;
clrscr( );
printf(“enter value a”);
scanf(“%d”,&a);
printf(“enter the value of b”); scanf(“%d”,&b); big=(a>b)?a:b;
printf(“biggest of the given numbers IS %d”,big);
getch();
}
22. Program to find biggest of four no by using ternary numbers
main( )
{
int a,b,c,d,big;
clrscr( );
printf(“enter value a”);
scanf(“%d”,&a);
printf(“enter the value of b”);
scanf(“%d”,&b);
printf(“enter the value of c”);
scanf(“%d”,&c);
printf(“enter the value of d”); scanf(“%d”,&d); big=(a>b)?(a>c)?(a>d)?a:d:(c>d)?c:d:(b>c)?(b>d)?b:d:(c>d)?c:d; printf(“biggest of the given 4 numbers IS %d”,big);
getch();
}
23. Program to print smallest of four no by using ternary operators
main( )
{
int a,b,c,d,small;
clrscr( );
printf(“enter value a”);
scanf(“%d”,&a);
printf(“enter the value of b”);
scanf(“%d”,&b);
printf(“enter the value of c”);
scanf(“%d”,&c);
printf(“enter the value of d”); scanf(“%d”,&d); small=(a<b)?(a<c)?(a<d)?a:d:(c
printf(“biggest of the given 4 numbers IS %d”,small);
getch();
}
24. Program to accept a year and check the given year is leap or not by using ternary
main( )
{
int y,leap;
clrscr( );
printf(“enter any yr”);
scanf(“%d”,&y);
leap=(y%400= =0)?:(y%100!=0)?(y%4= =0)?1:0:0;
if(leap= =1)
printf(“ the given year is leap year”);
else
printf(“given year is not leap year);
getch( );
}
25. Program to accept a character in the uppercase and print in lower case.
main( )
{
char ch,c1;
clrscr( );
printf(“enter a cha in uppercase”);
ch=getchar();
c1=ch+32;
printf(“the given char in lowercasecase is”);
putchar(c1);
getch();
}
26. Program to accept a character in any case and print in another case.
main( )
{
char ch,c1;
clrscr( );
printf(“enter a char in anycase”);
ch=getchar();
if(ch>=65 && ch<=90)
c1=ch+32;
else
if(ch>=97 && ch<=122)
c1=ch-32;
printf(“the given char in anothercase IS”);
putchar(c1);
getch();
}
27. Program to natural number from 1 to 10 by using while loop.
main( )
{
int a=0; clrscr(); while( a<10)
{
a=a+1;
printf(“%d\n”,a);
}
getch();
}
28. Program to accept a string and print it by using the while loop.
main( )
{
char ch;
clrscr();
printf(“enter a string”); while(( ch=getchar( ))!=’\n’) putchar(ch);
getch();
}
29. Program to accept a string in upper case and print it by lower case.
main( )
{
char ch,c;
clrscr();
printf(“enter a string in upper case:”);
while(( ch=getchar( ))!=’\n’)
{
c=ch+32;
putchar(c);
}
printf(“ is in lower case”);
getch( );
}
30. Program to accept a string in any case and print it by another case.
main( )
{
char ch;
clrscr( );
printf(“enter a string :”);
while(( ch=getchar( ))!=’\n’)
{
if(ch>=’A’ && ch<=’Z’)
putchar(ch+32);
else
if(ch>=’a’ && ch<=’z’)
putchar(ch-32);
else putchar(ch);
}
printf(“ is the string”);
getch( );
}
31. Program to accept a string print each word in new line.
main( )
{
char ch;
clrscr( );
printf(“enter a string :”);
while(( ch=getchar( ))!=’\n’)
{ putchar(ch); if(ch= =’ ‘) printf(“\n”);
}
getch( );
}
32. Program to accept a string and count no of capital letters, no. of small letters and no. of special characters
main( )
{
char ch;
int c=0,s=0,s1=0;
clrscr( );
printf(“enter a string :”);
while(( ch=getchar( ))!=’\n’)
{
if(ch>=’A’&& ch>=’Z’)
c=c+1;
else
if(ch>=’a’&& ch>=’z’)
s=s+1; else s1=s1+1;
}
printf(“ no of capital letters are %d”,c); printf(“ no of smal1 letters are %d”,s); printf(“ no of special characters are %d”,s1); getch( );
}
33. Program to accept any single digit number and print it in words.
main( )
{
int n;
clrscr( );
printf(“enter a number :”); scanf(“%d “,&n); switch(n)
{
case 0: printf(“ZERO”);
break;
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(“please enter the number between 0 and 9”);
}
getch( );
}
34. Program to print prime numbers between 1 to 100
main( )
{
int n, i, check; clrscr(); for(i=1;i<=100;i++)
{
check=1; for(n=2;n<=i/2;n++) if(i%n= =0)
{ check=0; break;
}
if(check= =1)
printf(“\n %d is a prime”,i);
else
printf(“\n %d is not a prime”,i);
}
getch( );
}
35. Program to accept two numbers and print sum of two numbers by using functions
main( )
{
int a,b,c;
clrscr();
printf(“enter the value for a:”)
scanf(“%d”,&a);
printf(“enter the value for b:”)
scanf(“%d”,&b);
c=add(a,b);
printf(“sum of two numbers is %d”,c);
getch( );
}
int add(int x, int y)
{
int z; z=x+y; return z;
}
36. Program to accept a number and find factorial of given number
main( )
{
int n,f;
clrscr( );
printf(“enter a number:”)
scanf(“%d”,&n);
f= fact(n);
printf(“factorial value is %d”,f);
getch();
}
int fact(int n)
{
int i, fa=1; for(i=n;i>=1;i--) fa=fa*i;
return fa;
}
37. Program to accept a number and check the given number Armstrong or not
main( )
{
int n,arm;
clrscr();
printf(“enter any 3 digit number:”)
scanf(“%d”,&n); arm= armstrong(n); if(arm= =n)
printf(“%d is Armstrong number”,n);
else
printf(“%d not a Armstrong number”,n);
getch( );
}
int Armstrong (int n)
{
int a,b,c,d; a=n/100; b=((n/10)%10); c=n%10; d=a*a*a+b*b*b+c*c*c; return d;
}
38. Program to accept a number and print the sum of given and Reverse number
main( )
{
int a,b,n;
clrscr( );
printf(“enter a number:”)
scanf(“%d”,&n);
a=rev(n);
printf(“REVERSE OF A GIVEN NUMBER IS %d”,a);
b=add(n,a);
printf(“\n sum of a given and reverse number is %d”,b);
getch( );
}
int rev( int n)
{
int r,rev=0,s;
while(n>0)
{ r=n%10; rev=rev*10+r; n=n/10;
}
return rev;
}
int add(int n, int a)
{
return n+a;
}
39. Program to accept 10 numbers and print first five numbers in original order and print last five numbers in reverse order.
main( )
{
int i,a[10];
for(i=0;i<10;i++)
{
printf(“enter value for a[%d]”,i);
scanf(“%d”,&a[i]);
} for(i=0;i<=4;i++) printf(“\nA[%d]=%d”,i,a[i]); for(i=9;i>=5;i--) printf(“\nA[%d]=%d”,i,a[i]); getch( );
}
40. Program to accept a string and print the reverse of the given string by using for loop.
main( )
{
int i,j;
char name[80];
clrscr( );
printf(“ enter a string”);
gets(name);
for(i=0;i<80 && ((name [i]= getchar())!=’\n’);i++);
if(name[i]= =’\n’) name[i]=’\0’; for(j=i;j>=0;j--) putchar(name[j]);
printf(“is the reverse of given string”);
getch( );
}
No comments:
Post a Comment