1 Simulation of a SimpleCalculator.
2 Compute the roots of a quadratic equation by accepting the coefficients. Print
appropriate messages.
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.
4. Write a C Program to display the following by reading the number of rows as
input,
121
12321
1234321
---------------------------
nth row
5 Implement Binary Search on Integers.
6 Implement Matrix multiplication and validate the rules of multiplication.
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.
8 Sort the given set of N numbers using Bubble sort.
9 Write functions to implement string operations such as compare, concatenate,
and find string length. Use the
parameter passing techniques.
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.
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.
12. Write a C program to copy a text file to another, read both the input file name
and target file name.
1.simulation of a Simple calculator
Algorithm
Step 1: Declare variables a,b
Step 2: Enter any operator at runtime
Step 3: Enter any two integer values at runtime
Step 4: Apply switch case to select the operator:
// case '+': c=a+b;
break;
case '-': : c=a-b;
break;
case '*': : c=a*b;
break;
case '/': a/b;
break;
default: printf("
Invalid Operator ");
Step 5: Print the result
program
#include <stdio.h>
#include <stdlib.h>
int main()
char ch;
double a, b;
while (1) {
printf("Enter an operator (+, -, *, /), "
"if want to exit press x: ");
scanf(" %c", &ch);
// to exit
if (ch == 'x')
exit(0);
printf("Enter two first and second operand: ");
scanf("%lf %lf", &a, &b);
// Using switch case we will differentiate
// operations based on different operator
switch (ch) {
// For Addition
case '+':
printf("%.1lf + %.1lf = %.1lf\n", a, b, a + b);
break;
// For Subtraction
case '-':
printf("%.1lf - %.1lf = %.1lf\n", a, b, a - b);
break;
// For Multiplication
case '*':
printf("%.1lf * %.1lf = %.1lf\n", a, b, a * b);
break;
// For Division
case '/':
printf("%.1lf / %.1lf = %.1lf\n", a, b, a / b);
break;
// If operator doesn't match any case constant
default:
printf(
"Error! please write a valid operator\n");
printf("\n");
}}
2.Compute the root of a quadratic equation by accepting the coefficients.print
appropriate messages.
Algorithm
Start
Read a, b, c values
Compute d = b2 4ac
if d > 0 then
o r1 = b+ sqrt (d)/(2*a)
o r2 = b sqrt(d)/(2*a)
Otherwise if d = 0 then
o compute r1 = -b/2a, r2=-b/2a
o print r1,r2 values
Otherwise if d < 0 then print roots are imaginary
Stop
program
#include <stdio.h> // Include the standard input/output header file.
#include <math.h> // Include the math library for mathematical functions.
void main()
int a, b, c, d; // Declare variables to store coefficients and discriminant.
float x1, x2; // Declare variables to store roots.
printf("Input the value of a, b & c : "); // Prompt user for input.
scanf("%d%d%d", &a, &b, &c); // Read and store coefficients in 'a', 'b', and 'c'.
d = b*b - 4*a*c; // Calculate the discriminant.
if(d == 0) // Check if discriminant is zero.
printf("Both roots are equal.\n");
x1 = -b / (2.0 * a); // Calculate the single root.
x2 = x1;
printf("First Root Root1 = %f\n", x1); // Print the roots.
printf("Second Root Root2 = %f\n", x2);
else if(d > 0) // Check if discriminant is positive.
printf("Both roots are real and different.\n");
x1 = (-b + sqrt(d)) / (2 * a); // Calculate the first root.
x2 = (-b - sqrt(d)) / (2 * a); // Calculate the second root.
printf("First Root Root1 = %f\n", x1); // Print the roots.
printf("Second Root Root2 = %f\n", x2);
else // If discriminant is negative.
printf("Roots are imaginary;\nNo Solution. \n"); // Print no solution message.
}
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 rupees 1 per unit. All users are charged a minimum of
rupees 100 as a 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>
#include <string.h>
int main() {
char name[100];
int units;
float totalCharge, surcharge;
// Input: User's name and units consumed
printf("Enter your name: ");
scanf("%s", name);
printf("Enter the number of units consumed: ");
scanf("%d", &units);
// Calculate the electricity charges
float meterCharge = 100;
float charge = 0;
if (units <= 200) {
charge = units * 0.80;
} else if (units <= 300) {
charge = 200 * 0.80 + (units - 200) * 0.90;
} else {
charge = 200 * 0.80 + 100 * 0.90 + (units - 300) * 1.0;
}
totalCharge = charge + meterCharge;
// Check if the total amount is more than Rs. 400 and apply surcharge if necessary
if (totalCharge > 400) {
surcharge = 0.15 * totalCharge;
totalCharge += surcharge;
}
// Output: Display the charges
printf("\nName: %s\n", name);
printf("Units Consumed: %d\n", units);
printf("Electricity Charges: Rs. %.2f\n", charge);
printf("Meter Charge: Rs. %.2f\n", meterCharge);
printf("Total Charges: Rs. %.2f\n", totalCharge);
if (totalCharge > 400) {
printf("Surcharge (15%%): Rs. %.2f\n", surcharge);
}
return 0;
}
4. Write a C Program to display the following by reading the number of rows
as input,
121
12321
1234321
---------------------------
nth row
Algorithm
Step 1: Read the number of rows n from user.
Step 2: set for loop to i<=n.
Step 3: set for loop to print space.
Step 4: set for loop to print first number.
Step 5: set for loop to print next number.
Step 6: repeat the steps until i <=n.
Step 7:stop.
Program
#include<stdio.h>
#include<conio.h>
void main()
clrscr();
int n,i,j,k,l;
printf(“Enter the number of rows to be printed:”);
scanf(“%d”,&n);
for(i=0;i<=n; i++)
for(j=1;j<=n-i; j++)
{
printf(“ “);
for(k=1;k<=i; k++)
printf(“%d“,k);
for(l=i-1 ;l>0; l--)
printf(“%d”,l);
printf(“\n”);
getch();
}
5. Implement Binary Search on Integers
Algorithm
1. Read the search element from the user.
2. Find the middle element in the sorted array.
3. Compare the search element with the middle element in the sorted array.
4. If both are matched, then display "Given element is found!!!" and terminate the
function.
5. If both are not matched, then check whether the search element is smaller or larger
than the middle element.
6. If the search element is smaller than the middle element, repeat steps 2, 3, 4 and 5
for the left subarray of the middle element.
7. If the search element is larger than the middle element, repeat steps 2, 3, 4 and 5
for the right subarray of the middle element.
8. Repeat the same process until we find the search element in the array or until the
subarray contains only one element.
9. If that element also doesn’t match with the search element, then display "Element
is not found in the array!!!" and terminate the function.
#include<stdio.h>
#include<conio.h>
int main()
{
int i, arr[10], search, first, last, middle;
printf("Enter 10 elements (in ascending order): ");
for(i=0; i<10; i++)
scanf("%d", &arr[i]);
printf("\nEnter element to be search: ");
scanf("%d", &search);
first = 0;
last = 9;
middle = (first+last)/2;
while(first <= last)
{
if(arr[middle]<search)
first = middle+1;
else if(arr[middle]==search)
{
printf("\nThe number, %d found at Position %d", search, middle+1);
break;
}
else
last = middle-1;
middle = (first+last)/2;
}
if(first>last)
printf("\nThe number, %d is not found in given Array", search);
getch();
return 0;
}
Algorithm
Step-1: Start
Step-2: [Read the order of both matrices M, N, P, Q]
Step-3: Check for i = 0 to M and j = 0 to N for the matrix A, read A[i][j]
Step-4: Check for i = 0 to P and j = 0 to Q for the matrix B, read B[i][j]
Step-5: If in case (N=P) then only multiplication is possible then goto step 6
otherwise goto step 7
Step-6: Initially set matrix C[i][j] as 0
o For k=0 to n
o C[i][j] = C[i][j] + A[i][k]*B[i][k] than goto step 8
Step-7: Multiplication is not possible
Step-8: Prints the multiplication of the two matrix
Step-9: Stop
To implement matrix multiplication and validate the rules of matrix
#include<stdio.h>
#include<conio.h>
main()
{
int a[20][20],b[20][20],c[20][20];
int m,n,p,q,i,j,k;
clrscr();
printf("enter rows and columns of matrix a\n");
scanf("%d%d",&m,&n);
printf("enter rows and columns of matrix b\n");
scanf("%d%d",&p,&q);
if (n!=p)
{
printf("matrix multiplication not possible\n");
return 0;
}
printf("enter elements of matrix a\n");
for (i=0;i<m;i++)
{
for (j=0;j<n;j++)
{
scanf("%d",&a[i][j]); }
}
printf("enter 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<p;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("product of two matrices is\n");
for (i=0;i<m;i++)
{
for (j=0;j<q;j++)
{
printf("%d\n",c[i][j]);
}
}
getch();
}
7. Develop a Program to compute Sin(x) using Taylor series approximation.
Compare your result with the built-in Library function. Print both the results
with appropriate messages.
Algorithm
Step 1: [Start]
Step 2: [Read the value of x in degrees] Read x
Step 3: [Initialization & Radians conversion]
Temp=x
x=x*(3.142/180.0)
Term=x
Sinx=term
n=1
Step 4: [compute sin value]]
Repeat While (term>FLT_EPSILON)
Fact =2*n*(2*n+1)
Term=term*x*x/fact Sinx
sinx+term= n+1n
Step 5: [Output]
Print sin(x) without using library function Print sin(x) with using library
function
Step 6: [Stop]
Flowchart
PROGRAM:
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
void main()
{
int x,n,i;
float rad, res, 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("Calculate sin(%d) = %f", x,sum);
printf("\nLibrary sin(%d) = %f", x,sin(rad));
}
int fact(int m)
{
int i,f=1;
for(i=1;i<=m;i++)
{
f=f*i;
}
return 1;
}
8.Sort the given set of N numbers using Bubble sort
Algorithm
Step 1: Start
Step 2: Read the array of given items from the user.
Step 3: Take the first element(index = 0), compare the current element with the
next element.
Step 4: If the current element is greater than the next element, swap them.
Step 5: Else,
If the current element is less than the next element, then move to the next
element.
Step 6: Repeat Step 3 to Step 5 until all elements are sorted.
Step 7: Stop
Flowchart
#include<stdio.h>
#define maxsize 20
int main()
{
int array[maxsize];
int i, j, num, t;
printf("How many numbers you want to enter : ");
scanf("%d", &num);
printf("\nEnter %d numbers :\n",num);
for (i = 0; i < num; i++)
{
scanf("%d", &array[i]);
}
printf("\nInput array is : \n\n");
for (i = 0; i < num; i++)
{
printf("%d ", array[i]);
}
/* Bubble sorting begins */
for (i = 0; i < num; i++)
{
for (j = 0; j < (num - i - 1); j++)
{
if (array[j] > array[j + 1])
{
t = array[j];
array[j] = array[j + 1];
array[j + 1] = t;
}
}
} // end of sorting
printf("\n\nSorted array is : \n\n");
for (i = 0; i < num; i++)
{
printf("%d ", array[i]);
}
return 0;
9 Write functions to implement string operations such as compare,
concatenate, string length. Convince the parameter passing techniques.
Step 1: Start
Step 2: read two strings
Step 3: calculate the string length using strlen()
Step 4: compare the string using strcmp()
Step 5: concatenate the strings using strcat()
Step 6: print the stringlength
Step 7: print strings are equal or not equal
Step 8: print concatenated sting
Step 9: stop
Program
#include<stdio.h>
#include<string.h>
void stringlength(char a[100],char b[100]);
void concatenate(char a[100],char b[100]);
void stringcompare(char a[100],char b[100]);
void main()
{
char p[100],q[100],ch[100];
int len1,len2;
printf("Enter the first string:\n");
gets(p);
printf("Enter the second string:\n");
gets(q);
stringlength(p,q);
stringcompare(p,q);
concatenate(p,q);
}
void stringlength(char a[100], char b[100])
{
int len1,len2;
len1=strlen(a);
len2=strlen(b);
printf("First string length is :%d \nSecond string lenght is: %d",len1,len2);
}
void concatenate(char a[100], char b[100])
{
printf("The concatenated String is :%s ", strcat(a,b));
}
void stringcompare(char a[100], char b[100])
{
if(strcmp(a,b)==0)
printf("\nSTRINGS ARE EQUAL\n");
else
printf("\nSTRINGS ARE NOT EQUAL\n");
10. Implement structures to read, write, compute average- marks and the
students scoring above and below the average marks for a class of N students
ALGORITHM:
STEP 1: [input number of students]
Read n
STEP 2: [input details of students ie.name and marks]
Read name,m1,m2,m3
STEP 3: [ Calculate total and average]
For <- i=0 to n
s[i].total=(s[i].m1+s[i].m2+s[i].m3);
T=T+s[i].total;
AVG=T/N;
STEP 4: [Find students above and below average]
for <- i=0 to n
aboveavg[j]=i;
j++;
else
belowavg[k]=i;
k++;
STEP 5: [Finished]
STOP
Flowchart
#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;
float tavg,sum=0.0;
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",s[i].name);
else
printf("\n %s has scored below the average marks",s[i].name);
}
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.
Algorithm
Step-1: Start
Step-2: Read n
Step-3: For every value of n read the x
Step-4: Initialize sum=0 and i=0
Step-5: For every value of n and i, comuter sum using sum = sum + (*(x+i) –
mean) * ( * ( x+i) – mean)
Step-6: Using the sum value computer variance = sum / n and deviation = sqrt
( variance )
Step-7: Display mean, variance, deviation
Step-8: Stop
Flowchart
#include<stdio.h>
#include<math.h>
void main ()
{
float a[20], sum1 = 0, sum2 = 0, mean, var, dev;
int i, n;
printf ("Enter no of elements:");
scanf ("%d", &n);
printf ("Enter array elements:");
for (i = 0; i < n; i++)
{
scanf ("%f", a + i);
sum1 = sum1 + * (a + i);
}
mean = sum1 / n;
for (i = 0; i < n; i++)
{
sum2 = sum2 + pow ((*(a + i) - mean), 2);
}
var = sum2 / n;
dev = sqrt (var);
printf ("Sum :%f\n", sum1);
printf ("Mean :%f\n", mean);
printf ("Variance :%f\n", var);
printf ("Deviation :%f\n", dev);
}
12.Write a C program to copy a text file to another, read both the input file name
and target file name.
Step 1:Start
Step 2:open source file
Step 3:Read a source file
Step 4:copy the content of source to destination file character by character using
while loop
Step 5:Read the Destination file.
Step 6: stop.
#include <stdio.h>
#include <stdlib.h> // For exit()
int main()
FILE *fptr1, *fptr2;
char filename[100], c;
printf("Enter the filename to open for reading \n");
scanf("%s", filename);
// Open one file for reading
fptr1 = fopen(filename, "r");
if (fptr1 == NULL)
printf("Cannot open file %s \n", filename);
exit(0);
printf("Enter the filename to open for writing \n");
scanf("%s", filename);
// Open another file for writing
fptr2 = fopen(filename, "w");
if (fptr2 == NULL)
printf("Cannot open file %s \n", filename);
exit(0);
// Read contents from file
c = fgetc(fptr1);
while (c != EOF)
fputc(c, fptr2);
c = fgetc(fptr1);
printf("\nContents copied to %s", filename);
fclose(fptr1);
fclose(fptr2);
return 0;