Practical File of "Programming For Problem Solving": Manjit Singh
Practical File of "Programming For Problem Solving": Manjit Singh
Practical File of "Programming For Problem Solving": Manjit Singh
File
Of
“Programming for problem solving”
#include<stdio.h>
#include<conio.h>
void main()
{
float p,r,t,si;
clrscr();
printf("ENTER THE VALUES OF PRINCIPAL ,rate and time period = ");
scanf("%f%f%f",&p,&r,&t);
si=(p*r*t)/100;
printf("\n SIMPLE INTREST = %f",si);
getch();
}
Output:
Program 2.write a program to find greater of 3 numbers.
#include<stdio.h>
#include<conio.h>
void main()
{
inta,b,c;
clrscr();
printf("ENTER THE THREE NUMBERS = ");
scanf("%d%d%d",&a,&b,&c);
if(a>b && a>c)
{
printf("\n %d is the greater of the three numbers ",a);
}
else if(b>a && b>c)
{
printf("\n %d is greater of the three numbers ",b);
}
else printf("\n %d is greater of the three numbers ",c);
getch();
}
Output:
Program 3.Write a program to assign division on the basis of marks of 5
subjects using if-else.
#include<stdio.h>
#include<conio.h>
void main()
{
float m1,m2,m3,m4,m5,per;
clrscr();
printf("enter the marks of five subjects = ");
scanf("%f%f%f%f%f",&m1,&m2,&m3,&m4,&m5);
per=((m1+m2+m3+m4+m5)/500)*100;
if(per>=60)
{
printf("FIRST DIVISION");
}
else
{
if(per>=50)
{
printf("SECOND DIVISION");
}
else
{
if(per>=40)
{
printf("THIRD DIVISION");
}
else
{
printf("FAIL");
}
}
}
getch();
}
Output:
Program 4.Write a program to calculate S.I using while loop.
#include<stdio.h>
#include<conio.h>
void main()
{
inti=0,n;
float p,r,t,si;
clrscr();
printf("ENTER interst OF HOW MANY perosns you want to calculate = ");
scanf("%d",&n);
while(i<n)
{
printf("\n ENTER THE VALUES OF PRINCIPAL ,rate and time period for person %d = ",i+1);
scanf("%f%f%f",&p,&r,&t);
si=(p*r*t)/100;
printf("\n SIMPLE INTREST = %f",si);
i++;
}
getch();
}
Output:
Program 5.Write a program to calculate S.I using do while loop.
#include<stdio.h>
#include<conio.h>
void main()
{
inti=0,n;
float p,r,t,si;
clrscr();
printf("ENTER interst OF HOW MANY perosns you want to calculate = ");
scanf("%d",&n);
do
{
printf("\n ENTER THE VALUES OF PRINCIPAL ,rate and time period for person %d = ",i+1);
scanf("%f%f%f",&p,&r,&t);
si=(p*r*t)/100;
printf("\n SIMPLE INTREST = %f",si);
i++;
}while(i<n);
getch();
}
Output:
Program 6. write a program for sum of n natural number.
#include<stdio.h>
#include<conio.h>
void main()
{
inti,n,sum=0;
clrscr();
printf("enter the value of n = \n");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
sum=sum+i;
}
printf("sum=%d",sum);
getch();
}
Output:
program 7. write a program for multiplication of n natural number.
#include<stdio.h>
#include<conio.h>
void main()
{
inti,n,pro=1;
clrscr();
printf("enter the value of n = \n");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
pro=pro*i;
}
printf("product=%d",pro);
getch();
}
Output:
Program 8. write a program for a calculator using switch case.
#include<stdio.h>
#include<conio.h>
void main()
{
char opr;
float n1,n2;
clrscr();
printf("enter the operator (+,-,*,/)= ");
scanf("%c",&opr);
printf("\n enter the operand = ");
scanf("%f%f",&n1,&n2);
switch (opr)
{
case'+':
{
printf("%f+%f=%f",n1,n2,n1+n2);
break;
}
case'-':
{
printf("%f-%f=%f",n1,n2,n1-n2);
break;
}
case'*':
{
printf("%f*%f=%f",n1,n2,n1*n2);
break;
}
case'/':
{
printf("%f/%f=%f",n1,n2,n1/n2);
break;
}
default:
{
printf("error! operator is not correct");
}
}
getch();
}
Output:
Program 9. write a program for pre increment or post increment.
#include<stdio.h>
#include<conio.h>
void main()
{
int a=10,b=5,x,y;
clrscr();
x=++a + ++a + b++;
printf("THE X= %d",x);
y=++b + ++a + ++a + b++;
printf("\n the Y = %d",y);
getch();
}
Output:
Program 10. write a program for sorting the n numbers using array.
#include<stdio.h>
#include<conio.h>
void main()
{
int a[30],n,i,j,temp;
clrscr();
printf("ENTER HOW MANY ELEMENTS YOU WANT TO ENTER IN THE LIST = ");
scanf("%d",&n);
printf("ENTER THE LIST = ");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
Output:
Program 11. write a program for linear search.
#include<stdio.h>
#include<conio.h>
void main()
{
int a[20],n,i,ser,k=0;
clrscr();
printf("ENTER HOW many elements you want you want to enter= ");
scanf("%d",&n);
printf("\nENTER THE ARRAY ELEMENTS = ");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("\nYOU ENTERED = ");
for(i=0;i<n;i++)
{
printf("%d ",a[i]);
}
printf("\nENTER THE NUMBER YOU WANT TO SEARCH =");
scanf("%d",&ser);
for(i=0;i<n;i++)
{
if(a[i]==ser)
{ k=1;
printf("\nELEMENT FOUND at position %d",i+1);
}
}
if(k==0)
{
printf("\nELEMENT NOT FOUND ");
}
getch();
}
Output:
Program 12. write a program for binary search.
#include<stdio.h>
#include<conio.h>
void main()
{
int a[20],n,ser,i,mid,end,beg,k=0;
clrscr();
printf("ENTER HOW MANY ELEMENTS YOU WANT TO ENTER IN THE ARRAY = ");
scanf("%d",&n);
printf("ENTER THE ARRAY = ");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("\n YOU ENTERED = ");
for(i=0;i<n;i++)
{
printf("%d ",a[i]);
}
printf("\nENTER THE ELEMENT YOU WANT TO SEARCH = ");
scanf("%d",&ser);
beg=0;
end=n-1;
while(beg<=end)
{
mid=(beg+end)/2;
if(a[mid]==ser)
{k=1;
printf("\n ELEMENT IS FOUND AT %d position ",mid+1); break;}
else if(a[mid]>ser)
end=mid-1;
else beg=mid+1;
}
if(k==0)
{
printf("\n NUMBER NOT FOUND ");
}
getch();
}
Output:
Program 13. write a program for addition of 2D matrix.
#include<stdio.h>
#include<conio.h>
void main()
{int a[3][3],b[3][3],c[3][3],i,j;
clrscr();
printf("enter the elements of 1st matrix=");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("\n%d",&a[i][j]);
}
}
printf("\n enter the elements of 2nd matrix=");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("\n %d ",&b[i][j]);
}
}
printf("matrix 1=");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("%d ",a[i][j]);
}
printf("\n");
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("%d ",b[i][j]);
}
printf("\n");
}
for(i=0;i<3;j++)
{
for(j=0;j<3;j++)
{
c[i][j]=a[i][j]+b[i][j];
}
}
printf("\n sum ofmatrix :\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("%d ",c[i][j]);
}
printf("\n");
}
getch();
}
Output:
Program 14. write a program for multiplication of 2D matrix.
#include<stdio.h>
#include<conio.h>
void main()
{int a[3][3],b[3][3],c[3][3],i,j,k;
clrscr();
printf("enter the elements of 1st matrix=");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("\n%d",&a[i][j]);
}
}
printf("\n enter the elements of 2nd matrix=");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("\n %d ",&b[i][j]);
}
}
printf("\nmatrix 1=\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("%d ",a[i][j]);
}
printf("\n");
}
printf("\nmatrix 2=\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("%d ",b[i][j]);
}
printf("\n");
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{ c[i][j]=0;
for(k=0;k<3;k++)
{
c[i][j]=c[i][j]+a[i][k]*b[k][j];
}
printf("\n");
}
}
printf("\n pr0duct ofmatrix :\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("%d ",c[i][j]);
}
printf("\n");
}
getch();
}
Output:
Program 15. write a program of addition of two numbers using function (call
by value).
#include<stdio.h>
#include<conio.h>
int sum(intx,int y);
void main()
{
inta,b,s;
clrscr();
printf("enter the numbers=");
scanf("%d%d",&a,&b);
s=sum(a,b);
printf("\n sum of a+b =%d",s);
getch();
}
int sum(intx,int y)
{ int k;
k=x+y;
return k;
}
Output:
Program 16. write a program of addition of two numbers using function (call
by reference).
#include<stdio.h>
#include<conio.h>
int sum(int *a,int *b);
void main()
{
inta,b,s;
clrscr();
printf("enter the numbers =");
scanf("%d%d",&a,&b);
s=sum(&a,&b);
printf("\n sum of a+b =%d",s);
getch();
}
int sum(int *a,int *b)
{
int k;
k=*a+*b;
return k;
}
Output:
Program 17. write a program to find factorial using iterative .
#include<stdio.h>
#include<conio.h>
void main()
{
inti,a,fact=1;
clrscr();
printf("ENTER THE NUMBER = ");
scanf("%d",&a);
for(i=1;i<=a;i++)
{
fact=fact*i;
}
printf("FACTORIAL = %d",fact);
getch();
}
Output:
Program 18. write a program to find factorial using recursion.
#include<stdio.h>
#include<conio.h>
int rec(int x);
void main()
{
inta,fact;
clrscr();
printf(" enter any number ");
scanf("%d",&a);
fact=rec(a);
printf(" rec value=%d",fact);
getch();
}
int rec(int x)
{
int f;
if(x==1)
{
return (1);
}
else
{
f=x*rec(x-1);
}
return (f); }
Output:
Program 19. write a program accessing using one variable .
#include<stdio.h>
#include<conio.h>
void main()
{
inti=3;
int *j;
clrscr();
j=&i;
printf("\n address of i=%u",&i);
printf("\n address of i=%u",j);
printf("\n address of j=%u",&j);
printf("\n value of j=%u",j);
printf("\n value of i=%d",i);
printf("\n value of i=%d",*(&i));
printf("\n value of i=%d",*j);
getch();
}
Output:
Program 20. write a program for string function .
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char str1[30],str2[30],str3[30];
intcmp,len;
clrscr();
printf("enter the first string :\n");
gets(str1);
printf("you entered =");
puts(str1);
printf("enter the second string\n");
gets(str2);
printf("you entered=");
puts(str2);
strcpy(str3,str1);
printf("STRING 3 BECOMES = ");
puts(str3);
printf("\n comparing the str3 and str2...........=");
if(!strcmp(str3,str2))
printf("strings are identical");
else printf("not identical");
len=strlen(str1);
printf("\nstrlen(str1):=%d",len);
getch();
}
Output:
Program 21. write a program of string function using n.
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char str1[30],str2[30],str3[30];
intcmp,len,n;
clrscr();
#include<stdio.h>
#include<conio.h>
void main()
{
int a[50],n,i,j,pos,temp;
clrscr();
printf("enter the no. of element you want to enter in the list \n");
scanf("%d",&n);
printf("enter %d integers\n",n);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
for(i=0;i<n-1;i++)
{
pos=i;
for(j=i+1;j<n;j++)
{
if(a[pos]>a[j])
{
pos=j;
}
}
if(pos!=i)
{
temp=a[i];
a[i]=a[pos];
a[pos]=temp;
}
}
printf("sorted list:\n");
for(i=0;i<n;i++)
{
printf("%d\n",a[i]);
}
getch();}
Output:
Program 24 . write a program for bubble sort.
#include<stdio.h>
#include<conio.h>
void main()
{
int a[100],n,c,d,swap;
clrscr();
printf("enter the no. of element\n");
scanf("%d",&n);
printf("enter %d integer\n",n);
for(c=0;c<n;c++)
{
scanf("%d",&a[c]);
}
for(c=0;c<n;c++)
{
for(d=0;d<n-c-1;d++)
{
if(a[d]>a[d+1])
{
swap=a[d];
a[d]=a[d+1];
a[d+1]=swap;
}
}
}
printf("sorted list:\n");
for(c=0;c<n;c++)
{
printf("%d\n",a[c]);
}
getch();
}
Output:
Program 25. write a program for insertiom sort.
#include <stdio.h>
#include<conio.h>
void main()
{
int n, array[100], c, d, t;
d--;
}
}
getch();
}
Output: