The National Institute of Engineering, Mysuru 2024-25
Code: BPOPS103/203 Course: Principles of Programming Using C
List of Experiments with Solution
1. Simulation of a Simple Calculator.
#include<stdio.h>
int main()
{
int a,b,ans;
char o;
scanf("%d\n%c\n%d",&a,&o,&b);
switch(o){
case '+':
ans=a+b;
printf("The sum is %d",ans);
break;
case'-':
ans=a-b;
printf("The difference is %d",ans);
break;
case'*':
ans=a*b;
printf("The product is %d",ans);
break;
case'/':
if(b==0){
printf("Divide by Zero error");
}
else{
ans=a/b;
printf("The quotient is %d",ans);
}
break;
case'%':
ans=a%b;
printf("The remainder is %d",ans);
break;
default:
printf("Invalid Input");
break;
}
return 0;
}
The National Institute of Engineering, Mysuru 2024-25
Output :
Sample Input 1:
10
+
5
Sample Output 1:
The sum is 15
Sample Input 2:
10
-
5
Sample Output 2:
The difference is 5
Sample Input 3:
10
*
5
Sample Output 3:
The product is 50
Sample Input 4:
10
/
5
Sample Output 4:
The quotient is 2
Sample Input 5:
10
/
0
Sample Output 5:
Divide by Zero error
Sample Input 6:
10
%
5
Sample Output 6:
The remainder is 0
Sample Input 7:
10
=
5
Sample Output 7:
Invalid Input
The National Institute of Engineering, Mysuru 2024-25
2. Compute the roots of a quadratic equation by accepting the coefficients.
Printappropriate messages.
#include<stdio.h>
#include<math.h>
int main()
{
int a,b,c;
float d,r1,r2,r,i;
printf("Enter the coefficients of a, b and c\n");
scanf("%d%d%d",&a,&b,&c);
d=(b*b)-(4*a*c);
if(d==0)
{
r=(-b)/(2*a);
printf("%.2f",r);
}
else if(d>0)
{
r1=(-b+sqrt(d))/(2*a);
r2=(-b-sqrt(d))/(2*a);
printf("%.2f and %.2f",r1,r2);
}
else
{
r=-b/(2.0*a);
i=sqrt(-d)/(2*a);
printf("%.2f+%.2fi and %.2f-%.2fi",r,i,r,i);
}
return 0;
}
Output :
SAMPLE INPUT OUTPUT 1 :
Enter the coefficients of a, b and c
111
-0.50+0.87i and -0.50-0.87i
SAMPLE INPUT OUTPUT 2 :
Enter the coefficients of a, b and c
372
-0.33 and -2.00
The National Institute of Engineering, Mysuru 2024-25
3. An electricity board charges the following rates for the use of electricity: forthe 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
additionalsurcharge of 15% of totalamountis charged. Write a program to read the name of
the usr, number of units consumed and print out the charges.
#include<stdio.h>
#include<stdlib.h>
int main()
{
float y;
char ab[20];
int x;
printf("Enter name\n");
scanf("%s",ab);
printf("Enter units\n");
scanf("%d",&x);
if(x>=0 && x<=200)
{
y=(x*0.8)+100;
}
else if(x>200 && x<=300)
{
x=x-200;
y=160+(x*0.9)+100;
}
else if(x>300)
{
x=x-300;
y=160+90+(x*1)+100;
}
else
{
printf("Invalid Input\n");
exit(0);
}
if(y>=400)
{
y=y+((y*15)/100);
printf("Total amount charged to %s is %.2f",ab,y);
exit(0);
}
printf("Total amount charged to %s is %.2f",ab,y);
}
The National Institute of Engineering, Mysuru 2024-25
Output :
Sample Input:
Enter name
Param
Enter units
300
Sample Output:
Total amount charged to Param is 350.00
Sample Input:
Enter name
Rashmi
Enter units
200
Sample Output:
Total amount charged to Rashmi is 260.00
The National Institute of Engineering, Mysuru 2024-25
4. Write a C Program to display the following by reading the number of rows as input,
1
121
12321
1234321
---------------------------
n th row
#include<stdio.h>
int main( )
{
int i,j,n,k,m;
printf("Input number of rows : ");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
/* print blank spaces */
for(j=1;j<=n-i;j++)
{
printf(" ");
}
/* Display number in ascending order upto middle*/
for(k=1;k<=i;k++)
{
printf("%d",k);
}
/* Display number in reverse order after middle */
for(m=i-1;m>=1;m--)
{
printf("%d",m);
}
printf("\n");
}
return 0;
}
The National Institute of Engineering, Mysuru 2024-25
Output :
Sample Input:
Input number of rows : 5
Sample Output:
1
121
12321
1234321
123454321
Sample Input:
Input number of rows : 4
Sample Output:
1
121
12321
1234321
The National Institute of Engineering, Mysuru 2024-25
5. Implement Binary Search on Integers.
#include<stdio.h>
#include<stdlib.h>
int main()
{
int a[20],n,i,low,high, mid, key;
printf("Enter the number of elements\n");
scanf("%d",&n);
printf("Enter the elements\n");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("Enter the key to be searched\n");
scanf("%d",&key);
low=0;
high=n-1;
while(low<=high)
{
mid=(low+high)/2;
if(a[mid]==key)
{
printf("The key is present in the array \n");
printf("\nat index %d ",mid);
exit(0);
}
else if(a[mid]>key)
{
high=mid-1;
}
else if(a[mid]<key)
{
low=mid+1;
}
printf("The key is not present in the array\n");
return 0;
}
}
The National Institute of Engineering, Mysuru 2024-25
Output:
Sample Input and Output 1:
Enter the number of elements
5
Enter the elements
1
1
3
6
7
Enter the key to be searched
1
The key is present in the array at index 0
Sample Input and Output 2:
Enter the number of elements
5
Enter the elements
1
2
3
6
7
Enter the key to be searched
5
The key is not present in the array
The National Institute of Engineering, Mysuru 2024-25
6. Implement Matrix multiplication and validate the rules of multiplication.
#include<stdio.h>
#include<stdlib.h>
int main()
{
int i,j,k,m,n,p,q;
int a[10][10], b[10][10], c[10][10];
printf("Enter the size of the First matrix\n");
scanf("%d%d", &m, &n);
printf("Enter the size of the Second matrix\n");
scanf("%d%d", &p, &q);
if (n != p)
{
printf("Incompatible Array size\n ");
exit(0);
printf("Enter the elements of First matrix\n");
for (i = 0; i < m; i++)
{
for (j= 0; j< n; j++)
{
scanf("%d", &a[i][j]);
}
}
printf("Enter the elements of Second matrix\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++)
{
c[i][j]=0;
for (k = 0; k < n; k++)
{
c[i][j]= c[i][j] + a[i][k] * b[k][j];
}
}
}
printf("The Matrix after Multiplication is\n");
for (i = 0 ; i < m ; i++)
{
for (j = 0 ; j < q ; j++)
{
printf("%d ", c[i][j] );
}
The National Institute of Engineering, Mysuru 2024-25
printf("\n");
}
return 0;
}
}
Output:
Sample Input and Output 1 :
[ All text of bold corresponds to Input and the rest output]
Enter the size of the First matrix
2
2
Enter the size of the Second matrix
2
2
Enter the elements of First matrix
1
2
3
4
Enter the elements of Second matrix
5
6
7
8
The Matrix after Multiplication is
19 22
43 50
Sample input-output:
Enter the size of the First matrix
3
2
Enter the size of the Second matrix
3
2
Incompatible Array size
The National Institute of Engineering, Mysuru 2024-25
7. Compute sin(x)/cos(x) using Taylor series approximation. Compare your result with the
built-in library function. Print both the results with appropriate inferences.
#include<stdio.h>
#include<math.h>
int fact(int m);
int fact(int m)
{
int i,f=1;
for(i=1;i<=m;i++)
{
f=f*i;
}
return f;
}
int main()
{
int x,n,i;
float rad, sum=0;
printf("Enter degree\n");
scanf("%d",&x);
printf("Enter number of terms\n");
scanf("%d",&n);
rad=x*3.14/180;
for(i=1;i<=n;i+=2)
{
if ((i-1)%4==0)
{
sum=sum+pow(rad,i)/fact(i);
}
else
{
sum=sum-pow(rad,i)/fact(i);
}
}
printf("Calculated sin(%d) = %f", x,sum);
printf("\nLibrary sin(%d) = %f", x,sin(rad));
return 0;
}
The National Institute of Engineering, Mysuru 2024-25
Output :
Sample Input and Output 1:
Enter the value of degree
30
sin(30.000000)=0.500 without using built in function
sin(30.000000)=0.500 using built in function
Sample Input and Output 2:
Enter the value of degree
60
sin(60.000000)=0.866 without using built in function
sin(60.000000)=0.866 using built in function
The National Institute of Engineering, Mysuru 2024-25
8. Sort the given set of N numbers using Bubble sort.
#include<stdio.h>
int main()
{
int n,arr[100],i,j,temp;
printf("Enter the number elements in the array\n");
scanf("%d",&n);
printf("Enter the elements of the array\n");
for(i=0;i<n;i++)
scanf("%d ",&arr[i]);
printf("Before sorting the array\n");
for(i=0;i<n;i++)
printf("%d ",arr[i]);
for(i=0;i<n-1;i++)
{
for(j=0;j<n-1;j++)
{
if(arr[j]>arr[j+1])
{
temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}
}
printf("\nAfter sorting the array\n");
for(i=0;i<n;i++)
{
printf("%d ",arr[i]);
}
return 0;
}
The National Institute of Engineering, Mysuru 2024-25
}
Output :
Sample Input and Output:
Enter the number elements in the array
5
Enter the elements of the array
9
6
7
4
5
Before sorting the array
96745
After sorting the array
45679
The National Institute of Engineering, Mysuru 2024-25
9 . Write functions to implement string operations such as compare, concatenate, and
find string length. Use the parameter passing techniques.
#include <stdio.h>
#include<string.h>
int string_len(char s1[100])
{
int length;
// calculate the length of string s1
length = 0;
while(s1[length] != '\0')
{
length++;
}
printf("Length = %d\n", length);
return 0;
}
int string_cat(char s1[100],char s2[100])
{
int i,j;
// calculate the length of string s1
// and store it in i
for(i =0; s1[i]!='\0';i++);
// Now i' has the length of s1(same as index of \0).
// s[i] has '\0' of string s1
// Copy characters from string s2 to s1[i] onwards
for(j =0; s2[j]!='\0';j++,i++)
{
s1[i]= s2[j];
}
s1[i]='\0';
printf("After concatenation: %s\n", s1);
return 0;
}
int string_cmp(char s1[100],char s2[100])
{
int i;
for(i = 0; s1[i]!='\0' && s2[i]!='\0'; i++)
{
if(s1[i] < s2[i])
{
// s1 should come first alphabetically
printf("-1\n") ;
return 0;
}
if(s1[i] > s2[i] )
{
// s2 should come first alphabetically
printf("1\n") ;
return 0;
}
} // end of loop
if(s1[i] == '\0' && s2[i] == '\0') //if both strings are over
The National Institute of Engineering, Mysuru 2024-25
{
//Both strings are equal(same)
printf("0\n") ;
}
else if(s1[i] == '\0') // If Only s1 is over
printf("-1\n") ; // s1 should come first alphabetically
else if(s2[i] == '\0')// If only s2 is over
printf("1\n") ; // s2 should come first alphabetically
return 0;
}
int main()
{
char s1[100], s2[100];
printf("Enter first string: ");
gets(s1);
printf("Enter second string: ");
gets(s2);
string_len(s1);
string_len(s2);
string_cmp(s1,s2);
string_cat(s1,s2);
return 0;
Output :
Sample Input and Output
Enter first string: hello
Enter second string: how are you?
Length = 5
Length = 12
-1
After concatenation: hellohow are you?
The National Institute of Engineering, Mysuru 2024-25
10. Implement structures to read, write and compute average- marks of the students, list
the students scoring above and below the average marks for a class of N students.
#include<stdio.h>
#include<math.h>
#include<string.h>
struct student
{
char name[30];
int rollnumber;
int marks;
};
int main()
{
struct student s[10];
int i=1,n,sum=0;
float average=0.0;
printf("Enter the number of students\n");
scanf("%d",&n);
//printf("Enter the details of student \n");
for(i=0;i<n;i++)
{
printf("Enter the details of student %d\n",i+1);
printf("Enter the name\n");
scanf("%s",s[i].name);
//printf("%s\n",s[i].name);
fflush(stdin);
printf("Enter the rollnumber\n");
scanf("%d",&s[i].rollnumber);
fflush(stdin);
//printf("%d\n",s[i].rollnumber);
printf("Enter the marks\n");
scanf("%d",&s[i].marks);
fflush(stdin);
}
for(i=0;i<n;i++)
sum=sum+s[i].marks;
average=(float)sum/n;
int ABaverage=0, BLaverage=0;
for(i=0;i<n;i++){
if(s[i].marks>=average)
ABaverage++;
else
BLaverage++;
}
printf("Number of students who have above average is %d\n", ABaverage);
printf("Number of students who have below average is %d\n", BLaverage);
return 0;
}
The National Institute of Engineering, Mysuru 2024-25
Output :
Sample Input Output Format 1:
Enter the number of students
2
Enter the details of student 1
Enter the name
reshma
Enter the rollnumber
09
Enter the marks
77
Enter the details of student 2
Enter the name
nishu
Enter the rollnumber
11
Enter the marks
90
Number of students who have above average is 1
Number of students who have below average is 1
Sample Input Output Format 2:
Enter the number of students
2
Enter the details of student 1
Enter the name
sanvi
Enter the rollnumber
115
Enter the marks
70
Enter the details of student 2
Enter the name
samrudhi
Enter the rollnumber
12
Enter the marks
70
The National Institute of Engineering, Mysuru 2024-25
Number of sttudents who have above average is 2
Number of students who have below average is 0
The National Institute of Engineering, Mysuru 2024-25
11. Develop a program using pointers to compute the sum, mean and standard deviation
of all elements stored in an array of N real numbers.
#include<stdio.h>
#include<math.h>
int main()
{
float a[10];
int n,i;
float mean,dev,sum=0,sum2=0,var;
float *ptr=a;
printf("Enter the number of elements in the array\n");
scanf("%d",&n);
printf("Enter the elements in the array\n");
for(i=0;i<n;i++)
{
scanf("%f",&a[i]);
}
for(i=0;i<n;i++)
{
sum+=*ptr;
ptr++;
}
mean = sum/n;
for(i=0;i<n;i++)
{
sum2 = sum2 + pow((*(a+i) - mean), 2);
}
var = sum2/n;
dev = sqrt(var);
printf("Sum is %.2f\nMean is %.2f\nStandard deviation is %.2f",sum,mean,dev);
return 0;
Sample Input and Output:
Enter the number of elements in the array
5
Enter the elements in the array
1
2
3
4
5
Sum is 15.00
Mean is 3.00
Standard deviation is 1.41
The National Institute of Engineering, Mysuru 2024-25
12. Write a C program to copy a text file to another, read both the input file name and
target file name.
#include<stdio.h>
int main()
{
FILE *fp1,*fp2;
char c;
fp1=fopen("input.txt","r");
if(fp1==NULL)
{
printf("File cannot be opened\n");
return 0;
}
fp2=fopen("output.txt","w");
c=fgetc(fp1);
while(c!=EOF)
{
fputc(c,fp2);
c=fgetc(fp1);
}
fclose(fp1);
fclose(fp2);
return 0;
Output :
Rules:
The input file should be named as "input.txt".
The output file should be named as "output.txt".
Sample Input file (input.txt):
Hello World!
Welcome to C
Sample Output file (output.txt)
Hello World!
Welcome to
The National Institute of Engineering, Mysuru 2024-25