CP Lab Programs
CP Lab Programs
I SEMESTER – B. E
LHS RHS
purpose
Procedure
Input
expected output
Program
LHS RHS
Result: Program
Conclusion
Output:
#include<stdio.h>
#include<stdlib.h>
void main()
{
float a,b,res;
char ch;
switch(ch)
{
case '+':res=a+b;
break;
case '-':res=a-b;
break; case '*':res=a*b;
break; case '/':if(b!=0)
{
res=a/b;
}
else
{
printf("divide by zero error");
exit(0);
}
break;
default:printf("not a valid operator");
exit(0);
}
printf("result=%f",res);
}
Output:
Operator= +
1 Res=19 Res=19 PASS
a=9,b=10
#include<stdio.h>
int main()
{
char name[20];
int n; float amount;
{
amount=(n-300)*100;
amount=amount+100*90;
amount=amount+200*80;
}
amount=amount/100;
amount=amount+100;
if(amount>400)
{
amount=amount+15*amount/100;
}
printf("Total amount to be paid is %.2f Rs",amount);
}
Output:
Input
Test No Expected output Obtained output Remarks
Parameters
#include<stdio.h>
int main()
{
int a[30],i,num,key,low,mid,high;
printf("\n enter the no of elements");
scanf("%d",&num);
printf("\n enter the elements : ");
for(i=0;i<=high;i++)
{
scanf("%d",&a[i]);
}
printf("\nenter the key element to be searched\n");
scanf("%d",&key);
low=0;
high=num-1;
while(low<=high)
{
mid=(low+high)/2;
if(a[mid]==key)
{
printf("element %d is found at %d position :",key,mid+1);
exit(0);
}
else if(a[mid]>key)
high=mid-1;
else low=mid+1;
}
printf("UNSUCCESSFULL SEARCH\n");
return 0;
}
Result :
if (ctr == 0 && num != 1) // If no divisors were found and the number is not 1...
printf("%d ", num); // ...print the prime number.
}
printf("\n"); // Move to the next line after printing all prime numbers.
Result:
6. Develop a program to sort the given set of N numbers using Bubble sort
Purpose: This program demonstrates NESTED FOR loop.
Procedure: To read an array of elements a[ ] . While iterating, compare each pair of
adjacent items in every pass. If the former value is greater than the latter one, their
positions are swapped. Over a number of passes, at most equal to the number of
elements in the list, all of the values drift into their correct positions. Then print
sorted array elements.
Input: Number of Elements – n An array of unsorted elements – a[ ]
Output: An array of sorted elements – a[ ]
#include<stdio.h>
#include<conio.h>
int main()
{
int n,i,j,a[10],temp;
int i, degree;
float x, sum=0,term,nume,deno;
8. C program that calculates IHP, BHP, and CR for a four-stroke gas engine:
/*Concepts:
Program
#include <stdio.h>
int main() {
float bp, ip, rpm, lan, d, sp, v, cr;
// Calculations:
ip = bp / 0.85; // Assuming mechanical efficiency of 85%
v = 3.14159 * d * d * sp * 0.001 / 4; // Engine displacement in
liters
return 0;
}
#include <stdio.h>
int main() {
int m, n, p, q, i, j, k;
return 0;
}
10. Write functions to implement string operations such as copy, compare and
concatenate using user defined functions
#include<stdio.h>
int main()
{
char s1[200], s2[100];
int len,res,count;
printf("\nEnter the String s1: ");
gets(s1);
printf("\nEnter String s2 :");
gets(s2);
len = Length(s1);
printf("\nLength of the String s1 is : %d", len);
{
if(s1[count] == '\0' || s2[count] == '\0')
break;
else count++;
}
if(s1[count] == '\0' && s2[count] == '\0')
return 0;
else
return -1;
}
#include <stdio.h>
#include <string.h>
{
int len = strlen(str);
for (int i = 0; i < len / 2; i++) {
char temp = str[i];
str[i] = str[len - i - 1];
str[len - i - 1] = temp;
}
}
return len;
}
int main() {
char str1[50], str2[50];
reverse_string(str1);
printf("Reversed first string: %s\n", str1);
return 0;
}
#include <stdio.h>
int convert(int);
int main()
{
int dec, bin;
printf("Enter a binary number: ");
scanf("%d", &bin);
dec = convert(bin);
printf("The decimal equivalent of %d is %d.\n", bin,dec);
return 0;
}
int convert(int bin)
{
if (bin == 0)
{
Dept. of ECE, SJBIT Page 6
C Programming 23CPI15B
return 0;
}
else
{
return (bin % 10 + 2 * convert(bin / 10));
}
13. 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[50];
char name[50];
int marks;
}
s[10];
void main()
{
int i,n,countav=0,countbv=0;
float sum,average;
printf("Enter number of Students\n");
scanf("%d",&n);
printf("Enter information of students:\n");
for(i=0; i<n;i++)
{
printf("Enter USN: ");
scanf("%s",s[i].usn);
printf("Enter name: ");
scanf("%s",s[i].name);
printf("Enter marks: ");
scanf("%d",&s[i].marks);
printf("\n");
}
for(i=0;i<n;i++)
{
sum=sum+s[i].marks;
}
average=sum/n;
printf("\nAverage marks: %f",average);
countav=0;
countbv=0;
for(i=0;i<n;i++)
{
if(s[i].marks>=average)
countav++;
else
countbv++;
}
printf("\nTotal No of students above average= %d",countav);
printf("\nTotal No of students below average= %d",countbv);
}
14. Develop a program using pointers to compute the sum mean and standard
deviation of all elements stored in an array of N real numbers.
Dept. of ECE, SJBIT Page 7
C Programming 23CPI15B
#include<stdio.h>
#include <conio.h>
#include <math.h>
int main()
{
float a[10], *ptr, mean, std, sum=0, sumstd=0;
int n,i;
printf("Enter the no of elements\n");
scanf("%d",&n);
printf("Enter the array elements\n");
for(i=0;i<n;i++)
{
scanf("%f",&a[i]);
}
ptr=a;
for(i=0;I <n;i++)
{
sum=sum+ *ptr;
ptr++;
}
mean=sum/n;
ptr=a;
for(i=0;I <n;i++)
{
sumstd=sumstd + pow((*ptr - mean),2);
ptr++;
}
std= sqrt(sumstd/n);
printf("Sum=%.3f\t",sum);
printf("Mean=%.3f\t",mean);
printf("Standard deviation=%.3f\t",std);
return 0;
}
15. Write a c program to copy a text file to another , reading both the input file
name and target file name.