Cp Lab Programes
Cp Lab Programes
Cp Lab Programes
#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
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
#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
#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
Output::
Enter three numbers : 10 20 30
The biggest number is : 30
return 0;
}
Output::
Enter 4 intgeres to find largest and smallest: 10 20 30 40
Largest: 40
Smallest: 10
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
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.
#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
#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
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
#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];
if (n != p)
printf("The multiplication isn't possible.\n");
else
{
printf("Enter elements of second matrix\n");
printf("\n");
}
}
return 0;
}
Output:
#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
int main()
{
char str[1000] = "helloworld";
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);
return 0;
}
Output::
Result matrix is
1234
1234
1234
1234
#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;
result = sum(number);
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
#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"
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