C-programs with output
/*c-program to find largest among three nos.
using logical AND*/
#include<stdio.h>
#include<conio.h>
void main ()
{
int a,b,c;
printf("Enter values of a, b and c");
scanf("%d%d%d",&a,&b,&c);
if((a>b)&&(a>c))
printf("%d is largest",a);
if((c>a)&&(c>b))
printf("%d is largest",c);
getch();
}
/*close of main()function*/
/*output */
Enter values of a, b and c1 2 3
3 is largest
Enter values of a, b and c7 8 9
9 is largest
Enter values of a, b and c11 15 23
23 is largest
Enter values of a, b and c1 6 9
9 is largest
/*c-program for finding the sum of series
of 1 to 1/(10!)*/
#include<stdio.h>
#include<conio.h>
void main()
{
float i,sum,fact;
for(i=1,sum=0,fact=1;i<=10;i++)
{
fact=fact*i;
sum=sum+(1/fact);
}
printf("sum=%f",sum);
getch();
}
/*output*/
sum=1.718282
/*c program to find sum of(1/1) to
(1/10)*/
#include<stdio.h>
#include<conio.h>
void main()
{
float i,sum;
for(i=1,sum=0;i<=10;i++)
{
sum=sum+(1/i);
}
printf("sum=%f",sum);
getch();
}
/*output*/
sum=2.928968
/*c program for addition of1-5 numbers
using while loop*/
#include<stdio.h>
#include<conio.h>
void main()
{
int sum=0,i=1;
while(i<=5)
{
sum=sum+i;
i++;
}
printf("addition=%d",sum);
getch();
}
/*output*/
addition=15
/* c-program to find table from 1 to 10*/
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j;
for(i=1;i<=10;i++)
{
for(j=1;j<=10;j++)
{
printf("%dt",i*j);
}
printf("n");
}
getch();
}
/*Output*/
1 2 3 4 5 6 7 8 9 10
2 4 6 8 10 12 14 16 18 20
3 6 9 12 15 18 21 24 27 30
4 8 12 16 20 24 28 32 36 40
5 10 15 20 25 30 35 40 45 50
6 12 18 24 30 36 42 48 54 60
7 14 21 28 35 42 49 56 63 70
8 16 24 32 40 48 56 64 72 80
9 18 27 36 45 54 63 72 81 90
10 20 30 40 50 60 70 80 90 100
/*c program for goto in unconditional
branching or taking square of a no.*/
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
clrscr();
start:
printf("enter the value of a");
scanf("%d",&a);
b=a*a;
if(b>50)
{
goto start;
}
printf("b=%d",b);
getch();
}
/*Output*/
enter the value of a5
b=25
/*c-program of to find largest among 3 number using
if..else laddar or nested if..else*/
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
printf("enter the values of a,b,c");
scanf("%d%d%d", &a,&b,&c);
if(a>c)
{
if(a>c)
printf("%d is largest",a);
else
printf("%d is largest",c);
}
else
{
if(b>c)
printf("%d is largest",b);
else
printf("%d is largest",c);
}
getch();
}
/*output*/
enter the values of a,b,c
1 4 0
4 is largest
enter the values of a,b,c
3 4 5
5 is largest
/*c-program for addition oftwo nos.*/
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
printf("Enter the values of a&b");
scanf("%d%d",&a,&b);
c=a+b;
printf("Addition=%d",c);
getch();
}
/*output*/
Enter the values of a&b90 10
Addition=100
Enter the values of a&b56 75
Addition=131
/*c-program for Division oftwo nos.*/
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
clrscr();
printf("Enter the values of a&b");
scanf("%d%d",&a,&b);
c=a/b;
printf("Division=%d",c);
getch();
}
/*output*/
Enter the values of a&b10 10
Division=1
Enter the values of a&b 70 10
Division=7
/*c-program for Subtraction of two
nos.*/
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
clrscr();
printf("Enter the values of a&b");
scanf("%d%d",&a,&b);
c=a-b;
printf("Subtraction=%d",c);
getch();
}
/*output*/
Enter the values of a&b56 45
Subtraction=11
/*c-program for multiplication oftwo
nos.*/
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
clrscr();
printf("Enter the values of a&b");
scanf("%d%d",&a,&b);
c=a*b;
printf("Multiplication=%d",c);
getch();
}
/*output*/
Enter the values of a&b10 10
Multiplication=100
Enter the values of a&b90 90
Multiplication=8100
/*c-program to find area of circle*/
#include<stdio.h>
#include<conio.h>
void main()
{
int r,pie=3.14,a;
clrscr();
printf("Enter the values of r");
scanf("%d",&r);
a=3.14*r*r;
printf("Area of circle=%d",a);
getch();
}
/*output*/
Enter the values of r10
Area of circle=314
/*c-program for conditional branching of
switch as well as to show break statment*/
#include<stdio.h>
#include<conio.h>
int main()
{
int n;
clrscr();
printf("enter no.=");
scanf("%d",&n);
switch(n);
{
case0:printf("zero");
break();
case1:printf("one");
break();
case2:printf("two");
break();
default:printf("Invalid Input");
break();
}
getch();
}
/*output*/
enter no.=1
one
enter no.=2
two
enter no.=0
zero
/*c-program to find whether the integer is odd
or even*/
#include<stdio.h>
#include<conio.h>
int main()
{
int n; //n is any integer
printf("Enter the value of n");
scanf("%d",&n);
//True if remainder is 0
if(n%2==0)
printf("%d is an even integer",n);
else
printf("%d is an odd integer",n);
getch();
}
/*output*/
Enter the value of n7
7 is an odd integer
Enter the value of n10
10 is an even integer
Enter the value of n1
1 is an odd integer
Enter the value of n0
0 is an even integer
/*c-program to find smallest among the three nos.
using nested if..else or if else laddar*/
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
printf("Enter the values of a,b,c");
scanf("%d%d%d",&a,&b,&c);
if(a<b)
{
if(a<c)
printf("%d is smallest",a);
else
printf("%d is smallest",c);
}
else
{
if(b<c)
printf("%d is smallest",b);
else
printf("%d is smallest",c);
}
getch();
}
/*output*/
Enter the values of a,b,c1 4 7
1 is smallest
Enter the values of a,b,c 0 1 9
0 is smallest
Enter the values of a,b,c991 198 90
90 is smallest
/*C-program to write array elements in descending order.*/
#include<stdio.h>
#include<conio.h>
main()
{
int a[10],i,j,k;
printf("Enter the array elements:");
for(i=0;i<10;i++)
{
scanf("%dt",&a[i]);
}
for(i=0;i<9;i++) /*write array elements in
descending order*/
{
for(j=i+1;j<10;j++)
{
if(a[i]<a[j])
{
k=a[i];
a[i]=a[j];
a[j]=k;
}
}
}
printf("Array element is in descending order.");
for(i=0;i<10;i++)
{
printf("%dt",a[i]);
}
getch();
}
/*output*/
Enter the array elements:10 34 65 5 345 490 90 86 43 14
Array element is in descending order. 490 345 90 86 65
43 34 14 10 5
/*C-program to write array elements in ascending order.*/
#include<stdio.h>
#include<conio.h>
main()
{
int a[10],i,j,k;
clrscr();
printf("Enter the array elements:");
for(i=0;i<10;i++)
{
scanf("%dt",&a[i]);
}
for(i=0;i<9;i++) /*write array elements in ascending
order*/
{
for(j=i+1;j<10;j++)
{
if(a[i]>a[j])
{
k=a[i];
a[i]=a[j];
a[j]=k;
}
}
}
printf("Array element is in ascending order.");
for(i=0;i<10;i++)
{
printf("%dt",a[i]);
}
getch();
}
/*output*/
Enter the array elements:434 45 465 13 67 83 53 77 67 13
Array element is in ascending order. 13 13 45 53 67 67 77
83 434 465
/*c-program to check wether the entered no. is prime
or not*/
#include<stdio.h>
#include<conio.h>
main()
{
int n,i,r;
printf("Enter a number:");
scanf("%d",&n);
i=2;
while(i<=n-1)
{
r=n%i;
if(r==0)
break;
i++;
}
if(i==n)
printf("The entered number is a prime no.");
else
printf("The entered number is not a prime no.");
getch();
}
/*output*/
Enter a number:5
The entered number is a prime no.
Enter a number:10
The entered number is not a prime no.
Enter a number:173
The entered number is a prime no.
Enter a number:237
The entered number is not a prime no.

some basic C programs with outputs

  • 1.
  • 2.
    /*c-program to findlargest among three nos. using logical AND*/ #include<stdio.h> #include<conio.h> void main () { int a,b,c; printf("Enter values of a, b and c"); scanf("%d%d%d",&a,&b,&c); if((a>b)&&(a>c)) printf("%d is largest",a); if((c>a)&&(c>b)) printf("%d is largest",c); getch(); } /*close of main()function*/ /*output */ Enter values of a, b and c1 2 3 3 is largest Enter values of a, b and c7 8 9 9 is largest Enter values of a, b and c11 15 23 23 is largest Enter values of a, b and c1 6 9 9 is largest
  • 3.
    /*c-program for findingthe sum of series of 1 to 1/(10!)*/ #include<stdio.h> #include<conio.h> void main() { float i,sum,fact; for(i=1,sum=0,fact=1;i<=10;i++) { fact=fact*i; sum=sum+(1/fact); } printf("sum=%f",sum); getch(); } /*output*/ sum=1.718282
  • 4.
    /*c program tofind sum of(1/1) to (1/10)*/ #include<stdio.h> #include<conio.h> void main() { float i,sum; for(i=1,sum=0;i<=10;i++) { sum=sum+(1/i); } printf("sum=%f",sum); getch(); } /*output*/ sum=2.928968
  • 5.
    /*c program foraddition of1-5 numbers using while loop*/ #include<stdio.h> #include<conio.h> void main() { int sum=0,i=1; while(i<=5) { sum=sum+i; i++; } printf("addition=%d",sum); getch(); } /*output*/ addition=15 /* c-program to find table from 1 to 10*/ #include<stdio.h>
  • 6.
    #include<conio.h> void main() { int i,j; for(i=1;i<=10;i++) { for(j=1;j<=10;j++) { printf("%dt",i*j); } printf("n"); } getch(); } /*Output*/ 12 3 4 5 6 7 8 9 10 2 4 6 8 10 12 14 16 18 20 3 6 9 12 15 18 21 24 27 30 4 8 12 16 20 24 28 32 36 40 5 10 15 20 25 30 35 40 45 50 6 12 18 24 30 36 42 48 54 60 7 14 21 28 35 42 49 56 63 70 8 16 24 32 40 48 56 64 72 80 9 18 27 36 45 54 63 72 81 90 10 20 30 40 50 60 70 80 90 100 /*c program for goto in unconditional branching or taking square of a no.*/
  • 7.
    #include<stdio.h> #include<conio.h> void main() { int a,b; clrscr(); start: printf("enterthe value of a"); scanf("%d",&a); b=a*a; if(b>50) { goto start; } printf("b=%d",b); getch(); } /*Output*/ enter the value of a5 b=25 /*c-program of to find largest among 3 number using if..else laddar or nested if..else*/
  • 8.
    #include<stdio.h> #include<conio.h> void main() { int a,b,c; printf("enterthe values of a,b,c"); scanf("%d%d%d", &a,&b,&c); if(a>c) { if(a>c) printf("%d is largest",a); else printf("%d is largest",c); } else { if(b>c) printf("%d is largest",b); else printf("%d is largest",c); } getch(); } /*output*/ enter the values of a,b,c 1 4 0 4 is largest enter the values of a,b,c 3 4 5 5 is largest
  • 9.
    /*c-program for additionoftwo nos.*/ #include<stdio.h> #include<conio.h> void main() { int a,b,c; printf("Enter the values of a&b"); scanf("%d%d",&a,&b); c=a+b; printf("Addition=%d",c); getch(); } /*output*/ Enter the values of a&b90 10 Addition=100 Enter the values of a&b56 75 Addition=131
  • 10.
    /*c-program for Divisionoftwo nos.*/ #include<stdio.h> #include<conio.h> void main() { int a,b,c; clrscr(); printf("Enter the values of a&b"); scanf("%d%d",&a,&b); c=a/b; printf("Division=%d",c); getch(); } /*output*/ Enter the values of a&b10 10 Division=1 Enter the values of a&b 70 10 Division=7
  • 11.
    /*c-program for Subtractionof two nos.*/ #include<stdio.h> #include<conio.h> void main() { int a,b,c; clrscr(); printf("Enter the values of a&b"); scanf("%d%d",&a,&b); c=a-b; printf("Subtraction=%d",c); getch(); } /*output*/ Enter the values of a&b56 45 Subtraction=11
  • 12.
    /*c-program for multiplicationoftwo nos.*/ #include<stdio.h> #include<conio.h> void main() { int a,b,c; clrscr(); printf("Enter the values of a&b"); scanf("%d%d",&a,&b); c=a*b; printf("Multiplication=%d",c); getch(); } /*output*/ Enter the values of a&b10 10 Multiplication=100 Enter the values of a&b90 90 Multiplication=8100
  • 13.
    /*c-program to findarea of circle*/ #include<stdio.h> #include<conio.h> void main() { int r,pie=3.14,a; clrscr(); printf("Enter the values of r"); scanf("%d",&r); a=3.14*r*r; printf("Area of circle=%d",a); getch(); } /*output*/ Enter the values of r10 Area of circle=314
  • 14.
    /*c-program for conditionalbranching of switch as well as to show break statment*/ #include<stdio.h> #include<conio.h> int main() { int n; clrscr(); printf("enter no.="); scanf("%d",&n); switch(n); { case0:printf("zero"); break(); case1:printf("one"); break(); case2:printf("two"); break(); default:printf("Invalid Input"); break(); } getch(); } /*output*/ enter no.=1 one enter no.=2 two enter no.=0 zero
  • 15.
    /*c-program to findwhether the integer is odd or even*/ #include<stdio.h> #include<conio.h> int main() { int n; //n is any integer printf("Enter the value of n"); scanf("%d",&n); //True if remainder is 0 if(n%2==0) printf("%d is an even integer",n); else printf("%d is an odd integer",n); getch(); } /*output*/ Enter the value of n7 7 is an odd integer Enter the value of n10 10 is an even integer Enter the value of n1 1 is an odd integer Enter the value of n0 0 is an even integer
  • 16.
    /*c-program to findsmallest among the three nos. using nested if..else or if else laddar*/ #include<stdio.h> #include<conio.h> void main() { int a,b,c; printf("Enter the values of a,b,c"); scanf("%d%d%d",&a,&b,&c); if(a<b) { if(a<c) printf("%d is smallest",a); else printf("%d is smallest",c); } else { if(b<c) printf("%d is smallest",b); else printf("%d is smallest",c); } getch(); } /*output*/ Enter the values of a,b,c1 4 7 1 is smallest Enter the values of a,b,c 0 1 9 0 is smallest Enter the values of a,b,c991 198 90 90 is smallest
  • 17.
    /*C-program to writearray elements in descending order.*/ #include<stdio.h> #include<conio.h> main() { int a[10],i,j,k; printf("Enter the array elements:"); for(i=0;i<10;i++) { scanf("%dt",&a[i]); } for(i=0;i<9;i++) /*write array elements in descending order*/ { for(j=i+1;j<10;j++) { if(a[i]<a[j]) { k=a[i]; a[i]=a[j]; a[j]=k; } } } printf("Array element is in descending order."); for(i=0;i<10;i++) { printf("%dt",a[i]); } getch(); } /*output*/ Enter the array elements:10 34 65 5 345 490 90 86 43 14 Array element is in descending order. 490 345 90 86 65 43 34 14 10 5
  • 18.
    /*C-program to writearray elements in ascending order.*/ #include<stdio.h> #include<conio.h> main() { int a[10],i,j,k; clrscr(); printf("Enter the array elements:"); for(i=0;i<10;i++) { scanf("%dt",&a[i]); } for(i=0;i<9;i++) /*write array elements in ascending order*/ { for(j=i+1;j<10;j++) { if(a[i]>a[j]) { k=a[i]; a[i]=a[j]; a[j]=k; } } } printf("Array element is in ascending order."); for(i=0;i<10;i++) { printf("%dt",a[i]); } getch(); } /*output*/ Enter the array elements:434 45 465 13 67 83 53 77 67 13 Array element is in ascending order. 13 13 45 53 67 67 77 83 434 465
  • 19.
    /*c-program to checkwether the entered no. is prime or not*/ #include<stdio.h> #include<conio.h> main() { int n,i,r; printf("Enter a number:"); scanf("%d",&n); i=2; while(i<=n-1) { r=n%i; if(r==0) break; i++; } if(i==n) printf("The entered number is a prime no."); else printf("The entered number is not a prime no."); getch(); } /*output*/ Enter a number:5 The entered number is a prime no. Enter a number:10 The entered number is not a prime no. Enter a number:173 The entered number is a prime no. Enter a number:237 The entered number is not a prime no.