Programming for Problem Solving (9)
Programming for Problem Solving (9)
Practical File
of
“OBJECT ORIENTED PROGRAMMING
WITH JAVA LAB (BCS-452)”
B.Tech IInd Year
Semester :IVth
Session : 2023-24(Even)
SOURCE CODE:
#include<stdio.h>
#include<conio.h>
void main()
inta,b,c,d,e,sum,perc;
intclrscr();
scanf("%d",&a);
scanf("%d",&b);
scanf("%d",&c);
scanf("%d",&d);
scanf("%d",&e);
sum=(a+b+c+d+e);
perc=(sum*100/500);
intgetch();
OUTPUT:
PROGRAM-2: WAP that calculates the Simple Interest and Compound Interest. The Principal
, Amount , Rate of Interest and Time are entered through the keyboard.
SOURCE CODE:
#include <stdio.h>
#include <conio.h>
#include <math.h>
int main()
floatp,r,t,amount,si,ci;
intclrscr();
scanf("%f",&p);
scanf("%f",&r);
scanf("%f",&t);
si=(p*r*t)/100;
amount=p*((pow((1+r/100),t)));
ci=amount-p;
int getch();
OUTPUT:
PROGRAM-3: WAP to calculate the area and circumference of a circle
SOURCE CODE:
#include <stdio.h>
#include <conio.h>
int main()
floatr,pi=3.14,a,c;
intclrscr();
printf("\tAREA OF CIRCLE!\n");
scanf("%f",&r);
a=pi*r*r;
c=2*pi*r;
intgetch();
OUTPUT:
PROGRAM-4: WAP that accepts the temperature in Centigrade and converts into Fahrenheit
using the formula C/5=(F-32)/9.
SOURCE CODE:
#include <stdio.h>
#include <conio.h>
void main()
floatc,f;
intclrscr();
printf("\tTEMPRATURE CONVERSION!\n");
scanf("%f",&c);
f=((9*c)/5)+32;
intgetch();
OUTPUT:
PROGRAM-5: WAP that swaps values of two variables using a third variable.
SOURCE CODE:
#include <stdio.h>
#include <conio.h>
int main()
doublea,b,c;
intclrscr();
scanf("%lf",&a);
scanf("%lf",&b);
c=a;
a=b;
b=c;
printf("After swaping\n");
return 0;
OUTPUT:
PROGRAM-6: WAP that checks whether the two numbers entered by the user are equal or
not.
SOURCE CODE:
#include<stdio.h>
#include<conio.h>
int main()
inta,b;
intclrscr();
printf("\tEQUAL NUMBERS\n");
scanf("%d",&a);
scanf("%d",&b);
if (a == b)
else
intgetch();
OUTPUT:
PROGRAM-7: WAP to find the greatest of three numbers.
SOURCE CODE:
#include <stdio.h>
int main()
int A, B, C;
return 0;
OUTPUT:
PROGRAM-8: WAP that finds whether a given number is even or odd.
SOURCE CODE:
#include<stdio.h>
#include<conio.h>
int main()
intnum;
printf("\tEVEN OR ODD\n");
scanf("%d",&num);
if (num%2==0)
printf("NUMBER %d IS EVEN",num);
else
printf("NUMBER %d IS ODD",num);
return 0;
OUTPUT:
PROGRAM-9:WAP that tells whether a given year is a leap year or not.
SOURCE CODE:
#include <stdio.h>
int main()
int year;
printf("\tLEAP YEAR\n");
scanf("%d", &year);
if (year % 400 == 0)
else if (year % 4 == 0)
else
return 0;
OUTPUT:
PROGRAM-10:WAP that accepts marks of five subjects and finds percentage and prints grades
according to the following criteria:
Between 90-100%-----Print ‘A’
80-90%-----------------Print ‘B’
60-80%-----------------Print ‘C’
Below 60%-------------Print ‘D’
SOURCE CODE:
#include<stdio.h>
#include<conio.h>
int main()
inta,b,c,d,e,sum,perc;
scanf("%d",&a);
scanf("%d",&b);
scanf("%d",&c);
scanf("%d",&d);
scanf("%d",&e);
sum=(a+b+c+d+e);
perc=(sum*100/500);
if(perc>=90&&perc<=100)
printf("Grade A");
else if(perc>=80&&perc<90)
printf("Grade B");
else if(perc>=60&&perc<80)
printf("Grade C");
}
else if(perc<60)
printf("Grade D");
return 0;
OUTPUT:
PROGRAM-11:WAP that takes two operands and one operator from the user, perform the operation,
and prints the result by using Switch statement.
SOURCE CODE:
#include<stdio.h>
#include<conio.h>
int main()
int num1,num2,c,choice;
printf("\tOPERATORS\n");
scanf("%d",&num1);
scanf("%d",&num2);
printf("SELECT OPERATOR:\n");
printf("ADDITION =1\n");
printf("SUBSTRACT=2\n");
printf("MULTIPLY =3\n");
printf("DIVISION =4\n");
scanf("%d",&choice);
switch(choice)
case 1:
c=num1+num2;
printf("SUM=%d\n",c);
break;
case 2:
c=num1-num2;
printf("SUBSTRACTION=%d\n",c);
break;
case 3:
c=num1*num2;
printf("MULTIPLY=%d\n",c);
break;
case 4:
c=num1/num2;
printf("DIVISION=%d\n",c);
break;
default:
return 0;
OUTPUT:
PROGRAM-12:WAP to print the sum of all numbers up to a given number.
SOURCE CODE:
#include <stdio.h>
#include <conio.h>
void main()
intnum, i, sum = 0;
scanf("%d", &num);
sum = sum + i;
intgetch();
OUTPUT:
PROGRAM-13:WAP to find the factorial of a given number.
SOURCE CODE:
#include<stdio.h>
#include<conio.h>
int main()
inti=1,fact=1,n;
printf("\tFACTORIAL OF A NUMBER\n");
printf("ENTTER A NUMBER:");
scanf("%d",&n);
while (i<=n)
fact=fact*i;
i++;
printf("FACTORIAL OF %d IS %d\n",n,fact);
return 0;
OUTPUT:
PROGRAM-14:WAP to print sum of even and odd numbers from 1 to N
numbers.
SOURCE CODE:
#include <stdio.h>
void main()
scanf("%d", &num);
if (i % 2 == 0)
sumE = sumE + i;
else
sumO = sumO + i;
OUTPUT:
PROGRAM-15:WAP to print the Fibonacci series.
SOURCE CODE:
#include <stdio.h>
int main()
printf("\tFIBONACCI SERIES\n");
scanf("%d", &n);
t1 = t2;
t2 = nextTerm;
nextTerm = t1 + t2;
return 0;
OUTPUT:
PROGRAM-16:WAP to check whether the entered number is prime or not.
SOURCE CODE:
#include <stdio.h>
int main()
int n, i, c = 0;
scanf("%d", &n);
if (n % i == 0)
c++;
if (c == 2)
else
return 0;
OUTPUT:
PROGRAM-17:WAP to find the sum of digits of the entered number.
SOURCE CODE:
#include<stdio.h>
int main()
intn,sum=0,m;
printf("\tSUM OF DIGITS\n");
printf("Enter a number:");
scanf("%d",&n);
while(n>0)
m=n%10;
sum=sum+m;
n=n/10;
printf("Sum is=%d",sum);
return 0;
OUTPUT:
PROGRAM-18: WAP to find the reverse of a number.
SOURCE CODE:
#include <stdio.h>
int main()
printf("\tREVERSE OF A NUMBER\n");
scanf("%d", &number);
while(number !=0)
number /= 10;
return 0;
OUTPUT:
PROGRAM-19:WAP to print Armstrong numbers from 1 to 100.
SOURCE CODE:
#include <stdio.h>
int main()
inti,n,r,sum=0;
for (i=1;i<=100;i++)
sum=0;
n=i;
while(n!=0)
r=n%10;
sum=sum+(r*r*r);
n=n/10;
if(i==sum)
printf("%d",i);
return 0;
OUTPUT:
PROGRAM-20:WAP to convert binary number into decimal number and vice
versa.
SOURCE CODE:
~BINARY TO DECIMAL:
#include <stdio.h>
#include <conio.h>
void main()
printf (" Enter a binary number with the combination of 0s and 1s: ");
binary_num = num;
while ( num> 0)
base = base * 2;
intgetch();
OUTPUT:
~DECIMAL TO BINARY :
#include <stdio.h>
int main()
printf("\tDECIMAL TO BINARY\n");
scanf("%d", &number);
a[i] = number % 2;
number = number / 2;
printf("\n");
return 0;
OUTPUT:
PROGRAM-21:WAP that simply takes elements of the array from the user and finds the sum of these
elements.
SOURCE CODE:
#include<stdio.h>
int main()
scanf("%d",&size);
scanf("%d",&arr[i]);
return 0;
OUTPUT:
PROGRAM-22:WAP that inputs two arrays and saves sum of corresponding elements of these arrays in
a third array and prints them.
SOURCE CODE:
#include<stdio.h>
void main()
int i,ar1[5],ar2[5],sum[5];
printf("\tSUM OF ARRAYS\n");
for(i=0;i<=4;i++)
printf("ar1[%d]=",i);
scanf("%d",&ar1[i]);
for(i=0;i<=4;i++)
printf("ar2[%d]=",i);
scanf("%d",&ar2[i]);
for(i=0;i<=4;i++)
sum[i]=ar1[i]+ar2[i];
printf("Sum of arrays:-");
for(i=0;i<=4;i++)
printf("\nsum[%d]=%d",i,sum[i]);
OUTPUT:
PROGRAM-23:WAP to find the minimum and maximum element of the array.
SOURCE CODE:
#include <stdio.h>
#include <conio.h>
int main()
int a[1000],i,n,min,max;
scanf("%d",&n);
scanf("%d",&a[i]);
min=max=a[0];
if(min>a[i])
min=a[i];
if(max<a[i])
max=a[i];
return 0;
OUTPUT:
PROGRAM-24:WAP to search an element in a array using Linear Search.
SOURCE CODE:
#include <stdio.h>
int main()
printf("\tLINEAR SEARCH\n");
scanf("%d",&number);
scanf("%d",&array[c]);
scanf("%d",&search);
if ( array[c] == search )
break;
if ( c == number )
return 0;
OUTPUT:
PROGRAM-25:WAP to sort the elements of the array in ascending order using Bubble Sort
technique.
SOURCE CODE:
#include <stdio.h>
int main()
printf("\tBUBBLE SORT\n");
scanf("%d", &n);
scanf("%d", &array[c]);
swap = array[d];
array[d] = array[d+1];
array[d+1] = swap;
printf("%d\t", array[c]);
return 0;
OUTPUT:
PROGRAM-26:WAP to add and multiply two matrices of order nxn.
SOURCE CODE:
MULTIPLY OF MATRICES:
#include<stdio.h>
int main()
int a[10][10],b[10][10],mul[10][10],r,c,i,j,k;
printf("\tMULTIPLYING OF MATRICES\n");
scanf("%d",&r);
scanf("%d",&c);
for(i=0;i<r;i++)
for(j=0;j<c;j++)
scanf("%d",&a[i][j]);
for(i=0;i<r;i++)
for(j=0;j<c;j++)
scanf("%d",&b[i][j]);
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
mul[i][j]=0;
for(k=0;k<c;k++)
mul[i][j]+=a[i][k]*b[k][j];
for(i=0;i<r;i++)
for(j=0;j<c;j++)
printf("%d\t",mul[i][j]);
printf("\n");
return 0;
OUTPUT:
ADDITION OF MATRICES:
#include<stdio.h>
int main()
printf("\tADDITION OF MATRICES\n");
printf("%d\t", sum[c][d]);
printf("\n");
return 0;
OUTPUT:
PROGRAM-27:WAP that finds the sum of diagonal elements of amxn matrix.
SOURCE CODE:
#include<stdio.h>
void main()
int mat[12][12];
inti,j,row,col,sum=0;
scanf("%d%d",&row,&col);
for(i=0;i<row;i++)
for(j=0;j<col;j++)
scanf("%d",&mat[i][j]);
printf("The matrix:\n");
for(i=0;i<row;i++)
for(j=0;j<col;j++)
printf("%d\t",mat[i][j]);
printf("\n");
for(i=0;i<row;i++)
for(j=0;j<col;j++)
if(i==j)
sum=sum+mat[i][j];
}
OUTPUT: