Let see some c programs.
1)
\* This is my first c program in c*\
#include<stdio.h>
#include<conio.h> .
void main()
{
clrscr();
printf("This is my first c program");
getch();
}
#include<stdio.h> is header file that use for working ' printf ' and 'scanf' statement
2) To check given number is 'Odd or Even'.
#include<stdio.h>
#include<conio.h>
void main()
{
int a;
clrscr();
printf("Enter the number");
scanf("%d",&a);
if(a/2= =0)
printf("The given number %d is even",a);
else
printf("The given number %d is odd",a);
getch();
}
3) To find sum of two number.
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,s;
clrscr();
printf("Enter two numbers");
scanf("%d%d",&a,&b); s=(a+b);
printf("The sum is %d",s);
getch();
}
4) To find the area of a circle.
#include<stdio.h>
#include<conio.h>
void main()
{
int b,h,a;
clrscr();
printf("Enter the breadth and height");
scanf("%d%d",&b,&h);
a=.5*b*h;
printf("The area is %d",a);
getch();
}
5) To find the roots of a quadratic equation.
#include<stdio.h>
#include<math.h>
#include<conio.h>
void main()
{
float a,b,c,d,rp,ip,r1,r2;
clrscr();
printf("Enter the coefficient");
scanf("%f%f%f",&a,&b,&c);
d=b*b-4*a*c;
if(d<0)
{
rp=-b/(2*a);
ip=sqrt(-1*d)/(2*a);
printf("The roots are imaginary.\n");
printf("%f+i%f\n",rp,ip);
printf("%f-i%f\n",rp,ip);
}
else
{
r1=(-b+sqrt(d))/2*a;
r2=(-b-sqrt(d))/2*a;
printf("The roots are\n%f",r1,r2);
}
getch();
}
#include<math.h> is a header file used for include math function.
6) To print like below
1
2 2
3 3 3
4 4 4 4
#include<stdio.h>
#include<conio.h>
void main()
{
int l,j;
clrscr();
for(l=1;l<=9;l++)
{
printf("\n");
for(j=1;j<=l;j++)
printf("%d ",l);
}
getch();
}
7) To find average
#include<conio.h>
void main()
{
int r,a,b,c,v;
clrscr();
printf("Enter the roll number");
scanf("%d",r);
printf("Enter the three marks obtained");
scanf("%d%d%d",&a,&b,&c);
v=(a+b+c)/3;
if(v>=80)
printf("The roll number %d has distinction",r);
else
if(60<=v && v<80)
printf("The roll number %d has first class",r);
getch();
}
8) To search an element in array
#include<stdio.h>
#include<conio.h>
void readnum(int a[],int n);
int searchnum(int a[],int n,int x);
void main()
{
int a[50],n,x,p;
clrscr();
printf("How many numbers\n");
scanf("%d",&n);
readnum(a,n);
printf("Enter the number to be search\n");
scanf("%d",&x);
p=searchnum(a,n,x);
if(p==0)
printf("%d is found",x);
else
printf("not found");
getch();
}
void readnum(int a[],int n)
{
int i;
printf("Enter the %d numbers\n",n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
}
int searchnum(int a[],int n, int x)
{
int j;
for(j=0;j<n;j++)
{
if(a[j]==x)
return(0);
}
return(1);
}
9) To read matrix and print
#include<conio.h>
#include<stdio.h>
void readmat(int a[][5],int m,int n)
{
int i,j;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
}
}
void printmat(int a[][5],int m,int n)
{
int i,j;
for(i=0;i<m;i++)
printf("\n");
for(j=0;j<n;j++)
printf("%d ",a[i][j]);
}
void main()
{
int a[5][5],m,n;
clrscr();
printf("how many rows and columns ");
scanf("%d%d",&m,&n);
printf("Enter the matrix");
readmat(a,m,n);
printmat(a,m,n);
getch();
}
10) check the given number is prime
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int n,i,r;
clrscr();
printf("Enter the number");
scanf("%d",&n);
for(i=2;i<=sqrt(n);i++);
r=n/i;
if(r==0)
printf("%d is prime",n);
getch();
}
11) Fibinosis number generation
#include<stdio.h>
#include<conio.h>
void main()
{
int n,x,y,z,i;
clrscr();
printf("Enter the limit");
scanf("%d",&n);
for(z=0,x=1,y=0,i=0;i<=n;z=x+y,i++,x=y,y=z)
printf("%d ",z);
getch();
}
12) Calculator
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
char c;
clrscr();
printf("Enter the operender operater and operent\n");
scanf("%d%c%d",&a,&c,&b);
switch(c)
{
case '+' :printf("%d %c %d=%d",a,c,b,a+b);
break;
case '-' :printf("%d %c %d=%d",a,c,b,a-b);
break;
case '*' :printf("%d %c %d=%d",a,c,b,a*b);
break;
case '/' :printf("%d %c %d=%d",a,c,b,(float)a/b);
break;
default:printf("sorry expression");
}
getch();
}
13) Bubble Sort
#include<stdio.h>
#include<conio.h>
void read(int a[],int n)
{
int i;
printf("Enter the %d numbers\n",n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
}
void sort(int a[],int n)
{
int i,p,c,temp;
for(p=1;p<n;p++)
{
for(c=0;c<n-p;c++)
if(a[c]>a[c+1])
{
temp=a[c];
a[c]=a[c+1];
a[c+1]=temp;
}
}
}
void print(int a[],int n)
{
int i;
printf("The sorted order is below\n");
for(i=0;i<n;i++)
printf("%d\n",a[i]);
}
void main()
{
int a[50],n;
clrscr();
printf("How many numbers\n");
scanf("%d",&n);
read(a,n);
sort(a,n);
print(a,n);
getch();
}
0 comments:
Post a Comment