Lab-Programs-Session-2
Lab-Programs-Session-2
#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;
int main() {
int a = 11, b = 2, c = 9;
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;
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;
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;
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;
return 0;
}
2. C program to convert kilometers into meters and centimeters.
#include <stdio.h>
int main()
{
float kilometers, meters, 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;
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;
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
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];
}
}
}
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);
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
#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