Professional Documents
Culture Documents
Programs of C
Programs of C
Programs of C
(Polytechnic)
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c,d,e,f,g;
clrscr();
printf("Enter the three values");
scanf("%d%d%d",&a,&b,&c);
d=((b*b)-(4*a*c));
if(d==0)
{
printf("Roots are real and equal");
f=-b/(2*a);
printf("x1=%d\nx2=%d",f,f);
}
else if(d>0)
{
printf("Roots are real and distinct");
e=sqrt(d);
f=(-b+e)/(2*a);
g=(-b-e)/(2*a);
printf("x1=%d\nx2=%d",f,g);
}
else
{
printf("Roots are imaginary");
d=-d;
e=sqrt(d);
f=-b/(2*a);
g=e/(2*a);
printf("x1=%d+%d\nx2%d-%d",f,g,f,g);
}
getch();
}
void main()
{
int a,b,c,big;
clrscr();
printf("\nEnter 3 numbers:");
scanf("%d %d %d",&a,&b,&c);
big=(a>b&&a>c?a:b>c?b:c);
printf("\nThe biggest number is:%d",big);
getch();
Program 3. Program to display 4 digit numbers in opposite Order.
#include<stdio.h>
#include<conio.h>
void main()
{
int number;
int opp=0,c=1000;
int remainder;
clrscr();
printf("\n Enter a 4 digit number please ");
scanf("%d",&number);
while(number>0)
{
remainder=number%10;
opp=opp+remainder*c;
number=number/10;
c=c/10;
}
printf("\nNumber in opposite order is %d ",opp);
getch();
}
Program 4: Program to convert centigrade temp. into Fahrenheit Temperature
#include<stdio.h>
#include<conio.h>
void main()
{
float celcius,farenheit;
clrscr();
printf( "Enter Celsius\n" );
scanf("%f",&celcius);
farenheit=1.8*celcius+32;
#include<stdio.h>
#include<conio.h>
void main()
{
int start,end,i,sum=0;
clrscr();
printf("\n Enter the value of Start of the Series");
scanf("%d",&start);
printf("\n Enter the value for End of the Series");
scanf("%d",&end);
for(i=start;i<=end;i++)
{
sum=sum+(3*i+4);
}
printf("\n Summation of this series is %d ",sum);
getch();
}
Program 6: Program to generate Fibonacci Series, Prime number.
#include<stdio.h>
#include<stdio.h>
#include<conio.h>
#include<conio.h>
void main() void main()
{ {
int a=0,b=1,c=0,n,sum=0; int a,c=0,i,n;
clrscr(); clrscr();
printf("\n Enter a Value of n "); printf("enter the
number to be checked");
scanf("%d",&n); scanf("%d",&n);
printf("%d %d ",a,b); for(i=1;i<=n;i++)
{
while(sum<=n)
{ a=n%i;
sum=a+b; if(a==0)
printf("%d ",sum); {
a=b; c = c+1;
b=sum; }
c=c+sum; }
} if(c==2)
printf("\n Total = %d ",c); {
getch(); printf(“\n The given
Number is Prime”);
} else
printf("the given number
is not prime");
getch();
}
#include<stdio.h>
#include<conio.h>
void main()
{
int number,a;
int fact=1;
clrscr();
printf("\n Enter the number who's factorial you want ");
scanf("%d",&number);
for(a=1;a<=number;a++)
{
fact=fact*a;
}
printf("\n Factorial is %d ",fact);
getch();
}
Program 8: program to find average of marks for N students
#include<stdio.h>
#include<conio.h>
void main()
{
int number,physics,chemistry,mathematics,a,avg;
int sum=0;
clrscr();
printf("\n How many students Record you want to Enter
");
scanf("%d",&number);
for(a=1;a<=number;a++)
{
printf("\n Enter the Marks for the student Number %d",a);
printf("\n Marks in Physics = ");
scanf("%d",&physics);
printf("\n Marks in Chemistry = ");
scanf("%d",&chemistry);
printf("\n Marks in Mathematics = ");
scanf("%d",&mathematics);
sum=physics+chemistry+mathematics;
avg=sum/3;
printf("\n Average is %d",avg);
printf("\n\n***********************************************
**********\n\n");
}
getch();
}
#include<stdio.h>
#include<conio.h>
void main()
{
int i,a=24,b=16,f=0,gcd=0;
clrscr();
for (i=1;i<=b;i++)
{
if(a%i==0&&b%i==0)
f=i;
}
printf("%d",f);
getch();
}
Program 10 : Menu driven programs using switch statement to do different
arithmetic operations.
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
int choice;
clrscr();
printf("Enter first number: ");
scanf("%d",&a);
printf("Enter second number: ");
scanf("%d",&b);
printf("Please Enter your Choice :\n");
printf("\n\nPlease Enter 1 for Add\n2 for Subtract\n3 for Multipy
and\n4 for Divide\n");
scanf("%d",&choice);
switch(choice)
{
case 1 : printf("\nSum of the two number = %d", a+b);
break;
case 2 : printf("Difference of two number is = %d", a-b);
break;
case 3 : printf("Product of two number is = %d", a*b);
break;
case 4 :
if(b!=0)
printf("devision of two number is = %d", a/b);
break;
default:
printf("\n Thanks for using this program");
}
getch();
}
Program 11. Programs to demonstrate the use of continue, break and conditional
operators like days of the Week and months based on selection of a character from
user.
#include<stdio.h> #inlcude<stdio.h>
#include<conio.h> #include<conio.h>
void main() void main()
{ {
int choice; int i, j ;
clrscr(); clrscr();
printf("\n Please Enter your Choice :"); for ( i = 1 ; i <= 2 ;
i++ )
scanf("%d",&choice); {
if(choice<=7) for ( j = 1 ; j <= 2 ; j++ )
{ {
switch(choice) if ( i == j )
{ continue;
case 1 : printf("\nSunday"); printf ( "\n%d
%d\n", i, j ) ;
break; }
case 2 : printf("\nMonday"); }
break; }
case 3 : printf("\nTuesday");
break;
case 4 : printf("\nWednesday");
break;
case 5: printf("\nThursday");
break;
case 6: printf("\n Friday");
break;
case 7: printf("\n Saturday");
default:
printf("\n Thanks for using this program");
}}
else
printf("\n Please enter value between 1 to 7");
getch();
}
Program 12: Program to print the following pyramid:
*
***
*****
*******
#include<stdio.h>
#include<conio.h>
void main()
{
int n, i,j,k,l,m,c=1;
clrscr ();
printf ("\n Enter the number of Rows");
scanf ("%d",&n);
clrscr ();
m=n;
for(i=1;i<=n;i++)
{
for(j=m;j>1;j--)
{
printf (" ");
}
m--;
for(k=1;k<=c;k++)
{
printf("* ");
}
printf("\n");
c=c+2;
}
getch();
}
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,k,n;
clrscr();
for(i=1;i<=5;i++)
{
for(n=0;n<5-i;n++)
{
printf(" ");
}
for(j=1;j<=i;j++)
{
printf("%d",j);
}
for(k=j-2;k>0;k--)
{
printf("%d",k);
}
printf("\n");
}
getch();
}
Program 14 : Program to print the following pyramid:
1
234
56789
1234567
#include<stdio.h>
#include<conio.h>
void main()
{
int c=1,i,j,n;
clrscr();
for(i=1;i<=4;i++)
{
for(n=0;n<=5-i;n++)
{
printf(" ");
}
for(j=1;j<=2*i-1;j++)
{
if(c<=9)
{
printf("%d",c);
c++;
}
else
{
c=1;
printf("%d",c);
c++;
}
}
printf("\n");
}
getch();
}
Program 15 : Program to print the following pattern:
1
23
456
7891
23456
#include<stdio.h>
#include<conio.h>
void main()
{
int c=1,i,j;
clrscr();
for(i=1;i<=6;i++)
{
for(j=1;j<=1*i-1;j++)
{
if(c<=9)
{
printf("%d",c);
c++;
}
else
{
c=1;
printf("%d",c);
c++;
}
}
printf("\n");
}
getch();
#include<stdio.h>
#include<conio.h>
void main()
{
char name[10]="LOVELY";
int i,j;
clrscr();
for(i=0;i<=5;i++)
{
for(j=0;j<=i;j++)
{
printf("%c",name[j]);
}
printf("\n");
}
for (i=5;i>=0;i--)
{
for(j=0;j<=i-1;j++)
{
printf("%c",name[j]);
}
printf("\n");
}
getch ();
}
Program 17. Program to print a lowercase string to its uppercase equivalent .
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
int length;
char *a;
char *b;
clrscr();
printf("\n Enter any String please ");
scanf("%s",a);
length=strlen(a);
b=strupr(a);
printf("\n The string you entered is %s",a);
printf("\n Length of the string you entered is
%d",length);
printf("\n upper case conversion is %s ",b);
getch();
}