0% found this document useful (0 votes)
34 views

CP Assignment Set 1-1

Project's and assignment to practice for computer programming Of C, and C++ For basics and can be helpful for 1st years doing btech and learning a computer language
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views

CP Assignment Set 1-1

Project's and assignment to practice for computer programming Of C, and C++ For basics and can be helpful for 1st years doing btech and learning a computer language
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 19

Computer programming

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);

printf("ENTER THE VALUE \n a: \n");


scanf("%f",&a);
printf("ENTER THE VALUE \n t: \n");
scanf("%f",&t);

printf("ENTER THE VALUE \n b: \n");


scanf("%f",&b);

printf("ENTER THE VALUE \n c:\n");


scanf("%f",&c);
v=u+a*t;
printf("v: %f \n", v);
s=u*t+(a*t*t)/2;
printf("s: %f \n", s);
T=2*a+sqrt(b)+9*c;
printf("T: %f \n", T);
return 0;
}
Output

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;

printf("\nStudent Name: %s\n", n);


printf("Roll Number: %d\n", rn);
printf("Percentage: %.2f%%\n", percent);

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;

printf("AFTER swapiing without using a third variable a:%d b:%d\n",a,b);

printf("enter the values of a and b\n");


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

printf("Before swapping value of a:%d b:%d\n",a,b);

c=a;
a=b;
b=c;

printf("After swapping values with third variable a:%d b:%d ",a,b);

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;

printf("enter value to be increment or decrement ");


scanf("%d",&a);
b = ++a;
printf("Prefix Increment: a = %d, b = %d\n", a, b);

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;

printf("Enter three numbers: ");


scanf("%d %d %d", &a, &b, &c);
int largest = (a> b) ? ((a>c) ? a:c) : ((b>c)? b:c);

printf("The largest number is: %d\n", largest);

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);

double d = sqrt(b * b - 4 * a * c);


double x1 = (-b + d) / (2 * a);
double x2 = (-b - d) / (2 * a);

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("Enter the marks of : ");

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;

if (marks >= 60 && marks <= 100) {


printf("1st Grade\n");
} else if (marks >= 45 && marks <= 59) {
printf("2nd Grade\n");
} else if (marks >= 33 && marks <= 44) {
printf("3rd Grade\n");
} else {
printf("Fail\n");
}

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);

(year%4==0 && (year%100!=0 || year%400==0)) ?


printf("THE GIVEN YEAR IS A LEAP YEAR:%d",year):
printf("THE GIVEN YEAR IS NOT A LEAP YEAR:%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

printf("ENTER the number till you want this pattern ");


scanf("%d",&n);
for(j=1;j<=n;j++)
{
for(i=1;i<=j;i++)
{printf("%d",j);
}
printf("\n");
}

//C

printf("ENTER the number till you want this pattern ");


scanf("%d",&n);

for ( i = 1; i <= n; ++i) {


for ( j = 1; j <= i; ++j) {
printf("%d ", j);
}
printf("\n");
}

//D

char ch;

printf("ENTER the number till you want this pattern ");


scanf("%d",&n);

for (i = 0; i < n; i++) {


ch = 'A';
for ( j = 0; j <= i; j++) {
printf("%c ", ch + j);
}
printf("\n");
}

//E

printf("Enter the number till you want the pattern ");


scanf("%d",&r);
for(j=0;j<r;j++)
{
for(i=0;i<2*(r-j)-1;i++)
{printf(" ");
}
for(k=0;k<(2 * j) + 1;k++)
{
printf("* ");
}
printf("\n");
}

//F

printf("Enter the number till you want the pattern ");


scanf("%d",&r);
for(j=0;j<r;j++)
{
for(i=0;i<2*j;i++)
{printf(" ");
}
for(k=0;k<2 *(r-j)-1;k++)
{
printf("* ");
}
printf("\n");
}

//G

printf("Enter the number of rows: ");


scanf("%d", &r);

for (int i = 0; i < r; i++)


{

for (j = 0; j < r - i - 1; j++) {


printf(" ");
}

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


{
printf("%d", j + 1);

if (j < i)
{
printf(" ");
}
}

for ( j = i -1; j >=0; j--)


{
printf(" %d", j + 1);

if (j > 0)
{
//printf(" ");
}

}
printf("\n");
}

//H

printf("Enter the rows : ");


scanf("%d",&r);
for (int i = 0; i < r ;i++) {

for (int j = 0; j < i; j++) {


printf(" ");
}

for (int k = 0; k < r - i; k++) {


printf("%c", 'A' + k);
}

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;

printf("Enter a number: ");


scanf("%d", &num);

printf("Factorial of %d = %d\n", num, factorial(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];
}
}
}

void displayMatrix(int mat[10][10], int rows, int colms) {


for (int i = 0; i < rows; i++) {
for (int j = 0; j < colms; j++) {
printf("%d\t", mat[i][j]);
}
printf("\n");
}
}

int main() {
int mat1[10][10], mat2[10][10], result[10][10];
int rows, colms;

printf("Enter the number of rows and columns: ");


scanf("%d %d", &rows, &colms);

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


for (int i = 0; i < rows; i++) {
for (int j = 0; j < colms; j++) {
scanf("%d", &mat1[i][j]);
}
}

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


for (int i = 0; i < rows; i++) {
for (int j = 0; j < colms; j++) {
scanf("%d", &mat2[i][j]);
}
}

addMatrices(mat1, mat2, result, rows, colms);

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


displayMatrix(result, 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;
};

struct Complex addComplex(struct Complex num1, struct Complex num2) {


struct Complex result;
result.real = num1.real + num2.real;
result.imaginary = num1.imaginary + num2.imaginary;
return result;
}

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);

sum = addComplex(num1, num2);

printf("Sum: %.2f + %.2fi\n", sum.real, sum.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, "");
}

StudentClass(char n[], int i, int s, float c) {


strcpy(name, n);
id = i;
semester = s;
cgpa = c;
maxCgpa[s - 1] = (c > maxCgpa[s - 1]) ? c : maxCgpa[s - 1];
}

void getDetails() {
printf("Name: %s\nID: %d\nSemester: %d\nCGPA: %f\n", name, id, semester, cgpa);
}

void setDetails(char n[], int i, int s, float c) {


strcpy(name, n);
id = i;
semester = s;
cgpa = c;
maxCgpa[s - 1] = (c > maxCgpa[s - 1]) ? c : maxCgpa[s - 1];
}
};

float StudentClass::maxCgpa[10] = {0};

int main() {

struct StudentClass students[10];

for (int i = 0; i < 10; i++) {


char name[50];
int id, semester;
float cgpa;

printf("\nEnter details for student %d:\n", i + 1);


printf("Name: ");
scanf("%s", name);
printf("ID: ");
scanf("%d", &id);
printf("Semester: ");
scanf("%d", &semester);
printf("CGPA: ");
scanf("%f", &cgpa);

students[i] = StudentClass(name, id, semester, cgpa);


}

printf("\nDetails of all students:\n");


for (int i = 0; i < 10; i++) {
printf("\nDetails for student %d:\n", i + 1);
students[i].getDetails();
}

printf("\nMaximum CGPA for each semester:\n");


for (int i = 0; i < 2; i++) {
printf("Semester %d: %f\n", i + 1, StudentClass::maxCgpa[i]);
}

return 0;
}
Output

You might also like