Cprogramming Lab Manual
Cprogramming Lab Manual
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.
121
12321
1234321
---------------------------
nth row
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
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
program
#include <stdio.h>
#include <stdlib.h>
int main()
char ch;
double a, b;
while (1) {
if (ch == 'x')
exit(0);
switch (ch) {
// For Addition
case '+':
break;
// For Subtraction
case '-':
break;
// For Multiplication
case '*':
// For Division
case '/':
break;
default:
printf(
printf("\n");
}}
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
void main()
scanf("%d%d%d", &a, &b, &c); // Read and store coefficients in 'a', 'b', and 'c'.
x2 = x1;
}
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;
}
121
12321
1234321
---------------------------
nth row
Algorithm
Step 7:stop.
Program
#include<stdio.h>
#include<conio.h>
void main()
clrscr();
int n,i,j,k,l;
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);
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;
}
Algorithm
Step 1: [Start]
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 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;
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 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);
}
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 5: [Finished]
STOP
Flowchart
#include<stdio.h>
struct student
char usn[10];
char name[10];
float m1,m2,m3;
float avg,total;
};
void main()
int n,i;
float tavg,sum=0.0;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%s",s[i].usn);
scanf("%s",s[i].name);
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)
else
}
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 4:copy the content of source to destination file character by character using
while loop
Step 6: stop.
#include <stdio.h>
int main()
char filename[100], c;
scanf("%s", filename);
if (fptr1 == NULL)
exit(0);
if (fptr2 == NULL)
exit(0);
c = fgetc(fptr1);
while (c != EOF)
fputc(c, fptr2);
c = fgetc(fptr1);
fclose(fptr2);
return 0;