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

Lab-Programs-Session-2

The document contains multiple C programs demonstrating various programming concepts, including finding the largest number among three, calculating simple interest, checking if a character is an alphabet, and performing matrix multiplication. It also includes programs for string operations, calculating student averages, and determining prime numbers. Each program is accompanied by sample outputs and explanations of their functionality.

Uploaded by

anushak8404
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Lab-Programs-Session-2

The document contains multiple C programs demonstrating various programming concepts, including finding the largest number among three, calculating simple interest, checking if a character is an alphabet, and performing matrix multiplication. It also includes programs for string operations, calculating student averages, and determining prime numbers. Each program is accompanied by sample outputs and explanations of their functionality.

Uploaded by

anushak8404
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 34

15.

// C Program to Find the Largest Number Among Three using


// Temporary Variable

#include <stdio.h>
int main()
{
int a = 10, b = 22, c = 9;

int max = a;

if (max < b)
max = b;

if (max < c)
max = c;

printf("%d is the largest number.", max);


return 0;
}
OUTPUT:
The numbers A, B and C are: 10, 22, 9
Maximum among 10, 22, and 9 is: 22
16. C program to find the maximum number out of the three given numbers
using if-else statement
#include <stdio.h>

int main() {
int a = 11, b = 2, c = 9;

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


printf("%d is the largest number.", a);

else if (b >= a && b >= c)


printf("%d is the largest number.", b);

else
printf("%d is the largest number.", c);

return 0;
}

OUTPUT:
The numbers A, B and C are: 10, 22, 9
22 is the largest number.
17. C program to find the largest number among three number using nested if-else

#include <stdio.h>
int main()
{
int c = 10, b = 22, a = 9;

// Finding largest by comparing using relational operators


if (a >= b) {
if (a >= c)
printf("%d is the largest number.", a);
else
printf("%d is the largest number.", c);
}
else {
if (b >= c)
printf("%d is the largest number.", b);
else
printf("%d is the largest number.", c);
}

return 0;
}

OUTPUT :
The numbers A, B and C are: 10, 22, 9
22 is the largest number.
18. // C program to find the simple interest

#include <stdio.h>

int main() {

// Input values
float P = 1, R = 1, T = 1;

// Calculate simple interest


float SI = (P * T * R) / 100;

// Print Simple Interest


printf("Simple Interest = %f\n", SI);

return 0;
}

Output
Simple Interest = 0.010000

Assignment:
C program to calculate Compound Interest
C program to demonstrate the area and perimeter of rectangle
Program to find roots of a quadratic
19. C Program to Check Whether a Character is an Alphabet or not

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

printf("Enter a character: ");


scanf("%c", &c);

if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'))
printf("%c is an alphabet.", c);

else
printf("%c is not an alphabet.", c);

return 0;
}

Output

Enter a character: *
* is not an alphabet
1. C program to find mechanical energy of a particle using e = mgh+1/2 mv2.
#include <stdio.h>

int main() {
float mass, height, velocity, g = 9.8, mechanical_energy;

printf("Enter the mass of the particle (kg): ");


scanf("%f", &mass);

printf("Enter the height of the particle above ground (m): ");


scanf("%f", &height);

printf("Enter the velocity of the particle (m/s): ");


scanf("%f", &velocity);

mechanical_energy = (mass * g * height) + (0.5 * mass * velocity * velocity);

printf("The mechanical energy of the particle is: %.2f Joules\n", mechanical_energy);

return 0;
}
2. C program to convert kilometers into meters and centimeters.

#include <stdio.h>

int main()
{
float kilometers, meters, centimeters;

printf("Enter the distance in kilometers: ");


scanf("%f", &kilometers);

meters = kilometers * 1000;


centimeters = kilometers * 100000;

printf("Distance in meters: %.2f m\n", meters);


printf("Distance in centimeters: %.2f cm\n", centimeters);

return 0;
}
3. C program to check the given character is lowercase or uppercase or special character.

#include <stdio.h>

int main()
{
char ch;

printf("Enter a character: ");


scanf("%c", &ch);

if (ch >= 'a' && ch <= 'z') {


printf("The character '%c' is lowercase.\n", ch);
}

else if (ch >= 'A' && ch <= 'Z') {


printf("The character '%c' is uppercase.\n", ch);
}

else {
printf("The character '%c' is a special character.\n", ch);
}

return 0;
}
20. Program to Print Numbers from 1
to 10
#include <stdio.h>

int main() {
for (int i = 1; i <= 10; ++i) {
printf("%d ", i);
}
return 0;
}
21. C Program to Calculate Sum of Natural Numbers
#include <stdio.h>
int main() {
int n, sum = 0;

printf(“Enter a positive integer: “);


scanf(“%d”, &n);

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


sum += i;
}

printf(“Sum of natural numbers from 1 to %d: %d\n”, n,


sum);
return 0;
}
22. Write a program to calculate a student's result based on two examinations, one
sports event, and three activities conducted. The weightage of activities = 30%, sports
= 20%, and examination @ 50%.
#include <stdio.h>
#include <conio.h>
#define ACTIVITIES WEIGHTAGE 30
#define SPORTS _WEIGHTAGE 20
#define EXAMS _WEIGHTAGE SO
#define EXAMS TOTAL 200
#define ACTIVITIES TOTAL 60
#define SPORTS _TOTAL 50
int main() {
int exam_scorel, activities_scorel, sports_score;
int exam_score2, activities _score2, activities _score3;
float exam_total, activities total;
float total_percent, exam_percent, sports_percent, activities_percent;
clrscr();
printf("\n Enter the score obtained in two examinations (out of 100): “);
scanf(“%d %d", &exam_scorel, &exam_score2);
printf("\n Enter the score obtained in Sports events (out of 50): “);
scanf("%d", &sports_ score);
printf("\n Enter the score obtained in three activities (out of 20): ");
scanf("Xd Xd %d", &activities scorel, &activities_score2, &activities_score3);
exam_total = exam_scorel + exam_score2;
activities total = activities _scorel + activities score2 + activities
score3;
exam_percent = (float)exam_total * EXAMS_ WEIGHTAGE /
EXAMS_TOTAL;
sports_percent = (float)sports_score * SPORTS WEIGHTAGE /
SPORTS_TOTAL;
activities percent = (float)activities total * ACTIVITIES _WEIGHTAGE
/ ACTIVITIES_TOTAL;
total_percent = exam_percent + sports. percent + activities
percent;
printf("\n\n * RESULT **********************\n”)
print€("\n Total percent in examintaion : Xf", exam_percent);
printf("\n Total percent in activities : xf", activities_percent);
printf("\n Total percent in sports : Xf", sports_percent);
printf("\n ******************\n”);
printf("\n Total percentage : total_percent);
return 0; }
23.Write a program to convert an integer into the corresponding
floating point number.
#include <stdio.h>
#include <conio.h>
int main()
{
float f_num;
int i_num;
clrscr();
printf("\n Enter any integer: “);
scanf("X%d", &i_num);
f_num = (float) i_num;
printf("\n The floating point variant of Xd is = Xf",
i_num, #_num);
return 0;
}
/*Program to find the factorial of a given number using recursion*/
#include <stdio.h>
int fact(int);
int main()
{
int n;
long int l;
printf("enter the number\n");
scanf("%d",&n);
l=fact(n);
printf("Factorial of %d is %ld",n,l);
return 0;
}
int fact(int x)
{
if(x==1)
{
return 1;
}
else
{
return x*fact(x-1);
}
}
Output
enter the number
10
Factorial of 10 is 3628800
To compute prime numbers between n and m
#include <stdio.h>

int main()
{
int n ,m,f;
printf("enter the first and last limit such that (first limit<last limit\n");
scanf("%d%d",&n,&m);
printf("prime number between %d and %d is\n",n,m);
for (int i=n;i<=m;i++)
{
f=0;
for(int j=2; j<=i/2; j++)
{
if (i%j==0)
{
f=1;
break;
}
}
if(f==0)
printf("%d\t",i);
}

return 0;
}
Output
enter the first and last limit such that (first limit<last limit
13 55
prime number between 13 and 55 is
13 17 19 23 29 31 37 41 43 47 53
program to check given number is palindrome
#include <stdio.h>
int main()
{
int n,r,rev=0,k;
printf("Enter the number\n");
scanf("%d",&n);
k=n;
while(n!=0)
{
r=n%10;
rev=r+rev*10;
n=n/10;
}
printf("Reverse fo %d is %d\n",k, rev);
if (k==rev)
printf("Entered number is palindrome");
else
printf("Entered number is not palindrome");
return 0;
}
OUTPUT
Enter the number
123
Reverse for 123 is 321
Entered number is not palindrome

Enter the number


121
Reverse for 121 is 121
Entered number is palindrome
Program to sort the given set of n numbers using bubble sort
#include <stdio.h>
int main()
{
int n, a[10],i,j,temp;
printf("Enter the value for N\n");
scanf("%d",&n);
printf("Enter the %d elements",n);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
for(i=1;i<=n-1;i++)
{
for(j=0;j<=n-i-1;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
printf("The sorted array is\n");
for(i=0;i<n;i++)
printf("%d\n",a[i]);
return 0;
}
Output
Enter the value for N
7
Enter the 7 elements
3
55
6
88
9
11
9
The sorted array is
3
6
9
9
11
55
88
Matrix multiplication
• Step 1: Check the compatibility of the matrix by checking
that the number of columns in the 1st matrix equals the
number of rows in the 2nd matrix.
• Step 2: Multiply the elements in the first row of the first
matrix with the elements in the first column of the matrix
and find the sum of all the products.
• Then multiply the element in the first row of the first matrix
with the elements of the second column in the second
matrix. Repeat this process till elements of all the positions
are not obtained.
• Step 3: Substitute all the elements obtained in Step 2 in
their respective position to find the required product
matrix.
• Notation for Matrix Multiplication
• We represent a multiplication matrix as the
multiplication of two matrices A and B such that the
order of A is m×p and the order of B is p×n then the
order of the multiplied matrix is m×n. Then
• X = AB
• Where,
• A and B are Given Matrix of Order m × p and p × n
• X is Resulting Matrix of m × n Order
Matrix multiplication
#include <stdio.h>

int main()
{
int a[10][10],b[10][10],c[10][10],i,j,k,n,m,n1,m1;
printf("enter the order of firts matrix\n");
scanf("%d%d",&n,&m);
printf("enter the order of second matrix\n");
scanf("%d%d",&n1,&m1);
if(m==n1)
{
printf("enter the elements of first matrix\n");
for(i=0;i<n;i++)
{
for(j=0;j<m;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("enter the elements of seocnd matrix matrix\n");
for(i=0;i<n1;i++)
{
for(j=0;j<m1;j++)
{
scanf("%d",&b[i][j]);
}
}
for(i=0;i<n;i++)
{
for(j=0;j<m1;j++)
{
c[i][j]=0;
for(k=0;k<m;k++)
{
c[i][j]=c[i][j]+a[i][k]*b[k][j];
}
}
}

printf("Product of two matrices is\n");

for(i=0;i<n;i++)
{
for(j=0;j<m1;j++)
{
printf("%d\t",c[i][j]);
}
printf("\n");
}
}
else
{
printf("Matrix Multiplication is not possible");
}

return 0;
}
• Output
enter the order of firts matrix
2
3
enter the order of second matrix
3
1
enter the elements of first matrix
123
456
enter the elements of seocnd matrix matrix
1
2
3
Product of two matrices is
14
32
/* Assignment 9 String operations*/
#include <string.h>
int main()
{
char a[10],b[10];
int k,k1,t;
printf("Enter the first string\n");
gets(a);
printf("\nenter the second string\n");
gets(b);

k=strlen(a);
k1=strlen(b);

printf("Length of the %s is %d\n", a,k);


printf("Length of the %s is %d\n", b,k1);

t=strcmp(a,b);

if(t==0)
printf("Both strings are equal\n");
else
printf("not equal\n");

strcat(a,b);
printf("Concatenated string is %s\n",a);
return 0;
}
• Output
Enter the first string
becbgl

enter the second string


sss shy
Length of the becbgl is 7
Length of the sss shy is 7
not equal
Concatenated string is becbglsss shy
Assignment-10 Use structures to read, write and compute average- marks of N
students. Also, list the students scoring above and below the average marks.

#include <stdio.h>
struct student
{
char usn[10];
int m1,m2,m3,m4,m5;
float avg;
};
int main()
{
struct student s[10];
int N,i,avg_pass=50;
printf("Enter the number of students\n");
scanf("%d",&N);
for(i=0;i<N;i++)
{
printf("Enter the details of %d studnets\n",i+1);
printf("Enter the usn\n");
scanf("%s",s[i].usn);
printf("Enter the marks obtained in five subjects\n");
scanf("%d%d%d%d%d",&s[i].m1,&s[i].m2,&s[i].m3,&s[i].m4,&s[i].m5);
s[i].avg=(s[i].m1+s[i].m2+s[i].m3+s[i].m4+s[i].m5)/5;
}
printf("Details of students\n");
for(i=0;i<N; i++)
{
printf("Student USN is %s\n",s[i].usn);
printf("Mraks obtained in five subjects\n");

printf("%d\t%d\t%d\t%d\t%d\n",s[i].m1,s[i].m2,s[i].m3,s[i].m4,s[i].
m5);
printf("Average marks is %f\n", s[i].avg);
}
printf("Students with below average\n");
for(i=0;i<N;i++)
{
if(s[i].avg<avg_pass)
{
printf("USN %s\n",s[i].usn);
printf("Average %f\n",s[i].avg);
}
}
printf("Students with Above average\n");
for(i=0;i<N;i++)
{
if(s[i].avg>=avg_pass)
{
printf("USN %s\n",s[i].usn);
printf("Average %f\n",s[i].avg);
}
}

return 0;
}
Output
Enter the number of students
3
Enter the details of 1 studnets
Enter the usn
cs001
Enter the marks obtained in five subjects
34
44
56
78
90
Enter the details of 2 studnets
Enter the usn
cs002
Enter the marks obtained in five subjects
23
33
22
44
55
Enter the details of 3 studnets
Enter the usn
cs003
Enter the marks obtained in five subjects
12
33
22
33
22
Details of students
Student USN is cs001
Mraks obtained in five subjects
34 44 56 78 90
Average marks is 60.000000
Student USN is cs002
Mraks obtained in five subjects
23 33 22 44 55
Average marks is 35.000000
Student USN is cs003
Mraks obtained in five subjects
12 33 22 33 22
Average marks is 24.000000
Students with below average
USN cs002
Average 35.000000
USN cs003
Average 24.000000
Students with Above average
USN cs001
Average 60.000000

You might also like