You are on page 1of 16

C - PROGRAMS

1. Program To Read Two Numbers And Print The Sum Of Given Two Numbers.
# include <stdio.h>
# include <conio.h>
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();
}

2. Program To Accept Student Roll No, Marks in 3 Subjects and Calculate Total, Average and
Print it.
# include <stdio.h>
# include <conio.h>
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(\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 TOTAL :%d,tot);
printf(\t AVERAGE MARKS ; %d, avg);
getch();
}

3. Program To Read Three Numbers And Print The Biggest Of Given Three Numbers(**)
# include <stdio.h>
# include <conio.h>
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( );
}
4.Program To Read A Number And Find Whether The Given Number Is Even Or Odd.(**)
# include <stdio.h>
# include <conio.h>
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();
}
5. Program to accept a year and check whether the given year is Leap Year or not.
# include <stdio.h>
# include <conio.h>
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();
}

6. Program to accept a two digit number and print the sum of individual digits.
# include <stdio.h>
# include <conio.h>
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 is: %d, d);
getch( );
}
OUTPUT:
Enter a two digit number :45
sum of individual digits of given numbers is:9
7. Program to accept a three digit number and print the sum of individual digits.
# include <stdio.h>
# include <conio.h>
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( );
}
OUTPUT:
Enter a Three Digit Number:165
Sum of Individual Digits of Given Numbers is:11
8. Program to accept a number and check the given number is Armstrong or not.
# include <stdio.h>
# include <conio.h>
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( );
}
OUTPUT:
Enter a Three Digit Number:153
The Given Number is Armstrong number
Enter a Three Digit Number:143
The Given Number is Not Armstrong number
C PROGRAMS USING FOR LOOP
9. Program to print ODD numbers from 1 to 10 Using For loop
# include <stdio.h>
# include <conio.h>
main( )
{
int i;
clrscr( );
for (i=1; i<=10; i+=2)
printf(%d,i);
getch( );
}
OUTPUT:
13579
10.C Program to print natural numbers from 1 to 10 in Reverse
# include <stdio.h>
# include <conio.h>
main( )
{
int i;
clrscr( );
for (i=10; i>=1; i--)
printf(%d,i);
getch( );
}
OUTPUT
10987654321
11. Program to print sum of the natural numbers from 1 to 10.
# include <stdio.h>
# include <conio.h>
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,sum);
getch( );
}
OUTPUT
sum of natural numbers from 1 to 10 is 55

12. Program to print fibonacci series (upto 10 nos).


# include <stdio.h>
# include <conio.h>
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( );
}
OUTPUT
011235813213455

13. Program to accept a string and print the reverse of the given string by using for loop.
# include <stdio.h>
# include <conio.h>
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( );
}
OUTPUT
Enter a string
ram
mar is the reverse of given string

IF
Program to find whether given no is prime or not
#include <stdio.h>
#include <math.h>
void main( )
{
int i, n, prime=1;
printf("Enter a number");
scanf("%d", &n);
for( i=2; i<= n-1; i++)
{
if( n % i = =0 ) {
prime =0;
break;
}
}
if( prime )
printf("%d is prime number !\n", n);
else
printf("%d isnt prime number!\n", n);
}
14. Program to find biggest of two no by using ternary operator( ternary operator ? : )
# include <stdio.h>
# include <conio.h>
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();
}
15. Program to find biggest of four no by using ternary operator
# include <stdio.h>
# include <conio.h>
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();
}

16 . Program to accept a character in the uppercase and print in lower case.


# include <stdio.h>
# include <conio.h>
main( )
{
char ch,c1;
clrscr( );
printf(enter a char in uppercase);
ch=getchar();
c1=ch+32;
printf(the given char in lowercasecase is);
putchar(c1);
getch();
}
Output:
Enter a char in upper case B
The given char in lower case is b

17. Program to accept a character in any case and print in another case.
# include <stdio.h>
# include <conio.h>
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();
}

PROGRAMS USING WHILE LOOP


18. Program to find natural number from 1 to 10 by using while loop.
# include <stdio.h>
# include <conio.h>
main( )
{
int a=0;
clrscr();
while( a<10)
{
a=a+1;
printf(%d\n,a);
}
getch();
}

19. Program to accept a string and print it by using the while loop.
# include <stdio.h>
# include <conio.h>
main( )
{
char ch;
clrscr();
printf(enter a string);
while(( ch=getchar( ))!=\n)
putchar(ch);
getch();
}

20.C Program to accept a string in upper case and print it by lower case.
# include <stdio.h>
# include <conio.h>
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( );
}

SWITCH CASE
21. Program to accept any single digit number and print it in words .
# include <stdio.h>
# include <conio.h>
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( );
}

21. b Program to count the number of words, characters, alphabets, vowels, consonants and
digit in a line
of text.
#include<stdio.h>
#include<conio.h>
main( )
{
int noa=0,nob=0,noc=0,nov=0,now=0,noch=0,l,I;
char ch,s[100];
clrscr( );
printf(enter 2 lines of text);
gets(s);
l=strlen(s);
for(i=0;i<1;i++)
{
switch(s[i])
{
case a:
case e:
case i:
case o:
case u:
case A:
case E:
case I:
case O:
case U:
nov++;
break;
}
if(isalpha(s[i]))
noa++;
if(isdigit(s[i]))
nod++;
if(noa[i]== ) && (noa[i+1]!= )
now++;
}
noch=l-nob;
noc=noa-nov;
printf(total no of words %d,now);
printf(total no of characters(without blanks)%d,noch);
printf(total no of characters(including blanks)%d,l);
printf(total no of alphabets %d,noa);
printf(total no of vowels %d,nov);
printf(total no of characters %d,noc);
printf(total no of digits %d,nod);
getch( );
}

FUNCTIONS
22. Program to accept two numbers and print sum of two numbers by using functions
# include <stdio.h>
# include <conio.h>
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;
}

23. Program to accept a number and find factorial of given number using Function
# include <stdio.h>
# include <conio.h>
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;
}

24. Program to accept a number and check the given number Armstrong or not using Functions
# include <stdio.h>
# include <conio.h>
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;
}
25. Program to accept a string and find the length of the given string by using functions
# include <stdio.h>
# include <conio.h>
int getline(char str[]);
main( )
{
char str[80];
int length;
clrscr( );
printf( enter a string);
length=getline(str);
printf(length of the given string is %d,length);
getch ( );
}
int getline(char str[])
{
int i;
for(i=0;i<80&&((str[i]=getchar( ))!=\n); i++);
if(str[i]= =\n)
str[i]=\0;
return i;
}

ARRAYS
26. Program to accept values into two dimensional array and print .
# include <stdio.h>
# include <conio.h>
main( )
{
int a[3][3],i,j;
clrscr( );
for(i=0;i<=2;i++)
for(j=0;j<=2;j++)
{
printf( enter the value for a[%d][%d] :,i,j);
scanf(%d,&a[i][j]);
}
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
printf( %d:,a[i][j]);
printf(\n);
}
getch( );
}

27.C Program to print upper triangle .


# include <stdio.h>
# include <conio.h>
main( )
{
int a[4][4],i,j,c;
clrscr( );
printf( enter which no u want);
scanf(%d,&c);
for(i=0;i<4;i++)
for(j=0;j<4;j++)
if(i<j)
a[i][j]=c;
else
a[i][j]=0;
for(i=0;i<4;i++)
for(j=0;j<4;j++)
{
printf( %d:,a[i][j]);
printf(\n);
}
getch( );
}

28. Programs to multiply two Matrices


# include <stdio.h>
# include <conio.h>
main( )
{
int a[10][10],b[10][10],c[10],[10],i,j,m,n,p,q,k;
clrscr( );
printf(enter the size of first matrices);
scanf(%d%d,&m,&n);
printf(enter the size of second matrix);
scanf(%d%d,&p,&q);
if(n==p)
{
printf(enter first matrices elements);
for(i=1;i<m;i++)
for(j=1;j<n;j++)
scanf(%d,&a[i][j]);
printf(enter second matrix elements);
for(i=1;i<p;i++)
for(j=1;j<q;j++)
scanf(%d,&b[i][j]);
for(i=1;i<m;i++)
for(j=1;j<n;j++)
{
c[i][j]=0;
for(k=1;k<n;k++)
c[i][j]=c[i][j]+a[i][k]*b[k][j];
}
printf(the multiplication matrix is);
for(i=1;i<m;i++)
{
for(j=1;j<n;j++)
print(%2d,c[i][j]);
printf(\n);
}} else
printf(multiplication is not possible);
getch( );
}

29. Program to Swap values using pointers.(Call By Reference)


# include <stdio.h>
# include <conio.h>
void interchange(int *a,int *b);
main( )
{
int x,y;
clrscr( );
printf(enter values of x and y);
scanf(%d%d,&x,&y);
interchange(&x,&y);
}
void interchange(a,b)
int *a,*b;
{
int c;
c=*a;
*a=*b;
*b=c;
printf(x=%d y=%d,*a,*b);
getch( );
}
30. Program to Swap values .(Call By Value)
# include <stdio.h>
# include <conio.h>
void interchange(int a,int b);
main( )
{
int x,y;
clrscr( );
printf(enter values of x and y);
scanf(%d%d,&x,&y);
interchange(&x,&y);
}
void interchange(a,b)
int a,b;
{
int c;
c=a;
a=b;
b=c;
printf(x=%d y=%d,a,b);
getch( );
}

31. Program to accept two numbers and print the sum of given two numbers by using pointers
# include <stdio.h>
# include <conio.h>
main( )
{
int a, b,c;
clrscr( );
a=10;
b=20;
c=*(&a)+*(&b);
printf(%d,c);
getch( );
}
32. C Program for printing book Details(like book name,quantity& price) using structures
# include<stdio.h>
# include<conio.h>
main( )
{
struct book
{
char name[50];
int quantity;
float price;
};
struct book b1,b2,b3;
main( )
{ clrscr( );
printf(enter name,quantity,price);
scanf(%s%d%f,&b1.name,&b1.quantity,&b1.price);
printf(enter name,quantity,price);
scanf(%s%d%f,&b1.name,&b1.quantity,&b1.price);
printf(enter name,quantity,price);
scanf(%s%d%f,&b3.name,&b3.quantity,&b3.price);
printf(the details are);
printf(\n %s%d%f,b1.name,b1.quantity,b1.price);
printf(\n %s%d%f,b2.name,e,b2.quantity,b2.price);
printf(\n %s%d%f,b3.name,b3.quanity,b3.price);
getch( );
}

You might also like