CP Assignment Set 1-1
CP Assignment Set 1-1
Lab experiment 1
A C program to add two numbers and display its sum
Input
#include <stdio.h>
int main() {
int sum, n1, n2;
printf("ENTER NUMBER1\n");
scanf("%d",&n1);
printf("ENTER NUMBER2\n");
scanf("%d",&n2);
sum = n1 + n2;
printf("Sum: %d\n", sum);
return 0;
}
Output
Lab experiment 2
A C program to calculate and display the volume of cylinder
Input
#include <stdio.h>
int main(){
float v,h,r;
printf("ENTER HEIGHT OF CYLINDER\n");
scanf("%f",&h);
printf("ENTER RADIUS OF CYLINDER\n");
scanf("%f",&r);
v = 3.14 * r * r * h ;
printf("VOLUME OF CYLINDER IS:%f", v);
return 0;
}
Output
Lab experiment 3
Program to REALISE EXPRESSION
v=u+at
s=ut+1/2at²
T=2a+✓b+9c
Input
#include <stdio.h>
#include <math.h>
int main() {
float v,u,a,t,s,T,b,c;
printf("ENTER THE VALUE \n u: \n");
scanf("%f",&u);
Lab experiment 4
Program to display percentage result of students
Input
#include <stdio.h>
int main() {
char n[50];
int rn;
float s1;
float s2;
float s3;
float s4;
float s5;
float percent;
printf("Enter name: ");
scanf("%s", n);
printf("Enter roll number: ");
scanf("%d", &rn);
printf("Enter the marks\n");
printf("SUBJECT1:");
scanf("%f",&s1);
printf("SUBJECT2:");
scanf("%f",&s2);
printf("SUBJECT3:");
scanf("%g",&s3);
printf("SUBJECT4:");
scanf("%f",&s4);
printf("SUBJECT5:");
scanf("%f",&s5);
percent = (s1+s2+s3+s4+s5)/5;
return 0;
}
Output
Lab experiment 5
Program for swapping numbers without using a third variable
Input
#include <stdio.h>
int main() {
int a,b,c;
printf("ENTER THE VALUES OF a and b\n");
scanf("%d%d",&a,&b);
printf("Before swapping a: %d b:%d\n",a,b);
a=a+b;
b=a-b;
a=a-b;
c=a;
a=b;
b=c;
return 0;
}
Output
Lab experiment 6
Program of illustration of unary function in prefix and postfix increment and decrement
Input
#include <stdio.h>
int main() {
int a, b, c, d, e;
c = a++;
printf("Postfix Increment: a = %d, c = %d\n", a, c);
d = --a;
printf("Prefix Decrement: a = %d, d = %d\n", a, d);
e = a--;
printf("Postfix Decrement: a = %d, e = %d\n", a, e);
return 0;
}
Output
Lab experiment 7
Program to find largest of 3 by ternary operator
Input
#include <stdio.h>
int main() {
int a, b, c;
return 0;
}
Output
Lab experiment 8
Program to find roots of quadratic equation
Input
#include <stdio.h>
#include <math.h>
int main() {
int a, b, c;
printf("ENTER THE VALUE OF a,b and c: \n");
scanf("%d%d%d",&a,&b,&c);
printf("%lf\n", x1);
printf("%lf\n", x2);
return 0;
}
Output
Lab experiment 9
Program to find a number is prime or not
Input
#include <stdio.h>
int main() {
int n, i, primeis = 1;
printf("Enter a number: ");
scanf("%d", &n);
if (n <= 1) {
primeis= 0;
} else {
for (i = 2; i * i <= n; i++) {
if (n % i == 0) {
primeis = 0;
break;
}
}
}
if (primeis)
printf("%d is a prime number.\n", n);
else
printf("%d is not a prime number.\n", n);
return 0;
}
Output
Lab experiment 10
Program to find grade of a student
Input
#include <stdio.h>
int main() {
int marks,s1,s2,s3,s4,s5;
printf("subject 1:\n");
scanf("%d",&s1);
printf("subject 2:\n");
scanf("%d",&s2);
printf("subject 3:\n");
scanf("%d",&s3);
printf("subject 4:\n");
scanf("%d",&s4);
printf("subject 5 :\n");
scanf("%d",&s5);
marks=(s1+s2+s3+s4+s5)/5;
return 0;
}
Output
Lab experiment 11
A program to find a year is a leap year or not
Input
#include<stdio.h>
int main(){
int year,lp;
printf("Enter the year\n");
scanf("%d", &year);
return 0;
}
Output
Lab experiment 12
Program to find Sum of digits of a number
Input
#include <stdio.h>
#include<conio.h>
int main() {
int n,t;
printf("Enter a number: ");
scanf("%d", &n);
int sod= 0;
for (t=n;t>0;t=t/10)
{
int d=t%10;
sod =sod+d;
}
printf("Sum of digits: %d\n", sod);
getch();
}
Output
Lab experiment 13
Program to make the following patterns
Input
#include<stdio.h>
#include<conio.h>
int main(){
//A
int j,i,n,r,k;
printf("ENTER the number till you want this pattern ");
scanf("%d",&r);
for(r;r>=1;r--)
{
for(i=1;i<=r;i++)
{printf("*");
}
printf("\n");
}
//B
//C
//D
char ch;
//E
//F
//G
if (j < i)
{
printf(" ");
}
}
if (j > 0)
{
//printf(" ");
}
}
printf("\n");
}
//H
printf("\n");
}
getch();
}
Output
Lab experiment 14
A C program to find the factorial of a number using recursion
Input
#include <stdio.h>
#include <conio.h>
int factorial(int n) {
if (n == 0 || n == 1) {
return 1;
} else {
return n *factorial(n - 1);
}
}
int main() {
int num;
getch();
}
Output
Lab experiment 15
Program to add two matrices of same order
Input
#include <stdio.h>
#include<conio.h>
void addMatrices(int mat1[10][10], int mat2[10][10], int result[10][10], int rows, int colms) {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < colms; j++) {
result[i][j] = mat1[i][j] + mat2[i][j];
}
}
}
int main() {
int mat1[10][10], mat2[10][10], result[10][10];
int rows, colms;
getch();
}
Output
Lab experiment 16
Program to find sum of 2 complex numbers
Input
#include <stdio.h>
#include<conio.h>
struct Complex {
float real;
float imaginary;
};
int main() {
struct Complex num1, num2, sum;
printf("Enter real and imaginary parts of the first complex number: ");
scanf("%f %f", &num1.real, &num1.imaginary);
printf("Enter real and imaginary parts of the second complex number: ");
scanf("%f %f", &num2.real, &num2.imaginary);
getch();
}
Output
Lab experiment 17
To display the cgpa of students and highest cgpa of the class
Input
#include <stdio.h>
#include <string.h>
struct Student {
char name[50];
int rollNumber;
float marks;
};
struct StudentClass {
char name[50];
int id;
int semester;
float cgpa;
static float maxCgpa[10];
StudentClass() : id(0), semester(0), cgpa(0.0) {
strcpy(name, "");
}
void getDetails() {
printf("Name: %s\nID: %d\nSemester: %d\nCGPA: %f\n", name, id, semester, cgpa);
}
int main() {
return 0;
}
Output