Download as pdf or txt
Download as pdf or txt
You are on page 1of 32

Parth Sunil Chavan

20BCI0055
CSE2010
Slot L25 + L26
Lab Assessment 1

1) Write a Program to calculate and display the volume of a CUBE having its
height (h=10cm), width (w=12cm) and depth (8cm).
C code:
# include<stdio.h>
# include<stdlib.h>

int main()
{
int height=10,width=12,depth=8,volume;
volume=height*width*depth;
printf("%d",volume);
return 0;

Output:
2) Write a program to take input of name, rollno and marks obtained by a
student in 4 subjects of 100 marks each and display the name, rollno with
percentage score secured
C code:
# include<stdio.h>
# include<stdlib.h>

int main()
{
char name[20];
float m1,m2,m3,m4,p;
int rollno;
scanf("%s %f %f %f %f %d",name,&m1,&m2,&m3,&m4,&rollno);
p=((m1+m2+m3+m4)*100)/400;
printf("Name:%s\nRoll No:%d\nPercentage:%.2f",name,rollno,p);
return 0;

Output:
3) Write a program to find the largest of three numbers using ternary
operators.
C code:
# include<stdio.h>
# include<stdlib.h>

int main()
{
int a=5,b=2,c=9;
int large;
large=a>b?(a>c?a:c):(b>c?b:c);
printf("%d",large);
return 0;

Output:

4) Write a program to find the roots of quadratic equation.


C code:

# include<stdio.h>
# include<stdlib.h>
#include<math.h>
int main()
{
// ax^2 + bx + c=0
float a,b,c,r1,r2;
a=3,b=5,c=1;
if((b*b-4*a*c)>=0)
{
r1 = (-b + sqrt(b*b-4*a*c)) / (2 * a);
r2 = (-b - sqrt(b*b-4*a*c)) / (2 * a);
printf("root1 = %.2f and root2 = %.2f",r1,r2);
}
else{
printf("Roots are imaginary");
}

return 0;

Output:

5) Write a Program to Check Whether a Number is Prime or not.


C code:
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
int main()
{
int n,flag=0;
printf("Enter Number: ");
scanf("%d",&n);
if(n==1 || n==0)
{
flag=0;
}
else if(n>=2)
{
for(int i=2;i<n/2;i++)
{
if(n%i!=0)
{ flag=1;}

else
{ flag=0;
break;
}
}
}
else{
printf("Negative number");
}
if(flag==0)
{printf("Non prime number");}
else
{printf("Prime number");}
return 0;

Output:

6) Write a program to find the largest and smallest among three entered
numbers and also display whether the identified largest/smallest number is
even or odd.
C code:
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
int main()
{
int a=5,b=2,c=9;
int large,small;
large=a>b?(a>c?a:c):(b>c?b:c);
small=a<b?(a<c?a:c):(b<c?b:c);
printf("Largest Number: %d Smallest Number: %d\n",large,small);
if(large%2==0)
printf("Largest no. is Even\n");
else
printf("Largest no. is Odd\n");
if(small%2==0)
printf("Smallest no. is Even\n");
else
printf("Smallest no. is Odd\n");

return 0;

Output:

7) Write a program to compute grade of students using if else adder. The


grades are assigned as followed:
a. Marks Grade
b. marks<50 F
c. 50≤marks< 60 C
d. 60≤marks<70 B
e. 70≤marks<80 B+
f. 80≤marks<90 A
g. 90≤mars≤ 100 A+

C code:
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
int main()
{
int marks;
scanf("%d",&marks);
if(marks>=90 && marks<=100)
printf("A+");
else if(marks>=80 && marks<90)
printf("A");
else if(marks>=70 && marks<80)
printf("B+");
else if(marks>=60 && marks<70)
printf("B");
else if(marks>=50 && marks<60)
printf("C");
else
printf("F");

return 0;

Output:

8) Write a program to check whether the entered year is leap year or not (a
year is leap if it is divisible by 4 and divisible by 100 or 400.)
C code:
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
int main()
{
int year;
printf("Enter a year: ");
scanf("%d", &year);
if (year % 400 == 0) {
printf("%d is a leap year.", year);
}
else if (year % 100 == 0) {
printf("%d is not a leap year.", year);
}
else if (year % 4 == 0) {
printf("%d is a leap year.", year);
}
else {
printf("%d is not a leap year.", year);
}
return 0;
}

Output:

9) Write a program to find the factorial of a number.


C code:
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
int main()
{
int n,factorial=1;
printf("Enter number: ");
scanf("%d",&n);
if(n==0 || n==1)
printf("%d",factorial);
else
{
for(int i=1;i<=n;i++)
{
factorial=factorial*i;
}
printf("%d",factorial);
}

}
Output:

10) Write a program to check number is Armstrong or not


C code:
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
int main()
{
int n,sum=0,a;
printf("Enter number: ");
scanf("%d",&n);
a=n;
if(n==0 || n==1)
printf("Armstrong Number");
else{
while(n!=0)
{
sum=sum+(n%10)*(n%10)*(n%10);
n=n/10;
}
if(sum==a)
printf("Armstrong Number");
else
printf("Not a Armstrong Number");
}

}
Output:

11) Write a program to find whether a character is consonant or vowel using


switch statement.
C code:
#include<stdio.h>
#include<stdlib.h>

int main()
{

char ch;
printf("Enter alphabet: ");
scanf("%c",&ch);
switch(ch)
{
case 'a':
printf("Vowel");
break;
case 'e':
printf("Vowel");
break;
case 'i':
printf("Vowel");
break;
case 'o':
printf("Vowel");
break;
case 'u':
printf("Vowel");
break;
default:
printf("Consonant");
}
return 0;
}

Output:

12) Demonstrate the differences among getch(), getche(), getchar().


Demonstrate the difference between scanf() & gets(), printf() & puts().
C code:
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>

int main()
{

printf("%c\n", getchar());
printf("%c\n", getch());
printf("%c\n", getche());

return 0;
}

#include<stdio.h>
#include<stdlib.h>
#include<conio.h>

int main()
{

char a[20],b[20];
gets(b);
puts(b);
scanf("%s",a);
printf("%s\n",a);

return 0;
}

Output:

13) Write a program to find GCD (greates common divisor or HCF) and LCM
(least common multiple) of two numbers.
C code:
#include<stdio.h>
#include<stdlib.h>

int LCM(int x,int y)


{
int l,s;
l=x>y?x:y;
s=x<y?x:y;
if(x%y==0)
{
return x;
}
else if (y%x==0)
{
return y;
}
else
{
for(int i=l;i<=x*y;i++)
{
if(i%l==0 && i%s==0)
{
return i;
break;
}
}
}

}
int GCD(int m,int n)
{
return((m*n)/LCM(m,n));
}
int main()
{
printf("LCM is: %d\n",LCM(6,15));
printf("GCD is: %d",GCD(6,15));
return 0;
}

Output:
14) Write a program to display Fibonacci series of last term up to 300.
C code:
#include<stdio.h>
#include<stdlib.h>

int main()
{
int a=0,b=1,temp,sum1=0;
printf("%d\t",a);
printf("%d\t",b);
sum1=a+b;
while(sum1<=300)
{
a=b;
b=sum1;
printf("%d\t",sum1);
sum1=a+b;

}
return 0;
}

Output:

15) Write a program to read two matrices of order m* n, perform addition and
multiplication of two matrices and display the resultant matrices in matrix
form.
C code:
#include<stdio.h>
#include<stdlib.h>

int main()
{
int r1,c1,r2,c2;
printf("Enter No. of rows and columns in matrix1: ");
scanf("%d %d",&r1,&c1);
printf("Enter the elements of matrix1:\n");
int m1[r1][c1];
for(int i=0;i<r1;i++)
{
for(int j=0;j<c1;j++)
{
scanf("%d",&m1[i][j]);
}
}
printf("Enter No. of rows and columns in matrix2: ");
scanf("%d %d",&r2,&c2);
printf("Enter the elements of matrix2:\n");
int m2[r2][c2];
for(int i=0;i<r2;i++)
{
for(int j=0;j<c2;j++)
{
scanf("%d",&m2[i][j]);
}
}
printf("Matrix Addition:\n");
for(int i=0;i<r1;i++)
{
for(int j=0;j<c1;j++)
{
printf("%d ",m1[i][j]+m2[i][j]);
}
printf("\n");
}
int mul[r1][c2];
for(int i=0;i<r1;i++)
{
for(int j=0;j<c2;j++)
{
mul[i][j]=0;
for(int k=0;k<c2;k++)
{
mul[i][j]+=m1[i][k]*m2[k][j];
}
}
}
printf("Multiplication of the matrix=\n");
for(int i=0;i<r1;i++)
{
for(int j=0;j<c2;j++)
{
printf("%d ",mul[i][j]);
}
printf("\n");
}
return 0;
}

Output:

16) Write a program to find biggest among three numbers using pointer.
C code:
#include<stdio.h>
#include<stdlib.h>

int main()
{
int *a,*b,*c;
int p=5,q=2,r=9;
a=&p;
b=&q;
c=&r;
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);
}
}

Output:
17) Write a program to find the sum of all the elements of an array using
pointers.
C code:
#include<stdio.h>
#include<stdlib.h>

int main()
{
int arr[5]={2,3,5,1,7};
int *p,sum=0;
p=arr;
for(int i=0;i<5;i++)
{
sum=sum+*(p+i);
}
printf("%d",sum);
return 0;
}

Output:

18) Write a program to swap value of two variables using pointer.


C code:
#include<stdio.h>
#include<stdlib.h>

int main()
{
int a=10,b=20;
int *p,*q,temp;
p=&a;
q=&b;
temp=*p;
*p=*q;
*q=temp;
printf("a=%d b=%d",*p,*q);

return 0;
}

Output:

19) Write a program to read a sentence and count the number of characters
&words in that sentence.
C code:
#include<stdio.h>
#include<stdlib.h>

int main()
{
char s[100];
int i=0,w=0,c=0;
printf("Enter String: ");
gets(s);
while (s[i] !='\0')
{
if (s[i] == ' ')
{
w++;
c++;
}
else
c++;
i++;
}
printf("Number of characters: %d\n", c);
printf("Number of words: %d",w+1);
return 0;
}

Output:

20) Write a program to read a sentence & delete all the white spaces. Replace
all “.” by “:”.
C Code:
#include <stdio.h>
#include <stdlib.h>

int main()
{
char s[100];
gets(s);
int l=sizeof(s)/sizeof(s[0]);
for(int i=0;i< l;i++)
{
if(s[i] == ' ')
{
for(int j=i;j<l;j++)
{
s[j]=s[j+1];
}
l--;
}
}
l=sizeof(s)/sizeof(s[0]);
for(int i=0;i<l;i++)
{
if(s[i]=='.')
{
s[i]=':';
}
}

printf("%s",s);
}

Output:

21) Write a program to copy one string to another string with and without
using string handling function
C code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{
char s1[100],s2[100],s3[100],s4[100];
gets(s1);
printf("\n");
int l=sizeof(s1)/sizeof(s1[0]);
for(int i=0;i<l;i++)
{
s2[i]=s1[i];
}

printf("%s\n",s2);
printf("%s\n",s2);

gets(s3);
strcpy(s4,s3);
printf("\n");
printf("%s\n",s3);
printf("%s\n",s4);
}

Output:

22) Write a program to print the following pattern


UN
UNIV
UNIVER
UNIVERSI
UNIVERSITY
UNIVERSI
UNIVER
UNIV
UN

C code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{

char s[]={"UNIVERSITY"};
int l=sizeof(s)/sizeof(s[0]);

for(int i=0;i<l;i=i+2)
{
for(int j=0;j<i;j++)
{
printf("%c",s[j]);
}
printf("\n");
}
for(int i=l-3;i>=2;i=i-2)
{
for(int j=0;j<i;j++)
{
printf("%c",s[j]);
}
printf("\n");
}
return 0;
}

Output:
23) Write a program to read RollNo, Name, Address, Age & marks in physics, C,
math in 1st semester of three students in B.Tech and display the student details
with average marks achieved.(use structures )

C code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{

int n;
scanf("%d",&n);
while(n!=0)
{
printf("%d",n%10);
n=n/10;
}
return 0;
}

Output:
24) Write a program to reverse a given integer.
C code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int factorial(int n)
{
if(n==1||n==0)
return 1;
else
return n*factorial(n-1);
}
int main()
{
printf("%d",factorial(5));
return 0;
}

Output:

25) Write a program to calculate factorial of a number using recursion.


C Code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct students
{
int rollno;
int age;
char name[30];
char addr[100];
float p_marks,c_marks,m_marks;
};

int main()
{
struct students s[3];
for(int i=0;i<3;i++)
{
scanf("%d",&s[i].rollno);
scanf("%s %s",s[i].name,s[i].addr);
scanf("%d %f %f
%f",&s[i].age,&s[i].p_marks,&s[i].c_marks,&s[i].m_marks);
}
for(int i=0;i<3;i++)
{
printf("\nRoll no %d\n",s[i].rollno);
printf("Name %s\nAddress %s\n",s[i].name,s[i].addr);
printf("Age %d\nAvg. Marks
%.2f\n",s[i].age,(s[i].p_marks+s[i].c_marks+s[i].m_marks)/3);
}
return 0;
}

Output:
26) Write a C program to delete successive repeated characters from a string.
C code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{
char str1[] = "abbbcfffghjjjjj";
int n = strlen(str1);
char p = '\0';
int k = 0;
for (int i = 0;i<n;i++)
{
if (p!=str1[i])
{
str1[k]=str1[i];
k++;
}
p=str1[i];
}
str1[k] = '\0';
printf("%s", str1);

return 0;
}

Output:

27) Write a C program to compute binary addition and subtraction


C Code:
#include<stdio.h>

void BinaryAdd(int b1,int b2)


{
int i=0,r = 0,s[20];
while(b1!=0||b2!=0){
s[i]=(b1%10 + b2%10 + r)%2;
r= (b1%10 + b2%10 + r)/2;
b1=b1/10;
b2=b2/10;
i++;
}
if(r!=0)
s[i++]=r;
--i;
printf("Sum: ");
while(i>=0){
printf("%d",s[i]);
i--;}
}

int main()
{
BinaryAdd(10011,11010);
}

Output:

28) Write a C program to identify Armstrong’s number in a give range.


C Code:

#include <stdio.h>
int main()
{ int r1,r2;
printf("Enter Range: ");
scanf("%d %d",&r1,&r2);
int i=r1;
printf("Armstrong number(s) in the given range are: \n");
while(i<=r2)
{
int sum=0;
int a=i;
while (i!=0)
{
sum=sum+(i%10)*(i%10)*(i%10);
i=i/10;
}
if(sum==a)
{
printf("%d\n",a);
}
i=a;
i++;
}
return 0;
}

Output:

29) Write a C program to add two matrices.

C Code:
# include<stdio.h>
# include<stdlib.h>

int main()
{
int *mat1[3],*mat2[3],*r[3];
for(int i=0;i<3;i++)
{
*(mat1+i)=(int*)malloc(3*sizeof(int));
*(mat2+i)=(int*)malloc(3*sizeof(int));
*(r+i)=(int*)malloc(3*sizeof(int));
}
printf("Matrix1\n");
for(int m=0;m<3;m++)
{
for(int n=0;n<3;n++)
{
scanf("%d",(*(mat1+m)+n));
}
}
printf("Matrix2\n");

for(int m=0;m<3;m++)
{
for(int n=0;n<3;n++)
{
scanf("%d",(*(mat2+m)+n));
}
}
printf("Matrix1+Matrix2\n");
for(int m=0;m<3;m++)
{
for(int n=0;n<3;n++)
{
printf("%d ",*(*(mat1+m)+n)+*(*(mat2+m)+n));
}
printf("\n");
}
return 0;

Output:

You might also like