Download as pdf or txt
Download as pdf or txt
You are on page 1of 15

PRACTICAL FILE

ON

PROGRAMMING FOR PROBLEM SOLVING LAB

(21BTCS181)

SCHOOL OF COMPUTER SCIENCE ENGINEERING

Submitted To: Submitted By: vaibhavi Singh


Dr. Luxmi Sapra Roll No.-21btcse0271
Associate Professor programme-B.Tech
Section : D
Experiment No. 1
Write a program to perform arithmetic operations in c.
#include<stdio.h>
#include<conio.h>
int main()
{
int num1, num2;
int sum, sub, mult, mod;
float div;
clrscr();

printf("enter num1: ");


scanf("%d",&num1);
printf("enter num2: ");
scanf("%d",&num2);

sum=num1+num2;
sub=num1-num2;
mult=num1*num2;
div=num1/num2;
mod=num1%num2;

printf("SUM=%d\n",sum);
printf("DIFFERENCE=%d\n",sub);
printf("PRODUCT=%d\n",mult);
printf("QUOTIENT=%d\n",div);
printf("MODULUS=%d\n",mod);

getch();
return 0;
}
Experiment No. 2
Write a program to swap two numbers with or without using third variable.
#include<stdio.h>
#include<conio.h>
int main()
{
int a, b, c=0;
printf("enter a: ");
scanf("%d",&a);
printf("enter b: ");
scanf("%d",&b);

c=a;
a=b;
b=c;

printf("after swaping\n");
printf("a=%d\n",a);
printf("b=%d\n",b);

getch();
return 0;
}
Experiment No. 3
Write a program to convert temperature from Celsius to Fahrenheit.
#include<stdio.h>
#include<conio.h>
int main()
{
clrscr();
float f,c;
printf("enter temperature in celsius: ");
scanf("%f",&c);

f=(c*1.8)+32;

printf("%.2f celsius= %.2f fahrenheit",c,f);

getch();
return 0;
}
Experiment No.4
Write a program to find largest number from three numbers.
#include<stdio.h>
#include<conio.h>
int main()
{
clrscr();
int n1, n2, n3;
printf("enter three numbers: ");
scanf("%d %d %d",&n1,&n2,&n3);

if(n1>n2)
{
if(n1>n3)
printf("%d is largest number",n1);
else
printf("%d is largest number",n3);
}
else
{
if(n2>n3)
printf("%d is largest number",n2);
else
printf("%d is largest number",n3);
}

getch();
return 0;
}
Experiment No. 5
Write a program to print Fibonacci series up to n terms.
#include<stdio.h>
#include<conio.h>
int main()
{
clrscr();
int i,n;
int a=0,b=1,c=0;
printf("enter number of terms: ");
scanf("%d",&n);
printf("fibonacci series: %d\t%d\t",a,b);
for(i=3;i<=n;i++)
{ c=a+b;
printf("%d\t",c);
a=b;
b=c;
}
getch();
return 0;
}

Experiment No. 6
Write a program to test whether a number entered by user is positive, negative
or equals to zero.
#include<stdio.h>
#include<conio.h>
int main()
{
int a;
printf("enter the value of a: ");
scanf("%d",&a);
if(a>0)
printf("%d is positive integer\n");
else if(a<0)
printf("%d is negative integer\n");
else
printf("%d is equals to zero\n");
getch();
return 0;
}

Experiment No.7
Write a program to create calculator program 1.Addition 2. Subtration 3.
Multiplication 4.division using switch case.
#include<stdio.h>
#include<conio.h>
int main()
{
int choice, a, b;
float result;
printf("enter 1 for addition ,2 for subtraction, 3 for multiplication, 4 for
division: ");
scanf("%d",&choice);
printf("enter a: ");
scanf("%d",&a);
printf("enter b: ");
scanf("%d",&b);
switch(choice)
{
case 1:
result=a+b;
break;
case 2:
result=a-b;
break;
case 3:
result=a*b;
break;
case 4:
result=a/b;
break;
default:
printf("enter valid choice");
}
printf("%f",result);
getch();
return 0;
}

Experiment No. 8
The Air Quality (AQI) shows the level of health concern for a particular area.
The level of air quality for a given AQI is mentioned as below.
AQI (Air Quality Index) Level of air Quality
>0 and <=50 Healthy
>50 and <=100 Moderate
>100 and <=200 Unhealthy
>200 and <=300 Very Unhealthy
>300 and <=500 Hazardous
Write a program to check the level of air quality depend upon the table.

#include<stdio.h>
#include<conio.h>
int main()
{
int AQI;
printf("enter air quality index: ");
scanf("%d",&AQI);
if(AQI>0 && AQI<=50)
printf("Healthy\n");
else if(AQI>50 && AQI<=100)
printf("Moderate\n");
else if(AQI>100 && AQI<200)
printf("Unhealthy\n");
else if(AQI>200 && AQI<300)
printf("Very Unhaelthy\n");
else if(AQI>300 && AQI<500)
printf("hazardous\n");
getch();
return 0;
}

Experiment No. 9
Write a program to search an element using linear search in an array.
#include<stdio.h>
#include<conio.h>
int main()
{ clrscr();
int num, arr[4];
int i, keynum, found=0;
printf("enter the number of elements: ");
scanf("%d",&num);
printf("enter the elements one by one:\n");
for(i=0;i<num;i++)
{ scanf("%d",&arr[i]);
}
printf("enter the element to be searched: ");
scanf("%d",&keynum);
for(i=0;i<num;i++)
{
if(keynum==arr[i])
{
found=1;
break;
}
}
if(found==1)
printf("element is present in the array at position %d",i+1);
else
printf("element is not present in the array\n");

getch();
return 0;
}

Experiment No. 10
Write a program to print the transpose of matrix.
#include<stdio.h>
#include<conio.h>
int main()
{
clrscr();
int n, m, i, j;
printf("enter no of rows and column: ");
scanf("%d %d",&m,&n);
int arr[10][10];
printf("\nEnter the elements of the matrix: \n");
for(i=0;i<m;i++)
{ for(j=0;j<n;j++)
{ scanf("%d",&arr[i][j]);
}
}
printf("\nThe elements of matrix are: \n");
for(i=0;i<m;i++)
{ for(j=0;j<n;j++)
{ printf("%d\t",arr[i][j]);
}
printf("\n");
}
int brr[10][10];
for(i=0;i<m;i++)
{ for(j=0;j<n;j++)
{ brr[j][i]=arr[i][j];
}
}
printf("\nTranspose of matrix is: \n");
for(i=0;i<m;i++)
{ for(j=0;j<n;j++)
{ printf("%d\t",brr[i][j]);
}
printf("\n");
}
getch();
return 0;
}
Experiment No. 11
Write a program to display the use of call by value and call by reference in C.

#include<stdio.h>
#include<conio.h>

void swap(int a,int b)


{
int temp;
temp=a;
a=b;
b=temp;
printf("\n values of 'a' and 'b' inside the function are %d and %d
respectively",a,b);
}
int main()
{
int a=10,b=20;
clrscr();
printf("\n call by value");
printf("\n initial values of 'a' and 'b' are %d and %d respectively",a,b);
swap(a,b);
printf("\n final values of 'a' and 'b' are %d and %d respectively",a,b);
getch();
return 0;
}
#include<stdio.h>
#include<conio.h>

void swap(int a,int b)


{
int temp;
temp=a;
a=b;
b=temp;
printf("\n values of 'a' and 'b' inside the function are %d and %d
respectively",a,b);
}
int main()
{
int a=10,b=20;
clrscr();
printf("\n call by value");
printf("\n initial values of 'a' and 'b' are %d and %d respectively",a,b);
swap(a,b);
printf("\n final values of 'a' and 'b' are %d and %d respectively",a,b);
getch();
return 0;
}
Experiment No. 12
Write a program to create a structure and print the elements of structure.

#include<stdio.h>
#include<conio.h>
struct stud
{
int roll_no;
char stud_name[20];
int std;
}s;
void student(struct stud s)
{
printf("Student Details:\n");
printf("-------------------\n");
printf("Roll Number: %d",s.roll_no);
printf("\nName : %s",s.stud_name);
printf("\nStd : %d",s.std);
}
int main()
{ clrscr();
printf("Enter Students Details\n");
printf("-----------------------\n");
printf("Roll Number: ");
scanf("%d",&s.roll_no);
printf("Name : ");
scanf("%s",s.stud_name);
printf("std. : ");
scanf("%d",&s.std);
printf("------------------------\n");
student(s);
getch();
return 0;
}

You might also like