(1)Find largest of three no
(2)Compute cosine series
(3)Compute sin series
(4)Compute exponential series
(5)Find factorial of a no
(6)Construct a pyramid of digit for n no
1
1 2
1 2 3
1 2 3 4
(7)Print reverse pyramid for n no
1 2 3 4
1 2 3
1 2
1
(8)Check validity of triangle and type of triangle(Isosceles, equilateral, scalene, right angle triangle)
(9)print a character certain number of times
(10)Illustrate ways of passing parameters to function
(11)Use of ternary operator to find largest no out of three no
(12)Demonstrate use of break stmt
(13)Illustrate use of continue
(14)Use of macros with and without arguments
(15)Demonstration of static and auto variables
(Program count the no of times a function is visited)
(16)Find the largest and smallest elements in the vector
(17)Sorting elements from array
(18)Inserting elements in array
(19)Deleting elements in array
(20)Search an element in 1 Dimensional array
(21)Calculating mean, median and variance from array
(22)Trace of matrix
(23)Norm of a matrix
(square root of sum of squares of elements of a matrix)
(24)Program to find the sum of above and below the main diagonal of matrix
(25)Magic Matrix (ODD NO)
(26)Program to sort the matrix row wise
(27)Multiplication of two matrices
(28)Matrix is symmetric or not
(29)Transpose of matrix
(30)Fibonacci sequence generation using recursion
(31)GCD of two no using recursion
(32)Tower of Hanoi using recursion
(33)Reversing the N no of chars using recursion
(33)Binary search using recursion
(34)Maximum and minimum using recursion
(35)Upper case to lower case conversion
(36)Counting no of vowels, consonants, words, white spaces and others in a line of text
(37)Check if string is palindrome or not
(38)Sort the names
(39)Pass parameters to function using call by value and call by reference
(40)check whether given number is prime or not and generate prime numbers within range
(41)Use of Structure
(structure that can describe hotel.it have members that include name, address, grade, average room charge and no. of rooms)
(42)Store records of a professor in a file (a. insert a record
b. delete a record
c. display a record)
(43)Program to merge 2 files (STUDENT ROLL NO,NAME and marks of 3 subjects)
(44)Convert Decimal No. To Binary
(45)Delete the duplicate values from array
(46)Find Out Largest and Second Largest In an Array
(47)Permutation and combination
(48)Check whether The No. Is Palindrome or Not
(49)Swap Two No. without Using Third Variable
(50)Convert centigrade into Fahrenheit
(51)Quadratic Equation
(52)Diamond
1
1 2
1 2 3
1 2
1
(53)Simple And Compound Interest
-----------------------------------------------------------------------------------
Find largest of three no
#include<stdio.h>
#include<conio.h>
void main()
{
int n1,n2,n3;
clrscr();
printf("Enter values for n1,n2 and n3.");
scanf("%d%d%d",&n1,&n2,&n3);
if(n1==n2&& n1==n3)
{
printf("All numbers are same.");
}
else if(n1==n2 && n1!=n3)
{
if(n1>n3)
printf("n1 and n2 are the largest numbers.");
else
printf("n3 is a largest number.");
}
else if(n1==n3 && n1!=n2)
{
if(n1>n2)
printf("n1 and n3 are the largest numbers.");
else
printf("n2 is a largest number.");
}
else if(n2==n3 && n1!=n2)
{
if(n1<n2)
printf("n2 and n3 are the largest numbers.");
else
printf("n1 is a largest number.");
}
else if(n1>n2 && n1>n3)
{
printf("n1 is the largest number.");
}
else if(n2>n1 && n2>n3)
{
printf("n2 is the largest number.");
}
else if(n3>n1 && n3>n2)
{
printf("n3 is the largest number.");
}
getch();
}
-----------------------------------------------------------------------------------Compute cosine series
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int n,j,i,t=1;
float x,s=0;
double f;
printf("enter upper limit n and value of x\n");
scanf("%d%f",&n,&x);
x=x*3.142/180;
for(i=0;i<=n;i=i+2)
{
f=1;
for(j=2;j<=i;j++)
f=f*j;
s=s+pow(x,i)*t/f;
t=-t;
}
printf("\n cos(%.4f)=%g",x*180/3.142,s);
getch();
}
Compute sin series
#include<stdio.h>
#include<conio.h>
#include<math.h>
#define pi 3.142
void main()
{
int n,i,j,t=1;
float x;
double s=0,f;
printf("enter upper limit and value of x in degree\n");
scanf("%d%f",&n,&x);
x=x*pi/180;
for(i=1;i<=n;i=i+2)
{
f=1;
for(j=2;j<=i;j++)
f=f*j;
s=s+pow(x,i)*t/f;
t=-t;
}
printf("sin(%.4f)=%g",x*180/pi,s);
getch();
}
-----------------------------------------------------------------------------------Compute exponential series
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int n,j,i;
float x;
double ex=0,f;
printf("enter upper limit n and value of x\n");
scanf("%d%f",&n,&x);
for(i=0;i<n;i++)
{
f=1;
for(j=2;j<=i;j++)
f=f*j;
ex=ex+pow(x,i)/f;
}
printf("\n exp(%.4f)=%g",x,ex);
getch();
}
-----------------------------------------------------------------------------------Find factorial of a no
#include<stdio.h>
#include<conio.h>
void main()
{
int i,n;
long f=1;
clrscr();
printf("Enter the number=");
scanf("%d",&n);
for(i=n;i>0;i--)
{
f=f*i;
}
printf("Factorial of %d=%ld",n,f);
getch();
}
-----------------------------------------------------------------------------------Construct a pyramid of digit for n no
1
1 2
1 2 3
1 2 3 4
#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,j;
clrscr();
printf("enter n\n ");
scanf("%d",&n);
printf("\n");
for(i=1;i<=n;i++)
{
gotoxy((40-i),i);
for(j=1;j<=i;j++)
{
printf("%d ",j);
}
printf("\n");
}
getch();
}
-----------------------------------------------------------------------------------Print reverse pyramid for n no
1 2 3 4
1 2 3
1 2
1
#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,j,k=3;
clrscr();
printf("enter n\n ");
scanf("%d",&n);
printf("\n");
for(i=n;i>=1;i--)
{
gotoxy(k,k);
for(j=1;j<=i;j++)
{
printf("%d ",j);
}
printf("\n"); k++;
}
getch();
}
-----------------------------------------------------------------------------------Check validity of triangle and type of triangle(Isosceles, equilateral, scalene, right angle triangle)
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
clrscr();
printf("Enter values of three sides of triangle a,b and c respectively =");
scanf("%d%d%d",&a,&b,&c);
if(a>(b+c))
{
printf("Triangle is not a valid triangle.");
}
else if(b>(a+c))
{
printf("Triangle is not a valid triangle.");
}
else if(c>(a+b))
{
printf("Triangle is not a valid triangle.");
}
else
{
if(a==b && a==c)
{
printf("Triangle is an equilateral triangle.");
}
else if(a==b)
{
printf("Triangle is an isosceles triangle having a and b sides are equal.");
}
else if(a==c)
{
printf("Triangle is an isosceles triangle having a and c sides are equal.");
}
else if(b==c)
{
printf("Triangle is an isosceles triangle having b and c sides are equal.");
}
else
{
printf("Triangle is a scalence triangle.");
}
}
getch();
}
-----------------------------------------------------------------------------------
print a character certain number of times
#include<stdio.h>
#include<conio.h>
void x(void);
void main()
{
int i,a;
clrscr();
printf("\nEnter, how many times do u wanna print a char..?\n");
scanf("%d",&a);
for(i=1;i<=a;i++)
{
x();
}
getch();
}
void x()
{
printf("V ");
}
-----------------------------------------------------------------------------------Illustrate ways of passing parameters to function
#include<stdio.h>
#include<conio.h>
void swap(int a,int b)
{
int t;
t=a;
a=b;
b=t;
}
void swap1(int *a,int *b)
{
int t;
t=*a;
*a=*b;
*b=t;
}
void main()
{
int a,b;
clrscr();
printf("enter two no..");
scanf("%d %d",&a,&b);
printf("\nusing call by value..");
swap(a,b);
printf("\n%d %d",a,b);
printf("\nusing call by ref..");
swap1(&a,&b);
printf("\n%d %d",a,b);
getch();
}
-----------------------------------------------------------------------------------Use of ternary operator to find largest no out of three no
#include<stdio.h>
#include<conio.h>
void main()
{
int x,a,b,c;
clrscr();
printf("Enter three nos..");
scanf("%d %d %d",&a,&b,&c);
x=(a>b)?a:b;
x=(x>c)?x:c;
printf("max=%d",x);
getch();
}
-----------------------------------------------------------------------------------Demonstrate use of break stmt
(user will enter positive no one by one, program will find the maximum no, program terminate when negative no is entered)
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,max,n;
clrscr();
printf("enter a no");
scanf("%d",&max);
while(1)
{
scanf("%d",&n);
if(n<0)
break;
if(max<n)
{
max=n;
}
}
printf("maimum =%d\n",max);
getch();
getch();
}
-----------------------------------------------------------------------------------
Illustrate use of continue
(The program print only negative nos. From array)
#include<stdio.h>
#include<conio.h>
void main()
{
int a[5],i,j,n;
clrscr();
printf("\nenter array size..");
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("Negative no from array..");
for(i=0;i<n;i++)
{
if(a[i]>0)
{
continue;
}
else
{
printf(" %d",a[i]);
}
}
getch();
}
-----------------------------------------------------------------------------------Use of macros with and without arguments
#include<stdio.h>
#include<conio.h>
#define max(a,b) (a>b)?a:b
void main()
{
int x,a,b,c;
clrscr();
printf("Enter three nos..");
scanf("%d %d %d",&a,&b,&c);
x=max(a,b);
x=max(x,c);
printf("max=%d",x);
getch();
}
-----------------------------------------------------------------------------------Demonstration of static and auto variables
(Program count the no of times a function is visited)
#include<stdio.h>
#include<conio.h>
void disp();
void main()
{
clrscr();
printf("Using static variables...");
disp();
disp();
disp();
getch();
}
void disp()
{
static a=1;
printf("\n%d",a);
a++;
}
-----------------------------------------------------------------------------------Find the largest and smallest elements in the vector
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10]i,j,n,max,min;
clrscr();
printf("enter size of vector..");
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("Largest and smallest vlue in vector..max , min resp..");
for(i=0;i<n;i++)
{
if(i==1)
{
max=min=a[i];
}
if(max<a[i])
{
max=a[i];
}
if(min>a[i])
{
min=a[i];
}
}
printf("%d %d",max,min);
getch();
}
-----------------------------------------------------------------------------------
Sorting elements from array
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10],i,j,n,t;
clrscr();
printf("enter size of array..");
scanf("%d",&n);
printf("\nenter elements..");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
for(i=0;i<n;i++)
{
for(j=0;j<n-1;j++)
{
if(a[j]>a[j+1])
{
t=a[j];
a[j]=a[j+1];
a[j+1]=t;
}
}
}
for(i=0;i<n;i++)
{
printf(" %d",a[i]);
}
getch();
}
-----------------------------------------------------------------------------------Inserting elements in array
#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,a[10],pos,num;
printf("enter size of array\n");
scanf("%d",&n);
printf("enter elements in array\n");
for(i=1;i<=n;i++)
{
scanf("%d",&a[i]);
}
printf("enter position to insert element\n");
scanf("%d",&pos);
for(i=n;i>=pos;i--)
{
a[i+1]=a[i];
}
printf("enter element to insert \n");
scanf("%d",&num);
a[pos]=num;
n++;
for(i=1;i<=n;i++)
printf("%d ",a[i]);
getch();
}
-----------------------------------------------------------------------------------Deleting elements in array
#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,a[10],pos,num;
printf("enter size of array\n");
scanf("%d",&n);
printf("enter elements in array\n");
for(i=1;i<=n;i++)
{
scanf("%d",&a[i]);
}
printf("enter element to delete\n");
scanf("%d",&num);
for(i=1;i<=n;i++)
{
if(a[i]==num)
pos=i;
}
for(i=pos;i<=n;i++)
a[i]=a[i+1];
n--;
printf("new array\n");
for(i=1;i<=n;i++)
printf("%d ",a[i]);
getch();
}
Search an element in 1 Dimensional array
#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,a[10],pos=0,num;
printf("enter size of array\n");
scanf("%d",&n);
printf("enter elements in array\n");
for(i=1;i<=n;i++)
{
scanf("%d",&a[i]);
}
printf("enter element to search\n");
scanf("%d",&num);
for(i=1;i<=n;i++)
{
if(a[i]==num)
pos=i;
}
if(pos !=0)
printf("Element found at %d location",pos);
else
printf("Element not found");
getch();
}
-----------------------------------------------------------------------------------
Calculating mean, median and variance from array
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int n,i;
float a[50],mean,var ,stdd;
printf("enter n\n");
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%f",&a[i]);
mean+=a[i];
}
mean/=n;
for(i=0;i<n;i++)
var+=(mean-a[i])*(mean-a[i]);
var/=n;
stdd=sqrt(var);
printf("\nmean=%f\nmedian=%f\nvariance=%f\nstandard deviation=%f",mean,a[n/2],var,stdd);
getch();
}
-----------------------------------------------------------------------------------
Trace of matrix
(sum of diagonal elements from matrix)
#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,j,s=0,a[10][10];
clrscr();
printf("Enter size of your matrix..");
scanf("%d",&n);
for(i=0;i<=n-1;i++)
{
for(j=0;j<=n-1;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("\n\n\tTrace of your matrix is : ");
for(i=0;i<=n-1;i++)
{
for(j=0;j<=n-1;j++)
{
if(i==j)
s=s+a[i][j];
}
}
printf("%d",s);
getch();
}
-----------------------------------------------------------------------------------
Norm of a matrix
(square root of sum of squares of elements of a matrix)
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
float a[5][5],s=0;
int i,j,m,n;
printf("enter order of matrix\n");
scanf("%d%d",&m,&n);
printf("enter elements in matrix\n") ;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf("%f",&a[i][j]);
s+=a[i][j]*a[i][j];
}
}
s=sqrt(s);
printf("Norm of matrix=%f ",s);
getch();
}
Program to find the sum of above and below the main diagonal of matrix
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,n,a[10][10],sa=0,sb=0;
clrscr();
printf("\nEnter size of a matrix..");
scanf("%d",&n);
printf("\nenter elements..");
for(i=0;i<=n-1;i++)
{
for(j=0;j<=n-1;j++)
{
scanf("%d",&a[i][j]);
}
}
for(i=0;i<=n-1;i++)
{
for(j=0;j<=n-1;j++)
{
if(i>j)
sb=sb+a[i][j];
if(i<j)
sa=sa+a[i][j];
}
}
printf("\n\n\tSum of Above & below of main diagonal elements \n\n\tof a matrix with resp %d %d",sa,sb);
getch();
}
Magic Matrix (ODD NO)
#include<stdio.h>
#include<conio.h>
void main(){
int i,j=1,k,n,a[22][22],p,q,r,count=0;
clrscr();
A:
printf("Enter No. Of Rows Or Columns(Should Be Odd) : ");
scanf("%d",&n);
printf("\n");
if(n%2==0){
printf("Enter Odd No.s Only\n\n");
goto A;}
k=(n+1)/2;
for(p=1;p<=n;p++)
{ for(q=1;q<=n;q++){
a[p][q]=NULL;}}
for(i=1;i<=(n*n);i++){
if(a[j][k]==NULL){
a[j][k]=i;}
else{
j=j+2;
k--;
if(j==n+2)
j=2;
if(k==0)
k=n;
if(j==0)
j=n;
if(k==n+1)
k=1;
a[j][k]=i;}
j--;
k++;
if(j==0)
j=n;
if(k==n+1)
k=1;}
for(p=1;p<=n;p++){
for(q=1;q<=n;q++){
r=a[p][q];
while(r>0)
{r=r/10;
count++;}
if(count==1)
printf(" %2d",a[p][q]);
else
printf(" %2d",a[p][q]);
if(q%n==0)
printf("\n\n");
count=0;}}
getch();
}
-----------------------------------------------------------------------------------
Program to sort the matrix row wise
#include<stdio.h>
#include<conio.h>
void main()
{
int a[5][5],i,j,k,n,m,t;
printf("enter size of matrix\n");
scanf("%d%d",&m,&n);
printf("enter elements in matrix\n");
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
for(k=0;k<m;k++)
{
for(i=0;i<n;i++)
for(j=0;j<n-1;j++)
{
if(a[i][j]>a[i][j+1])
{
t=a[i][j];
a[i][j]=a[i][j+1];
a[i][j+1]=t;
}
}
}
printf("row wise Sorted matrix\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("%5d",a[i][j]);
}
printf("\n");
}
getch();
}
-----------------------------------------------------------------------------------
Multiplication of two matrices
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,m,n,p,q,k,**a,**b,**c;
clrscr();
printf("enter order of first matrix\n");
scanf("%d%d",&n,&m);
printf("enter order of second matrix\n");
scanf("%d%d",&p,&q);
if((n==q) && (m==p))
{
printf("enter elements in first matrix\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
scanf("%d",&a+i+j);
}
printf("enter elements in second matrix\n");
for(i=0;i<p;i++)
{
for(j=0;j<q;j++)
scanf("%d",&b+i+j);
}
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
*((*c+i)+j)=0;
for(k=0;k<n;k++)
{
*(*(c+i)+j)+= *((*a+i)+k) * *(*(b+i)+j);
}
}
}
printf("Output\n");
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
printf("%5d",*(*(c+i)+j));
printf("\n");
}
}
else{
printf("enter order properly \n");
} getch();}
-----------------------------------------------------------------------------------
Matrix is symmetric or not
#include<stdio.h>
#include<conio.h>
void main()
{
int fl=0,i,j,n,a[9][9];
clrscr();
printf("\nEnter square matrix size(should be odd)..");
scanf("%d",&n);
printf("\nenter elements of the matrix..");
for(i=0;i<n;i++) {
for(j=0;j<n;j++)
{scanf("%d",&a[i][j]);}}
printf("\nyour matrix is..\n");
for(i=0;i<n;i++){
for(j=0;j<n;j++){
printf(" %d",a[i][j]);
}printf("\n");}
for(i=0;i<n;i++){
for(j=0;j<n;j++){
if(a[i][j]==a[j][i])
fl++;}}
x=n*n;
if(fl==x)
printf("\nSymmetric matrix..");
else
printf("\nNot Symmetric matrix..");
getch();
}
-----------------------------------------------------------------------------------Transpose of matrix
#include<stdio.h>
#include<conio.h>
void main()
{
int m,n,i,j,a[10][10];
printf("enter size of matrix\n");
scanf("%d%d",&m,&n);
printf("enter elements in matrix\n");
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("%3d",a[i][j]);
}
printf("\n");
}
printf("Transpose of matrix\n");
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
printf("%3d",a[j][i]);
} printf("\n");
}
getch();
}
-----------------------------------------------------------------------------------
Fibonacci sequence generation using recursion
#include<stdio.h>
#include<conio.h>
void fib(int,int,int);
void main()
{
int f=0,f1=1,n;
printf("enter n\n");
scanf("%d",&n);
fib(f,f1,n);
getch();
}
void fib(int x,int y,int n)
{
int f;
if(n !=0)
{
printf("%3d",x);
f=x+y;
x=y;
y=f;
fib(x,y,n-1);
}
}
-----------------------------------------------------------------------------------GCD of two no using recursion
#include<stdio.h>
#include<conio.h>
int gcd(int,int);
void main()
{
int a,b,c;
printf("enter two numbers to find GCD\n");
scanf("%d%d",&a,&b);
c=gcd(a,b);
printf("GCD=%d",c);
getch();
}
int gcd(int x,int y)
{
int r;
if(x==0)
return y;
else
{
r=y%x;
y=x;
x=r;
return(gcd(x,y));
}
}
-----------------------------------------------------------------------------------
Tower of Hanoi using recursion
#include<stdio.h>
#include<conio.h>
void tower_of_hanoi(int n, char LEFT, char RIGHT, char CENTER)
{
if(n>0)
{
tower_of_hanoi(n-1, LEFT, CENTER, RIGHT);
printf("\n\tMove disk %d from %c to %c", n, LEFT, RIGHT);
tower_of_hanoi(n-1, CENTER, RIGHT, LEFT);
}
}
void main()
{
int n;
clrscr();
printf("\nEnter no. of disks: ");
scanf("%d",&n);
printf("\nSOLUTION \n\t (L=Left,R=Right,C=Ceter)\n");
tower_of_hanoi(n,'L','R','C');
getch();
}
-----------------------------------------------------------------------------------Reversing the N no of chars using recursion
#include<stdio.h>
#include<conio.h>
void rev()
{
char ch;
if((ch=getchar()) !='\n')
rev();
putchar(ch);
return;
}
void main()
{
printf("enter string\n");
rev();
getch();
}
-----------------------------------------------------------------------------------Binary search using recursion
#include<stdio.h>
#include<conio.h>
int a[10];
void bserch(int low,int high,int x)
{
int mid;
if(low>high)
{
printf("unsuccessful serch\n");
}
else if(low<=high)
{
mid=(low+high)/2;
if(a[mid]==x)
{
printf("element found at %d location\n",mid);
return;
}
else if(a[mid]<x)
{
bserch(mid+1,high,x);
}
else
{
bserch(low,mid-1,x);
}
}
}
void main()
{
int n,i,ele;
printf("enter n\n");
scanf("%d",&n);
printf("enter elements in ascending order \n");
for(i=1;i<=n;i++)
scanf("%d",&a[i]);
printf("enter element to serach\n");
scanf("%d",&ele);
bserch(1,n,ele);
getch();
}
-----------------------------------------------------------------------------------Maximum and minimum using recursion
#include<stdio.h>
#include<conio.h>
int a[10];
void maximum(int,int);
void minimum(int,int);
void main()
{
int n,i;
printf("enter n\n");
scanf("%d",&n);
printf("enter element in array\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
maximum(a[0],n);
minimum(a[0],n-1);
getch();
}
void maximum(int max,int n)
{
if(n==0)
{
printf("maximum=%d\n",max) ;
}
else
{
if(max<a[n])
{
maximum(a[n],n-1);
}else
maximum(max,n-1);
}
}
void minimum(int min,int n)
{
if(n==0)
{
printf("minimum=%d\n",min) ;
}
else
{
if(min>a[n])
{
minimum(a[n],n-1);
}else
minimum(min,n-1);
}
}
-----------------------------------------------------------------------------------Upper case to lower case conversion
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char *s;
int len;
printf("enter a string \n");
gets(s);
len=strlen(s);
while(*s !='\0')
{
if(*s>=65 && *s<=90)
{
*s=(*s)+32;
}
s++;
}
printf("\nlowercase\n");
puts(s-len);
getch();
}
-----------------------------------------------------------------------------------Counting no of vowels, consonants, words, white spaces and others in a line of text
#include<stdio.h>
#include<conio.h>
void main()
{
char *s;
int v=0,c=0,space=0,other=0;
printf("enter a string\n");
gets(s);
while(*s != '\0')
{
if((*s=='a' || *s=='A') ||(*s=='e'||*s=='E')||(*s=='i'||*s=='I')||(*s=='o'||*s=='O')||(*s=='u'||*s=='U'))
{
v++;
}
else if((*s>=65 && *s<=90) || (*s>=97 && *s<=122))
{
c++;
}
else if(*s==' ')
{
space++;
}
else other++;
s++;
}
printf("vowels=%d consonants=%d white spaces=%d other=%d" ,v,c,space,other);
getch();
}
-----------------------------------------------------------------------------------Check if string is palindrome or not
#include<stdio.h>
#include<conio.h>
#include<string.h>
char* strrev1(char *a)
{
char *b;
int n=0,d;
while(*a !='\0')
{
a++; n++;
}
d=n;
while(n >=0)
{
*b=*a;
a--;
b++;
n--;
}
*b='\0';
return b-d;
}
void main()
{
char *s,*d;
printf("enter any string to check palindrome\n");
gets(s);
d=strrev1(s);
if(strcmp(d,s)==0)
printf("string is palindrome\n");
else printf("Not palindrome\n");
getch();
}
-----------------------------------------------------------------------------------Sort the names
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char *t,a[50][20];
int i,j,n;
printf("enter n\n");
scanf("%d",&n);
printf("Enter %d strings\n",n);
for(i=0;i<=n;++i)
gets(a[i]);
for(i=0;i<n;i++)
for(j=0;j<n-i;j++)
{
if(strcmp(a[j],a[j+1])>0)
{
strcpy(t,a[j]);
strcpy(a[j],a[j+1]);
strcpy(a[j+1],t);
}
}
for(i=0;i<=n;i++)
puts(a[i]);
getch();
}
-----------------------------------------------------------------------------------Pass parameters to function using call by value and call by reference
#include<stdio.h>
#include<conio.h>
void swap(int,int);
void swapptr(int *,int *);
void main()
{
int x,y;
clrscr();
printf("\n");
printf("\nEnter value of x=");
scanf("%d",&x);
printf("\nEnter value of y=");
scanf("%d",&y);
printf("\nBefore swapping: x=%d y=%d",x,y);
printf("\n\nAfter swapping through call by value: ");
swap(x,y);
printf("\n\nAfter swapping through call by Reference:");
swapptr(&x,&y);
printf("\n");
getch();
}
void swap(int a,int b)
{
int t;
t=a;
a=b;
b=t;
printf("\nx=%d y=%d",a,b);
}
void swapptr(int *a,int *b)
{
int t;
t=*a;
*a=*b;
*b=t;
printf("\nx=%d y=%d",*a,*b);
}
-----------------------------------------------------------------------------------check whether given number is prime or not and generate prime numbers within range
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int n,a,b,i,j,flag,flag1,x;
printf("Enter a no : \n");
scanf("%d",&a);
for(i=2;i<a;i++)
{
if(a%i==0)
flag=1;
}
if(flag==1)
printf("No is not prime");
else
printf("No is prime");
printf("\nEnter the range to find prime nos\n");
scanf("%d %d",&a,&b);
for(i=a;i<b;i++)
{
flag1=0;
for(j=2;j<i/2;j++)
{
if(i%j==0)
{
flag1=1;
break;
}
else
flag=0;
}
if(flag1==0)
printf("%d\t",i);
}
getch();
}
-----------------------------------------------------------------------------------Use of Structure
(structure that can describe hotel.it have members that include name, address, grade, average room charge and no. of rooms)
#include<stdio.h>
#include<conio.h>
struct hotel
{
char name[15];
char add[30];
int grade;
long charge;
int no;
}s[20];
void Grad(int,struct hotel[],int);
void main()
{
int i,j,n,g,count=1;
long c;
char str[15];
struct hotel t;
clrscr();
printf("How many Hotels' details you want to enter=");
scanf("%d",&n);
for(i=0;i<n;i++)
{
gets("");
printf("\n %d)Enter hotel name:",i+1);
gets(s[i].name);
printf("\n Enter hotel address:",i+1);
gets(s[i].add);
printf("\n Enter hotel grade:",i+1);
scanf("%d",&s[i].grade);
printf("\n Enter average hotel charge:",i+1);
scanf("%ld",&s[i].charge);
printf("\n Enter number of hotel rooms:",i+1);
scanf("%d",&s[i].no);
}
for(i=0;i<n;i++)
{
for(j=0;j<n-1;j++)
{
if(s[j].charge>s[j+1].charge)
{
t=s[j];
s[j]=s[j+1];
s[j+1]=t;
}
}
}
printf("\n\nEnter your affordable charge=");
scanf("%ld",&c);
printf("\nHotels having charge below %ld are:",c);
for(i=0;i<n;i++)
{
if(s[i].charge<c)
{
printf("\n%d) Name : %s",count,s[i].name);
printf("\n Address: %s",s[i].add);
printf("\n Grade : %d",s[i].grade);
printf("\n Charge : %ld",s[i].charge);
printf("\n Rooms : %d",s[i].no);
printf("\n");
count++;
}
}
if(count==1)
printf(" Not found.");
printf("\nEnter grade of hotel you want=");
scanf("%d",&g);
Grad(g,s,n);
getch();
}
void Grad(int g,struct hotel s[15],int n)
{
int i,count=1;
for(i=0;i<n;i++)
{
if(s[i].grade==g)
{
printf("\n%d) Name : %s",count,s[i].name);
printf("\n Address: %s",s[i].add);
printf("\n Grade : %d",s[i].grade);
printf("\n Charge : %ld",s[i].charge);
printf("\n Rooms : %d",s[i].no);
printf("\n");
count++;
}
}
if(count==1)
{
printf("\nResult not found.");
}
}
-----------------------------------------------------------------------------------Store records of a professor in a file
(a. insert a record
b. delete a record
c. display a record)
#include<stdio.h>
#include<string.h>
#include<fcntl.h>
#include<conio.h>
void main()
{
struct professor
{
int pid;
char pname[20];
char quali[20];
int yrs;
char sub[20];
long sal;
int flag;
}p;
int temflag=0;
FILE *fp;
int choice,pno,pos;
int val=1,del;
char ans='y',ch='y';
clrscr();
while(ans=='y')
{
printf("\n\nChoices are:");
printf("\n1]Insert a record:");
printf("\n2]Display record:");
printf("\n3]Delete a record:");
printf("\nEnter the choice:");
scanf("%d",&choice);
switch(choice)
{
case 1:
{
do
{
fp=fopen("profr.dat","a");
if(ch=='y')
{
printf("\nEnter details of professor:");
scanf("%d %s %s %d %s %ld %d",&p.pid,p.pname,p.quali,&p.yrs,p.sub,&p.sal,&p.flag);
fwrite(&p,sizeof(struct professor),1,fp);
}
flushall();
printf("\nDo you want to add more records(y/n)?");
scanf("%c",&ch);
}while(ch=='y');
pos=ftell(fp);
fclose(fp);
val=0;
break;
}
case 2:
{
fp=fopen("profr.dat","r");
if(val==1)
{
while(!feof(fp))
{
fread(&p,sizeof(struct professor),1,fp);
}
pos=ftell(fp);
rewind(fp);
}
while(ftell(fp)<pos)
{
fread(&p,sizeof(struct professor),1,fp);
if(p.flag==1)
{
printf("\n%d %s %s %d %s %ld %d\n",p.pid,p.pname,p.quali,p.yrs,p.sub,p.sal,p.flag);
}
}
printf("\np=%d",pos);
fclose(fp);
break;
}
case 3:
{
fp=fopen("profr.dat","r+w");
printf("\nEnter pid to be deleted:");
scanf("%d",&pno);
while(!feof(fp))
{
del=0;
fread(&p,sizeof(struct professor),1,fp);
if(p.flag==1)
{
if(p.pid==pno)
{
rewind(fp);
p.flag=temflag;
fwrite(&p,sizeof(struct professor),1,fp);
del=1;
}
}
if(p.flag==1&&del==1)
{break;
}
}
printf("\nRecord deleted successfully:");
fclose(fp);
break;
}
}
flushall();
printf("\nDo you want to continue?(y/n)");
scanf("%c",&ans);
}
}
-----------------------------------------------------------------------------------
Program to merge 2 files (STUDENT ROLL NO,NAME and marks of 3 subjects)
#include<stdio.h>
#include<conio.h>
#include<fcntl.h>
void main()
{struct student
{char name[20];
int rollno;
int sub1,sub2,sub3;
}s;
FILE *fp1,*fp2,*fp3;
int pos;
char ans='y';
clrscr();
printf("\nEntering Details of 1st File.....");
printf("\nEntering Student Details.....");
fp1=fopen("stu1.dat","a");
while(ans=='y')
{printf("\nEnter student details=");
scanf("%s %d %d %d %d",s.name,&s.rollno,&s.sub1,&s.sub2,&s.sub3);
fwrite(&s,sizeof(struct student),0,fp1);
flushall();
printf("\nDo you want to continue(y/n)?");
scanf("%c",&ans);
}
fclose(fp1);
printf("\nEntering Details of 2 File.....");
printf("\nEntering Student Details.....");
ans='y';
fp2=fopen("stu2.dat","a");
while(ans=='y')
{printf("\nEnter student details=");
scanf("%s %d %d %d %d",s.name,&s.rollno,&s.sub1,&s.sub2,&s.sub3);
fwrite(&s,sizeof(struct student),0,fp2);
flushall();
printf("\nDo you want to continue(y/n)?");
scanf("%c",&ans);
}
fclose(fp2);
fp1=fopen("stu1.dat","r");
fp2=fopen("stu2.dat","r");
fp3=fopen("stu3.dat","a");
printf("\n\ncoping file 1 to file 3");
while(!feof(fp1))
{fread(&s,sizeof(struct student),1,fp1);
fwrite(&s,sizeof(struct student),1,fp3);
}
printf("\n\ncoping file 2 to file 3");
while( !feof(fp2) )
{fread(&s,sizeof(struct student),1,fp2);
fwrite(&s,sizeof(struct student),1,fp3);
}
printf("\n\ncoping complete:");
printf("\n\nDisplaying the contents of file 3....");
fclose(fp1);
fclose(fp2);
fp3=fopen("stu3.dat","r");
while(!feof(fp3))
{fread(&s,sizeof(struct student),1,fp3);
printf("\n%s %d %d %d %d",s.name,s.rollno,s.sub1,s.sub2,s.sub3);
}
fclose(fp3);
getch();
}
-----------------------------------------------------------------------------------Convert Decimal No. To Binary
#include<stdio.h>
#include<conio.h>
void main()
{
int dec,x,rem,i=1;
long int b=0;
clrscr();
printf("Enter the decimal no.");
scanf("%d",&dec);
do
{
rem=dec%2;
dec=dec/2;
b=b+(rem*i);
i=i*10;
}while(dec!=1);
b=b+i;
printf("%ld",b);
getch();
}
-----------------------------------------------------------------------------------Delete the duplicate values from array
#include<conio.h>
#include<stdio.h>
void main()
{
int i,j,k,a[20],n;
clrscr();
printf("Enter the no elements");
scanf("%d",&n);
for(i=0;i<n;i++)
{ printf("Enter No%d :",i+1);
scanf("%d",&a[i]);
}
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
if(a[i]==a[j])
{
for(k=j;k<n;k++)
{
a[k]=a[k+1];
}
n--;
j--;
}
}
}
printf("\nNow THe Array Elements Are\n");
for(i=0;i<n;i++)
{
printf("%d",a[i]);
}
getch();
}
-----------------------------------------------------------------------------------Find Out Largest and Second Largest In an Array
#include<conio.h>
#include<stdio.h>
void main()
{
int i,l,s,a[20],n;
clrscr();
printf("Enter the no elements");
scanf("%d",&n);
for(i=0;i<n;i++)
{ printf("Enter No%d :",i+1);
scanf("%d",&a[i]);
}
l=a[0];
for(i=1;i<n;i++)
{
if(s<l&&s<a[i])
{
s=a[i];
}
if(l<a[i])
{ s=l;
l=a[i];
}
}
printf("\nSecond Largest NO: %d \nLargest NO: %d",s,l);
getch();
}
-----------------------------------------------------------------------------------Permutation and combination
#include<stdio.h>
#include<conio.h>
void main()
{ int n,r,i,j;
long int num=1,pdenm=1,cdenm=1;
double p,c,x;
clrscr();
printf("Enter the value of n and r");
scanf("%d%d",&n,&r);
printf("%d,%d",n,r);
for(i=1;i<=n;i++)
num=num*i;
printf("\n%ld\n",num);
if((n-r)==0)
{
pdenm=1;
printf("%ld\n",pdenm);
}
else
{
for(j=1;j<=(n-r);j++)
pdenm=pdenm*j;
}
printf("%ld\n",pdenm);
p=num/pdenm;
printf("\npermutation =%lf\n",p);
for(i=1;i<=r;i++)
cdenm=cdenm*i;
printf("%ld\n",cdenm);
printf("%ld\n",pdenm);
x=(cdenm*pdenm);
printf("\n%d,%lf",num,x);
c=num/x;
printf("\nCombination =%lf",c);
getch();
}
-----------------------------------------------------------------------------------Check whether The No. Is Palindrome or Not
#include<stdio.h>
#include<conio.h>
void main()
{ int a,rem,rev=0,num;
clrscr();
printf("enter the no.");
scanf("%d",&a);
num=a;
while(a!=0)
{ rem=a%10;
a=a/10;
rev=(rev*10)+rem;
}
if(rev==num)
{ printf("\nNo. is a palandrome No.");
}
else
printf("\nNot An Palandrome No.");
getch();
}
-----------------------------------------------------------------------------------Swap Two No. without Using Third Variable
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
clrscr();
printf("Enter the No. A,B");
scanf("%d%d",&a,&b);
a=a+b;
b=a-b;
a=a-b;
printf("%d ",a);
printf("%d",b);
getch();
}
-----------------------------------------------------------------------------------Convert centigrade into Fahrenheit
#include<stdio.h>
#include<conio.h>
void main()
{
float fen,cen,con=9/5.0;
clrscr();
printf("%f",con);
printf("Enter the tem in cent.");
scanf("%f",&cen);
fen=(cen*con)+32;
printf("THe Temp in Fernht is=%f",fen);
getch();
}
-----------------------------------------------------------------------------------Quadratic Equation
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int a,b,c;
float root,root1,root2;
double d;
clrscr();
printf("\nEnter the values of a,b,c:");
scanf("%d%d%d",&a,&b,&c);
if(a==0)
{
printf("\nThe following equation is a linear equation:");
root=(float)-c/b;
printf("\n Root:%f",root);
}
else
{
d=((b*b)-(4*a*c));
if(d>0)
{
root1=(-b+sqrt(d))/2*a;
root2=(-b-sqrt(d))/2*a;
printf("\n First Root:%lf and Second Root:%lf",root1,root2);
}
if(d<0)
{
double d2;
d2=-d;
root1=-b/2*a;
root2=root1;
d2=sqrt(d2);
printf("\n First Root:%lf+%lfi",root1,d2);
printf("\n Second Root:%lf-%lfi",root2,d2);
}
else if(d==0)
{
root1=-b/2*a;
printf("\n The Roots are Equal and are %lf %lf",root1,root1);
}
}
getch();
}
-----------------------------------------------------------------------------------Diamond
1
1 2
1 2 3
1 2
1
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
clrscr();
for(a=1;a<=9;a++)
{
printf("\n");
for(c=9-a;c>=0;c--)
{
printf(" ");
}
for(b=1;b<=a;b++)
{
printf(" %d",b);
}
}
for(a=1;a<=8;a++)
{
printf("\n");
for(c=8-a;c<=8;c++)
{
printf(" ");
}
for(b=1;b<=9-a;b++)
{
printf(" %d",b);
}
}
getch();
}
Simple And Compound Interest
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
float p,n,r,si,ci,a;
clrscr();
printf("Enter the Principle, Number of years, Rate of Interest:");
scanf("%f%f%f",&p,&n,&r);
si=(p*n*r)/100;
printf("\nSimple Interest:%f",si);
a=p*pow(1+r/100,n);
ci=a-p;
printf("\nCompound Interest:%f",ci);
getch();
}
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.