POP LAB 1-5

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

Principles of Programming using C Lab

1:Develop a program to solve simple computational problems using arithmetic


expressions and use of each operator leading to simulation of a commercial
calculator. (No built-in math function)

#include<stdio.h>

#include<stdlib.h>

int main()

int num1,num2;

char choice;

float res;

printf("enter the your choice\n");

scanf("%c",&choice);

printf("enter the first number\n");

scanf("%d",&num1);

printf("enter the second number\n");

scanf("%d",&num2);

switch(choice)

case'+' : res=num1+num2;

break;

case'-' : res=num1-num2;

break;

case'*': res=num1*num2;

break;

case'/' : res=(float)num1/(float)num2; break;

case'%' : res=num1%num2;

Mrs. Arpitha J C, Assistant professor, Dept of CE, PESITM Shivamogga Page 1


Principles of Programming using C Lab

break;

default : printf("invalid operation\n");

Mrs. Arpitha J C, Assistant professor, Dept of CE, PESITM Shivamogga Page 2


Principles of Programming using C Lab

printf("result:%d %c %d = %f\n",num1,choice,num2,res); return 0;

Output:

vsk-10@ubuntu:~/VSK$ gedit program1.c

vsk-10@ubuntu:~/VSK$ gcc program1.c

vsk-10@ubuntu:~/VSK$ ./a.out

enter your choice

enter the first number 10

enter the second number 20

result:10 + 20 = 30.000000

vsk-10@ubuntu:~/VSK$ ./a.out

enter your choice

enter the first number 10

enter the second number 20

result:10 - 20 = -10.000000

vsk-10@ubuntu:~/VSK$ ./a.out

enter your choice

enter the first number

10

enter the second number 20

result:10 * 20 = 200.000000

vsk-10@ubuntu:~/VSK$ ./a.out

Mrs. Arpitha J C, Assistant professor, Dept of CE, PESITM Shivamogga Page 3


Principles of Programming using C Lab

enter your choice

enter the first number 10

enter the second number 20

result:10 / 20 = 0.500000

vsk-10@ubuntu:~/VSK$ ./a.out

enter your choice

enter the first number 10

enter the second number 20

result:10 % 20 = 10.000000

Mrs. Arpitha J C, Assistant professor, Dept of CE, PESITM Shivamogga Page 4


Principles of Programming using C Lab

2:An electricity board charges the following rates for the use of electricity: for
the first 200 units 80 paise per unit: for the next 100 units 90 paise per unit:
beyond 300 units Rs 1 per unit. All users are charged a minimum of Rs. 100 as
meter charge. If the total amount is more than Rs 400, then an additional
surcharge of 15% of the total amount is charged. Write a program to read the
name of the user, the number of units consumed, and print out the charges.

#include<stdio.h>

int main()

char name[10];

float unit,

metercharge=100;

printf("Enter your name:\n");

scanf("%s",name);

printf(“Unit consumed:\n”);

scanf(“%f”,&unit);

if(unit<=200)

metercharge=metercharge+(unit*.80);

else if(unit>200 && unit<=300)

metercharge= metercharge+(200*0.80) +((unit-200)*0.90);

else if(unit>300)

metercharge=metercharge+(200*0.80)+(100*0.90)+((unit -300)*1);

if(metercharge>400)

metercharge=metercharge+(metercharge*0.15);

printf("Name:%s\n Number of unit consumed:%f

MeterCharge:%f",name,unit,metercharge);

return 0;

Mrs. Arpitha J C, Assistant professor, Dept of CE, PESITM Shivamogga Page 5


Principles of Programming using C Lab

Output:

vsk-10@ubuntu:~/VSK$ ./a.out

Enter your name:Ram

Unit consumed:150

Name: Ram

Number of unit consumed: 150.000000

MeterCharge: 220.000000

vsk-10@ubuntu:~/VSK$ ./a.out

Enter your name :Som

unit Consumed: 250

Name: Som

Number of unit consumed: 250.000000

MeterCharge: 305.000000

vsk-10@ubuntu:~/VSK$ ./a.out

Enter your name:Sham

unit Consumed:500

Name: Sham

Number of unit consumed: 500.000000

MeterCharge: 632.500000

Mrs. Arpitha J C, Assistant professor, Dept of CE, PESITM Shivamogga Page 6


Principles of Programming using C Lab

3: Write a C Program to display the following by reading the number of rows as input.

#include <stdio.h>

void main()

int i,j,n;

printf("Input number of rows : ");

scanf("%d",&n);

for(i=0;i<=n;i++)

for(j=1;j<=n-i;j++)

printf(" ");

for(j=1;j<=i;j++)

printf("%d",j);

for(j=i-1;j>=1;j--)

printf("%d",j);

printf("\n");

Mrs. Arpitha J C, Assistant professor, Dept of CE, PESITM Shivamogga Page 7


Principles of Programming using C Lab

4: Implement Matrix multiplication and validate the rules of multiplication.

#include<stdio.h>
void main()
{
int a[5][5],b[5][5],c[5][5],m,n,p,q,i,j,k;
printf("enter the size of Matrix A\n");
scanf("%d%d",&m,&n);
printf("enter the size of Matrix B\n");
scanf("%d%d",&p,&q);
if(n == p)
{
printf("Enter the elements of matrix A:\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("Enter the elements of matrix B:\n");
for(i=0;i<p;i++)
{
for(j=0;j<q;j++)
{
scanf("%d",&b[i][j]);
}
}
for(i=0;i<m;i++)

for(j=0;j<q;j++)

Mrs. Arpitha J C, Assistant professor, Dept of CE, PESITM Shivamogga Page 8


Principles of Programming using C Lab

c[i][j]=0; for(k=0;k<n;k++)

c[i][j] = c[i][j] + a[i][k] * b[k][j];

printf("The product of two matrix is:\n"); for(i=0;i<m;i++)

for(j=0;j<q;j++)

printf("%d\t",c[i][j]);

else

printf("\n");

Mrs. Arpitha J C, Assistant professor, Dept of CE, PESITM Shivamogga Page 9


Principles of Programming using C Lab

5: Develop a program to sort the given set of N numbers using Bubble sort.

#include<stdio.h>
void main()
{
int i,j,n,temp,a[6];
printf("\nEnter the size of an array : ");
scanf("%d",&n);
printf("\nEnter the elements of an array : \n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=0;i<n-1;i++)
{
for(j=0;j<n-i-1;j++)
{
if(a[j] > a[j+1])
{
temp = a[j];
a[j] = a[j+1];
a[j+1] =temp;
}
}
}
printf("The elements of the array after sorting : \n");
for(i=0;i<n;i++)
printf("%d\n",a[i]);
}

Mrs. Arpitha J C, Assistant professor, Dept of CE, PESITM Shivamogga Page


10

You might also like