Lab Programs
Lab Programs
#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 :
}
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
#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;
}
}
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
#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);
return 0;
}
}
Output:
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 :
#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 :
#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).
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 :
#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;
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".