[go: up one dir, main page]

0% found this document useful (0 votes)
0 views36 pages

Cp Lab Programes

Download as pdf or txt
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 36

1) C program to find sum and average of 3 numbers.

#include<stdio.h>
int main()
{
int a,b,c,avg,sum;
printf("enter numbers a,b,c");
scanf("%d %d %d",&a,&b,&c);
avg=(a+b+c)/3;
sum=a+b+c;
printf("average of 3 numbers is:%d",avg);
printf("sum of 3 numbers is:%d",sum);
return 0;
}

Output::
enter numbers a,b,c10 12 1
average of 3 numbers is:7sum of 3 numbers is:23

2)C program to convert temperature from Celsius to Fahrenheit


and viceversa.
#include<stdio.h>
int main()
{
float celsius,fahrenheit;
printf("\n enter temperature in fahrenheit");
scanf("%f",&fahrenheit);
celsius=(fahrenheit-32)/1.8;
printf("\n temperature in celsius is:%f",celsius);
printf("\n enter temperature in celsius");
scanf("%f",&celsius);
fahrenheit=(1.8*celsius)+32;
printf("\n temperature in fahrenheit is:%f",fahrenheit);
return 0;
}
Output::
enter temperature in Fahrenheit 102
temperature in celsius is:38.888889
enter temperature in celsius43
temperature in fahrenheit is:109.400002

3)C program to find simple interest.


#include<stdio.h>
int main()
{
float P,R,T,SI;
printf("enter Principle");
scanf("%f",&P);
printf("enter the rate");
scanf("%f",&R);
printf("enter the time");
scanf("%f",&T);
SI=(P*R*T)/100;
printf("simple interest is:%f",SI);
return 0;
}
Output::
enter Principle1000
enter the rate5
enter the time2
simple interest is:100.000000

4 ) c program to find the square root of a given number.


#include <stdio.h>
#include <math.h>
int main() {
double num, result;
printf("Enter a number: ");
scanf("%lf", &num);
if(num < 0) {
printf("Square root of a negative number is imaginary\n");
return 0;
}
result = sqrt(num);
printf("Square Root of %.2lf is: %.2lf\n", num, result);
return 0;
}
Output::
Enter a number: 9
Square Root of 9.00 is: 3.00
5) C program to find the compound interest
#include <stdio.h>
#include <math.h>

int main()
{
double Principal;
printf("Enter the Principal Amount: ");
scanf("%lf", &Principal);

double Rate;
printf("Enter the Interest Rate: ");
scanf("%lf", &Rate);

double Time;
printf("Enter the Time Period(in Years): ");
scanf("%lf", &Time);

double Amount;
Amount = Principal * pow((1 + Rate / 100), Time);

double Compound_Interest;
Compound_Interest = Amount - Principal;
printf("The Compound Interest is %.2lf ",Compound_Interest);
return 0;
}

Output::
Enter the Principal Amount: 5000
Enter the Interest Rate: 5
Enter the Time Period(in Years): 5
The Compound Interest is 1381.41

Note:: CI = P×(1+R/100)^T – P
A=P×(1+R100)T
CI = A – P

6) Write a C program to calculate Area of a triangle using heron’s


formulae

#include<stdio.h>
#inclede<math.h>
int main()
{
double a, b, c, s, area;
printf("Enter the lengths of the three sides of the triangle:\n");
printf("Side a: ");
scanf("%lf", &a);
printf("Side b: ");
scanf("%lf", &b);
printf("Side c: ");
scanf("%lf", &c);

s = (a + b + c) / 2.0;
area = sqrt(s * (s - a) * (s - b) * (s - c));
if (area > 0)
{
printf("The area of the triangle is: %.2lf\n", area);
}
else
{
printf("Entered values of sides do not form a valid triangle.\n");
}
return 0;
}
Output::
Enter the lengths of the three sides of the triangle:
Side a: 20
Side b: 30
Side c: 40
The area of the triangle is: 290.47

Note:: area of the triangle is = sqrt(s * (s – a) * (s – b) * (s – c))


7) Write a C program to calculate Distance travelled by an object

#include<stdio.h>
int main()
{
float s;
int u, t, a;
printf("Please enter the initial velocity = ");
scanf("%d",&u);
printf("Please enter the time = ");
scanf("%d",&t);
printf("Please enter acceleration = ");
scanf("%d",&a);
s=(u*t)+(0.5*a*t*t);
printf("Total distance is %f",s);
return 0;
}

Output::
Please enter the initial velocity = 100
Please enter the time = 60
Please enter acceleration = 70
Total distance is 132000.000000

Note::distance formula s=ut+1/2(a*t*t)


8) C program to find maximum of 3 numbers using ternary
operator.
# include <stdio.h>
int main()
{
int a, b, c, big ;
printf("Enter three numbers : ") ;
scanf("%d %d %d", &a, &b, &c) ;
big = (a > b) ? ((a > c) ? a : c) : ((b > c) ? b : c) ;
printf("\nThe biggest number is : %d", big) ;
return 0;
}

Output::
Enter three numbers : 10 20 30
The biggest number is : 30

9) c program to take marks of 5 subjects in integer and take


average and total in float.
#include<stdio.h>
int main()
{
int m1,m2,m3,m4;
float total,avg;
printf(" enter m1,m2,m3,m4");
scanf("%d %d %d %d",&m1,&m2,&m3,&m4);
total=m1+m2+m3+m4;
printf("total is %f",total);
avg=(m1+m2+m3+m4)/4;
printf("avg is % f",avg);
return 0;
}
Output::
enter m1,m2,m3,m410 20 30 40
total is 100.000000avg is 25.000000

10) C program to find max and min among 4 numbers.


#include<stdio.h>
int main()
{
int a, b, c, d;

printf("Enter 4 intgeres to find largest and smallest: ");


scanf("%d %d %d %d", &a, &b, &c, &d);

if (a > b && a > c && a > d)


printf("Largest: %d\n", a);
if (b > a && b > c && b > d)
printf("Largest: %d\n", b);
if (c > a && c > b && c > d)
printf("Largest: %d\n", c);
if (d > a && d > a && d > c)
printf("Largest: %d\n", d);
if (a < b && a < c && a < d)
printf("Smallest: %d\n", a);
if (b < a && b < c && b < d)
printf("Smallest: %d\n", b);
if (c < a && c < b && c < d)
printf("Smallest: %d\n", c);
if (d < a && d < a && d < c)
printf("Smallest: %d\n", d);

return 0;
}
Output::
Enter 4 intgeres to find largest and smallest: 10 20 30 40
Largest: 40
Smallest: 10

11) Write a C program to generate electricity bill.


#include <stdio.h>
int main(){
int units;
float amt, unitcharg, total;
printf(" Enter no of units consumed : ");
scanf("%d", &units);
if (units < 50){
amt = units * 3.50;
unitcharg = 25;
}else if (units <= 100){
amt = 130 + ((units - 50 ) * 4.25);
unitcharg = 35;
}else if (units <= 200){
amt = 130 + 162.50 + ((units - 100 ) * 5.26);
unitcharg = 45;
}else{
amt = 130 + 162.50 + 526 + ((units - 200 ) * 7.75);
unitcharg = 55;
}
total= amt+ unitcharg;
printf("electricity bill = %.2f", total);
return 0;
}
Output::
Enter no of units consumed : 50
electricity bill = 165.00

12) C program to find roots of quadratic equation.


# include<stdio.h>
# include<math.h>
int main ()
{
float a,b,c,r1,r2,d;
printf ("Enter the values of a b c: ");
scanf (" %f %f %f", &a, &b, &c);
d= b*b - 4*a*c;
if (d>0) {
r1 = -b+sqrt (d) / (2*a);
r2 = -b-sqrt (d) / (2*a);
printf ("The real roots = %f %f", r1, r2);
}
else if (d==0) {
r1 = -b/(2*a);
r2 = -b/(2*a);
printf ("Roots are equal =%f %f", r1, r2);
}
else
printf("Roots are imaginary");
return 0;
}
Output::
Enter the values of a b c: 30 40 50
Roots are imaginary

13) C program to simulate calculator


#include<stdio.h>
int main()
{

char ch;
int a, b, result;
printf("Enter an Operator (+, -, *, /): ");
scanf("%c", &ch);
printf("Enter two operands: \n");
scanf("%d %d", &a, &b);
switch(ch){
case '+':
result = a + b;
break;
case '-':
result = a - b;
break;
case '*':
result = a * b;
break;
case '/':
result = a / b;
break;
}
printf("Result = %d", result);
return 0;
}
Output::
Enter an Operator (+, -, *, /): -
Enter two operands:
20 10
Result = 10
14) C program to find given year is leap year or not.
#include<stdio.h>
void main()
{
int year;
printf("Enter a year \n");
scanf("%d", &year);
if ((year % 400) == 0)
printf("%d is a leap year \n", year);
else if ((year % 100) == 0)
printf("%d is a not leap year \n", year);
else if ((year % 4) == 0)
printf("%d is a leap year \n", year);
else
printf("%d is not a leap year \n", year);
}
Output::
Enter a year 2024
2024 is a leap year

15) C program to find the factorial of given number.


#include<stdio.h>
int main()
{
int num, i, fact=1;
printf("Enter any number: ");
scanf("%d", &num);
for(i=num; i>0; i--)
fact = fact*i;
printf("\nFactorial of %d = %d", num, fact);
return 0;
}
Output::
Enter any number: 30
Factorial of 30 = 1409286144

16) C program to find given number is prime or not.


#include <stdio.h>
int main()
{

int n, i, flag = 0;
printf("Enter a positive integer: ");
scanf("%d", &n);
if (n == 0 || n == 1)
flag = 1;
for (i = 2; i <= n / 2; ++i)
{
if (n % i == 0) {
flag = 1;
break;
}
}

if (flag == 0)
printf("%d is a prime number.", n);
else
printf("%d is not a prime number.", n);

return 0;
}
Output::
Enter a positive integer: 7
7 is a prime number.

17) C program to find given number is palindrome or not.


#include <stdio.h>
void main()
{
int input, reverse = 0, rem, temp;
printf("Please enter an integer number:");
scanf("%d", & input);
temp = input;
while (temp != 0)
{
rem = temp % 10;
reverse = reverse * 10 + rem;
temp /= 10;
}
if (reverse == input)
printf("%d is a palindrome number.", input);
else
printf("%d is not a palindrome number.", input);
}
Output::
Please enter an integer number:153
153 is not a palindrome number

18) c program to Construct a pyramid of numbers.

#include <stdio.h>
int main()
{
int i, j, rows;
printf("Enter the number of rows: ");
scanf("%d", &rows);
for (i = 1; i <= rows; ++i) {
for (j = 1; j <= i; ++j) {
printf("%d",i);
}
printf("\n");
}
return 0;
}
Output::
Enter the number of rows: 4
1
22
333
4444

19) c program to find min and max of a 1-D integer array.

#include <stdio.h>
#include <conio.h>
int main()
{
int a[1000],i,n,min,max;
printf("Enter size of the array : ");
scanf("%d",&n);
printf("Enter elements in array : ");
for(i=0; i<n; i++)
{
scanf("%d",&a[i]);
}
min=max=a[0];
for(i=1; i<n; i++)
{
if(min>a[i])
min=a[i];
if(max<a[i])
max=a[i];
}
printf("minimum of array is : %d",min);
printf("\nmaximum of array is : %d",max);
return 0;
}
Output:
Enter size of the array : 5
Enter elements in array : 1 2 3 4 5
minimum of array is : 1
maximum of array is : 5

20 ) C PROGRAM TO Perform Addition of two matrices


#include <stdio.h>
int main() {
int r, c, a[100][100], b[100][100], sum[100][100], i, j;
printf("Enter the number of rows (between 1 and 100): ");
scanf("%d", &r);
printf("Enter the number of columns (between 1 and 100): ");
scanf("%d", &c);
printf("\nEnter elements of 1st matrix:\n");
for (i = 0; i < r; ++i)
for (j = 0; j < c; ++j)
{
scanf("%d", &a[i][j]);
}

printf("Enter elements of 2nd matrix:\n");


for (i = 0; i < r; ++i)
for (j = 0; j < c; ++j) {
scanf("%d", &b[i][j]);
}
// adding two matrices
for (i = 0; i < r; ++i)
for (j = 0; j < c; ++j) {
sum[i][j] = a[i][j] + b[i][j];
}

printf("\nSum of two matrices: \n");


for (i = 0; i < r; ++i)
for (j = 0; j < c; ++j) {
printf("%d ", sum[i][j]);
if (j == c - 1) {
printf("\n\n");
}
}

return 0;
}

Output:
Enter the number of rows (between 1 and 100): 2
Enter the number of columns (between 1 and 100): 2
Enter elements of 1st matrix: 1 2 3 4
Enter elements of 2nd matrix: 5 6 7 8
Sum of two matrices: 6 8
10 12

21) C PROGRAM TO Perform multiplication of two matrices.

#include <stdio.h>

int main()
{
int m, n, p, q, c, d, k, sum = 0;
int first[10][10], second[10][10], multiply[10][10];

printf("Enter number of rows and columns of first matrix\n");


scanf("%d%d", &m, &n);
printf("Enter elements of first matrix\n");

for (c = 0; c < m; c++)


for (d = 0; d < n; d++)
scanf("%d", &first[c][d]);

printf("Enter number of rows and columns of second matrix\n");


scanf("%d%d", &p, &q);

if (n != p)
printf("The multiplication isn't possible.\n");
else
{
printf("Enter elements of second matrix\n");

for (c = 0; c < p; c++)


for (d = 0; d < q; d++)
scanf("%d", &second[c][d]);

for (c = 0; c < m; c++) {


for (d = 0; d < q; d++) {
for (k = 0; k < p; k++) {
sum = sum + first[c][k]*second[k][d];
}
multiply[c][d] = sum;
sum = 0;
}
}

printf("Product of the matrices:\n");

for (c = 0; c < m; c++) {


for (d = 0; d < q; d++)
printf("%d\t", multiply[c][d]);

printf("\n");
}
}

return 0;
}

Output:

Enter number of rows and columns of first matrix


22
Enter elements of first matrix
1234
Enter number of rows and columns of second matrix
22
Enter elements of second matrix
4567
Product of the matrices:
16 19
36 43

22) C PROGRAM TO concatenate two strings without using built-in


functions.

#include <stdio.h>
#include <string.h>
int main()
{
char str1[100], str2[100];
int i, j;
printf("Enter the first string: ");
gets(str1);
printf("Enter the second string: ");
gets(str2);
// To iterate the first string from beginning to end
for (i = 0; str1[i]!='\0'; i++);
// Concatenate Str2 with Str1
for (j = 0; str2[j]!='\0'; j++, i++)
{
str1[i] = str2[j];
}
str1[i] = '\0';
printf("After the concatenation: %s\n", str1);
return 0;
}
Output:
Enter the first string: hello
Enter the second string: world
After the concatenation: helloworld

23) C PROGRAM TO reverse a string without using& with using


built-in functions.
without using::
#include <stdio.h>
#include <string.h>
int main()
{
char string[20],temp;
int i,length;
printf("Enter String : ");
scanf("%s",string);
length=strlen(string)-1;
for(i=0;i<strlen(string)/2;i++){
temp=string[i];
string[i]=string[length];
string[length--]=temp;
}
printf("Reverse string :%s",string);
return 0;
}
Output::
Enter String : hello
Reverse string :olleh

with using built-in functions::


#include <stdio.h>
#include <string.h>
int main()
{
char s[100];
printf("Enter a string to reverse\n");
gets(s);
strrev(s);
printf("Reverse of the string: %s\n", s);
return 0;
}
Output:
Enter String : hello
Reverse string :olleh

24) Write a C function to find the length of a string


#include <stdio.h>
#include <string.h>

int main()
{
char str[1000] = "helloworld";

// Calling strlen with Str as argument


int len = strlen(str);
printf("Length of the String is %d", len);
return 0;
}
Output::
Length of the String is 10

25) Write a C function to find transpose of a matrix.


#include <stdio.h>
#define N 4
void transpose(int A[][N], int B[][N])
{
int i, j;
for (i = 0; i < N; i++)
for (j = 0; j < N; j++)

B[i][j] = A[j][i];
}
int main()
{
int A[N][N] = { { 1, 1, 1, 1 },
{ 2, 2, 2, 2 },
{ 3, 3, 3, 3 },
{ 4, 4, 4, 4 } };

int B[N][N], i, j;

transpose(A, B);

printf("Result matrix is \n");


for (i = 0; i < N; i++) {
for (j = 0; j < N; j++)
printf("%d ", B[i][j]);
printf("\n");
}

return 0;
}
Output::
Result matrix is
1234
1234
1234
1234

26) Write a recursive function to generate Fibonacci series in c.


#include<stdio.h>
int fibbonacci(int n) {
if(n == 0){
return 0;
} else if(n == 1) {
return 1;
} else {
return (fibbonacci(n-1) + fibbonacci(n-2));
}
}
int main() {
int n = 5;
int i;
printf("Fibbonacci of %d: " , n);
for(i = 0;i<n;i++) {
printf("%d ",fibbonacci(i));
}
}
Output::
Fibbonacci of 5: 0 1 1 2 3

27) Write a recursive function to factorial of a number in c.

#include <stdio.h>
int factorial(int n) {
//base case
if(n == 0) {
return 1;
} else {
return n * factorial(n-1);
}
}
int main() {
int n ;
printf("enter a number");
scanf("%d",&n);
printf("Factorial of %d: %d\n" , n , factorial(n));
}
Output::
Enter any number: 5
Factorial of 5 = 120
28) Write a recursive to find sum of series in c.

#include <stdio.h>
int sum(int n);

int main() {
int number, result;

printf("Enter a positive integer: ");


scanf("%d", &number);

result = sum(number);

printf("sum = %d", result);


return 0;
}

int sum(int n) {
if (n != 0)
// sum() function calls itself
return n + sum(n-1);
else
return n;
}
Output::
Enter a positive integer: 6
sum = 21

29) Write a C program to swap two numbers using call by


reference
#include <stdio.h>
int swap (int *, int *);
int main()
{
int a, b;
printf("\nEnter value of a & b: ");
scanf("%d %d", &a, &b);
printf("\nBefore Swapping:\n");
printf("\na = %d\n\nb = %d\n", a, b);
swap(&a, &b);
printf("\nAfter Swapping:\n");
printf("\na = %d\n\nb = %d", a, b);
return 0;
}
swap (int *x, int *y)
{
int temp;
temp = *x;
*x = *y;
*y = temp;
}
Output::
Enter value of a & b: 6 7
Before Swapping:
a=6
b=7
After Swapping:
a=7
b=6

30) Write a C program to copy one string into another using


pointer

#include<stdio.h>
void copy_string(char*, char*);
int main()
{
char source[100], target[100];
printf("Enter source string\n");
gets(source);

copy_string(target, source);
printf("Target string is \"%s\"\n", target);
return 0;
}
void copy_string(char *target, char *source)
{
while(*source)
{
*target = *source;
source++;
target++;
}
*target = '\0';
}
Output::
Enter source string
world
Target string is "world"

31) Write a C program to write and read text into a file


#include <stdio.h>
int main()
{
char name[50];
int marks, i, num;
printf("Enter number of students: ");
scanf("%d", &num);
FILE *fptr;
fptr = (fopen("C:\\student.txt", "w"));
if(fptr == NULL)
{
printf("Error!");

for(i = 0; i < num; ++i)


{
printf("For student%d\nEnter name: ", i+1);
scanf("%s", name);

printf("Enter marks: ");


scanf("%d", &marks);

fprintf(fptr,"\nName: %s \nMarks=%d \n", name, marks);


}

fclose(fptr);
return 0;
}
Output::
Enter number of students: 2
Error!For student1
Enter name: kiran
Enter marks: 70
For student2
Enter name: hari
Enter marks: 60

You might also like