USN :
PROGRAM : 1
Simulation of a Simple Calculator.
#include<stdio.h>
#include<stdlib.h>
void main()
{
int a,b,res;
char op;
printf("Enter the expression(of the format(a op b)):\t ");
scanf("%d %c %d", &a, &op, &b);
switch(op)
{
case '+': res=a+b;
printf("The result is %d %c %d =%d\n", a, op, b, res);
break;
case '-': res=a-b;
printf("The result is %d %c %d =%d\n", a, op, b, res);
break;
case '*': res=a*b;
printf("The result is %d %c %d =%d\n", a, op, b, res);
break;
case '/': if(b==0)
{
printf("\n Division not possible\n");
exit(0);
}
else
{
res=a/b;
printf("The result is %d %c %d =%d\n", a, op, b, res);
}
break;
case '%': res=a%b;
printf("The result is %d %c %d =%d\n", a, op, b, res);
break;
default: printf("Invalid operation.\n");
}
}
CSE DEPT. MMEC, BELAGAVI
USN :
OUTPUT:
ccp01@ubuntu1204-1:~/Documents/POP$ gcc tw1.c
ccp01@ubuntu1204-1:~/Documents/POP$ ./[Link]
Enter the expression(of the format(a op b)): 1 + 2
The result is 1 + 2 =3
ccp01@ubuntu1204-1:~/Documents/POP$ ./[Link]
Enter the expression(of the format(a op b)): 2 - 1
The result is 2 - 1 =1
ccp01@ubuntu1204-1:~/Documents/POP$ ./[Link]
Enter the expression(of the format(a op b)): 2 * 4
The result is 2 * 4 =8
ccp01@ubuntu1204-1:~/Documents/POP$ ./[Link]
Enter the expression(of the format(a op b)): 4 / 2
The result is 4 / 2 =2
ccp01@ubuntu1204-1:~/Documents/POP$ ./[Link]
Enter the expression(of the format(a op b)): 4 / 0
Division not possible
ccp01@ubuntu1204-1:~/Documents/POP$ ./[Link]
Enter the expression(of the format(a op b)): 4 % 2
The result is 4 % 2 =0
ccp01@ubuntu1204-1:~/Documents/POP$ ./[Link]
Enter the expression(of the format(a op b)): 4 & 2
Invalid operation.
CSE DEPT. MMEC, BELAGAVI
USN :
PROGRAM : 2
Compute the roots of a quadratic equation by accepting the coefficients. Print appropriate messages.
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
void main ( )
{
int a,b,c,disc;
float root1,root2,rpt,ipt;
printf("\n Enter the coefficient of quadratic equation\n");
scanf("%d%d%d",&a,&b,&c);
if(a==0||b==0||c==0)
{
printf("\n INVALID COEFFICIENT\n");
exit(0);
}
disc=b*b-4*a*c;
if(disc==0)
{
printf("\n Roots are real and equal:\n");
root1=-b/(2.0*a);
root2=root1;
printf("\n root1=%.3f",root1);
printf("\n root2=%.3f",root2);
}
else if (disc>0)
{
printf("\n Roots are real and distinct \n");
root1=(-b+sqrt(disc))/(2.0*a);
root2=(-b-sqrt(disc))/(2.0*a);
printf("\n root1=%.3f",root1);
printf("\n root2=%.3f",root2);
}
else
{
printf("\n Roots are complex\n");
rpt=-b/(2.0*a);
ipt=sqrt(abs(disc))/(2.0*a);
printf("\n Root1=%.3f+i%.3f",rpt,ipt);
printf("\n Root2=%.3f-i%.3f",rpt,ipt);
}
}
CSE DEPT. MMEC, BELAGAVI
USN :
OUTPUT:
ccp01@ubuntu1204-1:~/Documents/POP$ gcc tw2.c -lm
ccp01@ubuntu1204-1:~/Documents/POP$ ./[Link]
Enter the coefficient of quadratic equation
102
INVALID COEFFICIENT
ccp01@ubuntu1204-1:~/Documents/POP$ ./[Link]
Enter the coefficient of quadratic equation
121
Roots are real and equal:
root1=-1.000
root2=-1.000
ccp01@ubuntu1204-1:~/Documents/POP$ ./[Link]
Enter the coefficient of quadratic equation
123
Roots are complex
Root1=-1.000+i1.414
Root2=-1.000-i1.414
ccp01@ubuntu1204-1:~/Documents/POP$ ./[Link]
Enter the coefficient of quadratic equation
592
Roots are real and distinct
root1=-0.260
root2=-1.540
CSE DEPT. MMEC, BELAGAVI
USN :
PROGRAM : 3
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 total amount is charged. Write a program to read the name of the user, number of
units consumed and print out the charges.
#include<stdio.h>
#include<string.h>
void main()
{
int total_units,runits,nunits;
float c1, c2, c3, tc, additional_charge = 0.0;
char name[15];
printf("Enter the user name and total units:");
scanf("%s %d", name, &total_units);
if(total_units <= 200)
{
c1= total_units * 0.80;
tc= c1 + 100;
}
else if(total_units <= 300)
{
nunits= total_units - 200;
c1= nunits * 0.90;
c2= 200 * 0.80;
tc= c1 + c2 + 100;
}
else if(total_units > 300)
{
runits= total_units - 300;
c1= runits * 1.00;
c2= 100 * 0.90;
c3= 200 * 0.80;
tc= c1 + c2 + c3 + 100;
}
if(tc > 400)
additional_charge = additional_charge + (tc*15)/100;
tc = tc + additional_charge;
printf("The %s user and total_charges =%.2f\n", name,tc);
}
CSE DEPT. MMEC, BELAGAVI
USN :
OUTPUT:
ccp01@ubuntu1204-1:~/Documents/POP$ gcc tw3.c
ccp01@ubuntu1204-1:~/Documents/POP$ ./[Link]
Enter the user name and total units:mmec 200
The mmec user and total_charges =260.00
ccp01@ubuntu1204-1:~/Documents/POP$ ./[Link]
Enter the user name and total units:mmec 250
The mmec user and total_charges =305.00
ccp01@ubuntu1204-1:~/Documents/POP$ ./[Link]
Enter the user name and total units:mmec 350
The mmec user and total_charges =400.00
ccp01@ubuntu1204-1:~/Documents/POP$ ./[Link]
Enter the user name and total units:mmec 450
The mmec user and total_charges =575.00
ccp01@ubuntu1204-1:~/Documents/POP$ ./[Link]
Enter the user name and total units:mmec 80
The mmec user and total_charges =164.00
CSE DEPT. MMEC, BELAGAVI
USN :
PROGRAM : 4
Write a C Program to display the following by reading the number of rows as input,
1
121
12321
1234321
---------------------------
nth row
#include<stdio.h>
int main()
{
int n,i,j,k,l;
printf("\n Enter the number of rows:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
for(k=0;k<n-i;k++)
printf(" ");
for(j=1;j<=i;j++)
printf("%d",j);
for(l=j-2;l>0;l--)
printf("%d",l);
printf("\n");
}
return;
}
OUTPUT:
ccp01@ubuntu1204-1:~/Documents/POP$ gcc tw4.c
ccp01@ubuntu1204-1:~/Documents/POP$ ./[Link]
Enter the number of rows:5
1
121
12321
1234321
123454321
CSE DEPT. MMEC, BELAGAVI
USN :
PROGRAM : 5
Implement Binary Search on Integers.
#include<stdio.h>
#include<stdlib.h>
int main()
{
int n,i,a[20],item,low,high,mid;
printf("\nEnter the number of elements: ");
scanf("%d",&n);
printf("\nEnter the n values in ascending order\n");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("\nEnter the item to search: ");
scanf("%d",&item);
low=0;
high=n-1;
while(low <= high)
{
mid=(low+high)/2;
if(item==a[mid])
{
printf("Search is successful\n");
exit(0);
}
if(item<a[mid])
high=mid-1;
else
low=mid+1;
}
printf("Search is unsuccessful\n");
return 0;
}
CSE DEPT. MMEC, BELAGAVI
USN :
OUTPUT:
ccp01@ubuntu1204-1:~/Documents/POP$ gcc tw5.c
ccp01@ubuntu1204-1:~/Documents/POP$ ./[Link]
Enter the number of elements: 5
Enter the n values in ascending order
12345
Enter the item to search: 4
Search is successful
ccp01@ubuntu1204-1:~/Documents/POP$ ./[Link]
Enter the number of elements: 5
Enter the n values in ascending order
10 20 30 50 60
Enter the item to search: 80
Search is unsuccessful
CSE DEPT. MMEC, BELAGAVI
USN :
PROGRAM : 6
Implement Matrix multiplication and validate the rules of multiplication.
#include<stdio.h>
void main( )
{
int i,m,n,k,p,j,q,sum;
int a[10][10],b[10][10],c[10][10];
printf("\n Enter the order of the first matrix A:");
scanf("%d%d",&m,&n);
printf("\n Enter the order of the second matrix B:");
scanf("%d%d",&p,&q);
printf("\n");
printf("\nOrder of MATRIX-A=%dX%d\t Order of MATRIX-B=%dx%d\n",m,n,p,q);
if(n!=p)
{
printf("\nMultiplication is not possible\n");
exit(0);
}
printf("\n Enter elements of Matrix-A\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
}
printf("\n Enter elements of Matrix-B\n");
for(i=0;i<p;i++)
{
for(j=0;j<q;j++)
scanf("%d",&b[i][j]);
}
printf("\n Matrix-A\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
printf("%d\t",a[i][j]);
printf("\n");
}
printf("\n Matrix-B\n");
for(i=0;i<p;i++)
{
for(j=0;j<q;j++)
printf("%d\t",b[i][j]);
printf("\n");
}
CSE DEPT. MMEC, BELAGAVI
USN :
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("\n RESULTANT MATRIX\n");
for(i=0;i<m;i++)
{
for(j=0;j<q;j++)
printf("%d\t",c[i][j]);
printf("\n");
}
}
OUTPUT:
ccp01@ubuntu1204-1:~/Documents/POP$ gcc tw6.c
ccp01@ubuntu1204-1:~/Documents/POP$ ./[Link]
Enter the order of the first matrix A:2 2
Enter the order of the second matrix B:2 2
Order of MATRIX-A=2X2 Order of MATRIX-B=2x2
Enter elements of Matrix-A
1234
Enter elements of Matrix-B
1234
Matrix-A
12
34
Matrix-B
12
34
CSE DEPT. MMEC, BELAGAVI
USN :
RESULTANT MATRIX
7 10
15 22
ccp01@ubuntu1204-1:~/Documents/POP$ ./[Link]
Enter the order of the first matrix A:2 1
Enter the order of the second matrix B:2 1
Order of MATRIX-A=2X1 Order of MATRIX-B=2x1
Multiplication is not possible
CSE DEPT. MMEC, BELAGAVI
USN :
PROGRAM : 7
Compute sin(x) or 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>
void main( )
{
int n,i;
float x,degree,sum,term;
printf("enter the degree and number of terms\n");
scanf("%f %d",°ree,&n);
x=degree*3.1416/180;
term=x;
sum=term;
for(i=3;i<n;i=i+2)
{
term=-term*x*x/((i-1)*i);
sum=sum+term;
}
printf("mysine(%.2f)=%.2f\n",degree,sum);
printf("sin(%.2f)=%.2f\n",degree,sin(x));
printf("\n The above results shows,\n The results of Taylor series Approximation is almost
equal to the results of Built- in Library Functions");
}
OUTPUT:
ccp01@ubuntu1204-1:~/Documents/POP$ gcc tw7.c
ccp01@ubuntu1204-1:~/Documents/POP$ ./[Link]
enter the degree and number of terms
60 4
mysine(60.00)=0.86
sin(60.00)=0.87
The above results shows,
The results of Taylor series Approximation is almost equal to the results of Built- in Library Functions
ccp01@ubuntu1204-1:~/Documents/POP$ ./[Link]
enter the degree and number of terms
45 4
mysine(45.00)=0.70
sin(45.00)=0.71
The above results shows,
The results of Taylor series Approximation is almost equal to the results of Built- in Library Functions
CSE DEPT. MMEC, BELAGAVI
USN :
PROGRAM : 8
Sort the given set of N numbers using Bubble sort.
#include<stdio.h>
int main()
{
int a[10],i,j,temp,n;
printf("\n Enter the max [Link] Elements to Sort: \n");
scanf("%d",&n);
printf("\n Enter the Elements : \n");
for(i=0; i<n; i++)
{
scanf("%d",&a[i]);
}
for(i=0; i<n; i++)
{
for(j=i+1; j<n; j++)
{
if(a[i] > a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
printf("\n The sorted list is:");
for(i=0; i<n; i++)
{
printf("%d \t",a[i]);
}
return;
}
OUTPUT:
ccp01@ubuntu1204-1:~/Documents/POP$ gcc tw8.c
ccp01@ubuntu1204-1:~/Documents/POP$ ./[Link]
Enter the max [Link] Elements to Sort:
5
Enter the Elements :
13240
The sorted list is:0 1 2 3 4
CSE DEPT. MMEC, BELAGAVI
USN :
PROGRAM : 9
Write functions to implement string operations such as compare, concatenate, and find string
length. Use the parameter passing techniques.
#include<stdio.h>
#include<stdlib.h>
int find_length(char string[])
{
int len = 0, i;
for (i = 0; string[i] != '\0'; i++)
{
len++;
}
return len;
}
void join_strings(char string1[], char string2[])
{
int i, len1, len2;
len1 = find_length(string1);
len2 = find_length(string2);
for (i = len1; i < len1 + len2; i++)
{
string1[i] = string2[i - len1];
}
string1[i] = '\0';
}
int compare_strings(char string1[], char string2[])
{
int len1, len2, i, count = 0;
len1 = find_length(string1);
len2 = find_length(string2);
if (len1 != len2)
return 1;
for (i = 0; i < len1; i++)
{
if (string1[i] == string2[i])
count++;
}
if (count == len1)
return 0;
return 1;
}
CSE DEPT. MMEC, BELAGAVI
USN :
int main()
{
char string1[20], string2[20]; //string variables declaration with size 20
int choice;
while (1)
{
printf("\n1. Find Length \n2. Concatenate \n3. Compare\n4. Exit\n");
printf("Enter your choice: ");
scanf("%d", & choice);
switch (choice)
{
case 1:
printf("Enter the string: ");
scanf("%s", string1);
printf("The length of string is %d", find_length(string1));
break;
case 2:
printf("Enter two strings: ");
scanf("%s%s", string1, string2);
join_strings(string1, string2);
printf("The concatenated string is %s", string1);
break;
case 3:
printf("Enter two strings: ");
scanf("%s%s", string1, string2);
if (compare_strings(string1, string2) == 0)
{
printf("They are equal");
}
else
{
printf("They are not equal");
}
break;
case 4:
exit(0);
}
}
return 0;
}
CSE DEPT. MMEC, BELAGAVI
USN :
OUTPUT:
ccp01@ubuntu1204-1:~/Documents/POP$ gcc tw9.c
ccp01@ubuntu1204-1:~/Documents/POP$ ./[Link]
1. Find Length
2. Concatenate
3. Compare
4. Exit
Enter your choice: 1
Enter the string: mmec
The length of string is 4
1. Find Length
2. Concatenate
3. Compare
4. Exit
Enter your choice: 2
Enter two strings: mmec engg
The concatenated string is mmecengg
1. Find Length
2. Concatenate
3. Compare
4. Exit
Enter your choice: 3
Enter two strings: mmec mmec
They are equal
1. Find Length
2. Concatenate
3. Compare
4. Exit
Enter your choice: 3
Enter two strings: mmec engg
They are not equal
1. Find Length
2. Concatenate
3. Compare
4. Exit
Enter your choice: 4
ccp01@ubuntu1204-1:~/Documents/POP$
CSE DEPT. MMEC, BELAGAVI
USN :
PROGRAM : 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>
struct student
{
char usn[10];
char name[10];
float m1,m2,m3;
float avg,total;
};
void main()
{
struct student s[20];
int n,i;
printf("Enter the number of student=");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter the detail of %d students\n",i+1);
printf("\n Enter USN=");
scanf("%s",s[i].usn);
printf("\n Enter Name=");
scanf("%s",s[i].name);
printf("Enter the three subject score\n");
scanf("%f%f%f",&s[i].m1,&s[i].m2,&s[i].m3);
s[i].total=s[i].m1+s[i].m2+s[i].m3;
s[i].avg=s[i].total/3;
}
for(i=0;i<n;i++)
{
if(s[i].avg>=35)
printf("\n %s has scored above the average marks\n",s[i].name);
else
printf("\n %s has scored below the average marks\n",s[i].name);
}
}
CSE DEPT. MMEC, BELAGAVI
USN :
OUTPUT:
ccp01@ubuntu1204-1:~/Documents/POP$ gcc tw10.c
ccp01@ubuntu1204-1:~/Documents/POP$ ./[Link]
Enter the number of student=3
Enter the detail of 1 students
Enter USN=1
Enter Name=abc
Enter the three subject score
35 35 30
Enter the detail of 2 students
Enter USN=2
Enter Name=xyz
Enter the three subject score
35 45 50
Enter the detail of 3 students
Enter USN=3
Enter Name=tuv
Enter the three subject score
80 50 85
abc has scored below the average marks
xyz has scored above the average marks
tuv has scored above the average marks
CSE DEPT. MMEC, BELAGAVI
USN :
PROGRAM : 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()
{
int n , i;
float x[20],sum,mean;
float variance , deviation;
printf("Enter the value of n \n");
scanf("%d",&n);
printf("enter %d real values \n",n);
for (i=0;i<n;i++)
scanf("%f",(x+i));
sum=0;
for(i=0;i<n;i++)
sum= sum+*(x+i);
printf("sum=%f\n",sum);
mean=sum/n;
sum=0;
for(i=0;i<n;i++)
sum=sum+pow((*(x+i)- mean),2);
variance=sum/n;
deviation=sqrt(variance);
printf("mean(Average)=%f\n",mean);
printf("variance=%f\n",variance);
printf("standard deviation=%f\n",deviation);
}
OUTPUT:
ccp01@ubuntu1204-1:~/Documents/POP$ gcc tw11.c -lm
ccp01@ubuntu1204-1:~/Documents/POP$ ./[Link]
Enter the value of n
5
enter 5 real values
12345
sum=15.000000
mean(Average)=3.000000
variance=2.000000
standard deviation=1.414214
CSE DEPT. MMEC, BELAGAVI
USN :
PROGRAM : 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>
#include <stdlib.h>
int main()
{
char source_file[100], target_file[100], ch;
printf("Enter name of source file: ");
scanf("%s", source_file);
printf("Enter name of target file: ");
scanf("%s", target_file);
FILE *source_fp = fopen(source_file, "r");
FILE *target_fp = fopen(target_file, "w");
if (source_fp == NULL || target_fp == NULL)
{
printf("Error: File could not be opened\n");
exit(EXIT_FAILURE);
}
while ((ch = fgetc(source_fp)) != EOF)
{
fputc(ch, target_fp);
}
printf("File copied successfully.\n");
fclose(source_fp);
fclose(target_fp);
return 0;
}
OUTPUT:
ccp01@ubuntu1204-1:~/Documents/POP$ gcc tw12.c
ccp01@ubuntu1204-1:~/Documents/POP$ ./[Link]
Enter name of source file: tw1.c
Enter name of target file: simplecal.c
File copied successfully.
CSE DEPT. MMEC, BELAGAVI