NAME: PRITHVIJEET SAHA
REGISTRATION NUMBER:
2501020557
Lab Experiments
Lab 1: Basics – Variables, Input/Output, Data Types
1. Read and display your name.
CODE:
#include <stdio.h>
int main()
{
char name[20];
printf("Enter your name:");
scanf("%s",name);
printf("Your name is:%sIn", name);
return 0;
}
OUTPUT:
2. Read name and age, display them.
CODE:
#include <stdio.h>
int main()
{
char name[20];
int age;
printf("Enter name:");
scanf("%s", name);
printf("Enter age:");
scanf("%d", &age);
printf("Name:%s\nAge: %d", name, age);
return 0;
}
OUTPUT:
3. Read two integers and display their sum.
CODE:
#include <stdio.h>
int main()
{
int a, b;
printf("Enter two numbers: ");
scanf("%d %d", &a, &b);
printf("Sum = %d", a + b);
return 0;
}
OUTPUT:
[Link] two numbers and display average.
CODE:
#include <stdio.h>
int main()
{
int a, b;
printf("Enter two numbers: ");
scanf("%d %d", &a, &b);
printf("Average = %d", (a + b)/2);
return 0;
}
OUTPUT:
[Link] length and breadth, find area of
rectangle.
CODE:
#include<stdio.h>
int main()
{
int l, b;
printf("Enter length and breadth: ");
scanf("%d %d", &l, &b);
printf("Area = %d", l*b);
return 0;
}
OUTPUT:
[Link] radius, calculate area and
circumference of circle.
CODE:
#include <stdio.h>
int main()
{
float r;
printf("Enter radius:");
scanf("%f", &r);
printf("Area = %.2f\n", 3.14 *r*r);
printf("Circumference= %f",2*3.14*r);
return 0;
}
OUTPUT:
7. Read principal, rate, time, calculate simple
interest.
CODE:
#include<stdio.h>
int main() {
int P,T;
float R,SI;
printf("Enter principal value, time, rate:");
scanf("%d %d %f",&P,&T,&R);
SI=(P*T*R)/100.0;
printf(" Simple Interest = %f",SI);
return 0;
}
OUTPUT:
Lab 2: Operators & Expressions
1. Read two numbers and perform all
arithmetic operations.
CODE:
#include <stdio.h>
int main()
{
int a, b;
printf("Enter two numbers: ");
scanf("%d %d", &a, &b);
printf("Sum = %d\n", a + b);
printf("Difference = %d\n",a-b);
printf("Product = %d\n",a*b);
printf("Quotient = %d\n",a/b);
printf("Remainder = %d\n",a%b);
return 0;
}
OUTPUT:
2. Read a number and find square and cube.
CODE:
#include <stdio.h>
int main()
{
int a;
printf("Enter a number: ");
scanf("%d", &a);
printf("Squre= %d\n", a*a);
printf("Cube= %d", a*a*a);
return 0;
}
OUTPUT:
3. Calculate total and average of 5 subjects.
CODE:
#include <stdio.h>
int main() {
int m1, m2,m3,m4,m5,total;
float avg;
printf("Enter marks of 5 subjects (out of 100):");
scanf("%d %d %d %d %d", &m1, &m2, &m3, &m4,
&m5);
total=m1+m2+m3+m4+m5;
avg=total/5.0;
printf("Total = %d\nAverage = %.2f",total,avg);
return 0;
}
OUTPUT:
4. Find area of triangle using Heron’s
formula.
CODE:
#include<stdio.h>
#include<math.h>
int main() {
int a, b, c;
float s,area;
printf("Enter value of 3 sides: ");
scanf("%d %d %d", &a, &b, &c);
s=(a+b+c)/2.0;
area=sqrt(s*(s-a)*(s-b)*(s-c));
printf("Area = %.2f", area);
return 0;
}
OUTPUT:
[Link] salary, calculate DA = 10%, HRA =
15%, Gross Salary.
CODE:
#include<stdio.h>
int main() {
int sal,DA,HRA,gross;
printf("Enter Salary: ");
scanf("%d", &sal);
DA=0.1*sal;
HRA=0.15*sal;
gross=sal+DA+HRA;
printf("DA = %d\nHRA = %d\nGross Salary = %d",
DA,HRA,gross);
return 0;
}
OUTPUT:
6. Swap two numbers without using third
variable.
CODE:
#include <stdio.h>
int main(){
int a, b, temp;
printf("Enter two numbers:");
scanf("%d %d", &a, &b);
printf("Before swap: a=%d, b=%d\n", a, b);
temp =a;
a =b;
b= temp;
printf("After swap: a=%d, b=%d\n", a, b);
return 0;
}OUTPUT:
7. Demonstrate relational and
logical operators.
CODE:
#include <stdio.h>
int main() {
int a, b;
printf("Enter two integers(a & b): ");
scanf("%d %d", &a, &b);
printf("Relational Operators:\n");
printf("If a is greater than b: %d\n", a > b);
printf("If a is smaller than b: %d\n", a < b);
printf("If a is equal to b: %d\n", a == b);
printf("if a is not equal to b: %d\n", a != b);
printf("Logical Operators:\n");
printf("If a and b are greater than 0: %d\n", (a > 0) &&
(b > 0));
printf("If either a or b is greater than 0: %d\n", (a > 0)
|| (b > 0));
return 0;
}
OUTPUT:
Lab 3: Decision Making (if, else, switch)
1. Check whether a number is even or odd.
CODE:
#include <stdio.h>
int main(){
int n;
printf("Enter a number:");
scanf("%d", &n);
if(n % 2 == 0)
printf("Even/n");
else
printf("Odd\n");
return 0;
}
OUTPUT:
[Link] greatest of two numbers.
CODE:
#include<stdio.h>
int main(){
int a, b;
printf("Enter two numbers:");
scanf("%d %d", &a, &b);
if(a >b)
printf("Biggest = %d\n", a);
else
printf("Biggest = %d\n", b);
return 0;
}
OUTPUT:
3. Find greatest of three numbers.
CODE:
#include <stdio.h>
int main(){
int a, b, c;
printf("Enter three numbers:");
scanf("%d %d %d", &a, &b, &c);
if(a >= b && a >=c)
printf("Biggest = %d", a);
else if(b>=a && b>=c)
printf("Biggest = %d", b);
else
printf("Biggest = %d", c);
return 0;
}
OUTPUT:
[Link] whether a number is positive,
negative or zero.
CODE:
#include<stdio.h>
int main(){
int n;
printf("Enter a number:");
scanf("%d", &n);
if (n>0)
printf("Positive");
else if (n<0)
printf("Negative");
else if (n==0)
printf("Zero");
else
printf(" Others");
return 0;
}
OUTPUT:
5. Read a year and check whether it is leap
year.
CODE:
#include <stdio.h>
int main() {
int year;
printf("Enter year:");
scanf("%d", &year);
if((year % 400 == 0) || (year % 4 == 0 && year % 100 !=
0))
printf("Leap Year");
else
printf("Not a Leap Year\n");
return 0;
}
OUTPUT:
6. Read marks and print grade.
CODE:
#include <stdio.h>
int main() {
int m;
printf("Enter marks:");
scanf("%d", &m);
if(m >= 90)
printf("Grade:A");
else if(m>= 75)
printf("Grade:B");
else if(m>= 50)
printf("Grade:C");
else if(m>= 35)
printf("Grade:D");
else
printf("Grade: F (Fail)");
return 0;
}
OUTPUT:
7. Check whether a character is vowel or
consonant.
CODE:
#include<stdio.h>
int main(){
char ch;
printf("Enter alphabet:");
scanf("%d", &ch);
if (ch== 'a' || ch== 'e' || ch== 'i' || ch== 'o' || ch== 'u')
printf("Vowel");
else
printf("Consonant");
return 0;
}OUTPUT:
8. Simple calculator using switch case.
CODE:
#include <stdio.h>
int main() {
int a,b,ch;
printf("Arithmetic Operation\n");
printf("1. Add\n");
printf("2. Subtract\n");
printf("3. Multipy\n");
printf("4. Divide\n");
printf("5. Quit\n");
printf("Enter your choice : ");
scanf("%d",&ch);
if (ch==1)
{
printf("Enter 2 numbers : \n");
scanf("%d %d",&a,&b);
printf("Add result : %d\n",a+b);
}
else if (ch==2)
{
printf("Enter 2 numbers : \n");
scanf("%d %d",&a,&b);
printf("Subtract result : %d\n",a-b);
}
else if (ch==3)
{
printf("Enter 2 numbers : \n");
scanf("%d %d",&a,&b);
printf("Multiply result : %d\n",a*b);
}
else if (ch==4)
{
printf("Enter 2 numbers : \n");
scanf("%d %d",&a,&b);
printf("Division result : %d\n",a/b);
}
else if (ch==5)
printf("End \n");
else
printf("Invalid Choice");
return 0;
}
OUTPUT:
Lab 4: Loops (while, do-while, for)
[Link] first n natural numbers.
CODE:
#include <stdio.h>
int main() {
int n,i;
printf("Enter the value of n:");
scanf("%d", &n);
printf("First %d natural numbers:\n", n);
for(i = 1; i <= n; i++) {
printf("%d\n", i);
}
return 0;
}
OUTPUT:
2. Print even numbers up to n.
CODE:
#include <stdio.h>
int main() {
int n, i;
printf("Enter the value of n: ");
scanf("%d", &n);
printf("Even numbers up to %d:\n", n);
for(i = 2; i <= n; i += 2) {
printf("%d\n", i);
}
return 0;
}
OUTPUT:
3. Find sum of first n natural numbers.
CODE:
#include <stdio.h>
int main() {
int n, sum = 0;
printf("Enter n: ");
scanf("%d", &n);
sum = n * (n + 1) / 2;
printf("Sum = %d\n", sum);
return 0;
}
OUTPUT:
4. Find factorial of a number.
CODE:
#include <stdio.h>
int main() {
int n, i;
int fact = 1;
printf("Enter a positive integer: ");
scanf("%d", &n);
for(i = 1; i <= n; i++) {
fact *= i;
}
printf("Factorial of %d = %d\n", n, fact);
return 0;
}
OUTPUT:
5. Reverse a number
CODE:
#include <stdio.h>
int main() {
int num, rev = 0, rem;
printf("Enter an integer: ");
scanf("%d", &num);
while(num != 0) {
rem = num % 10;
rev = rev * 10 + rem;
num /= 10;
}
printf("Reversed number = %d\n", rev);
return 0;
}
OUTPUT:
[Link] whether a number is palindrome.
CODE:
#include <stdio.h>
int main() {
int num, og, rev = 0, rem;
printf("Enter an integer: ");
scanf("%d", &num);
og = num;
while(num != 0) {
rem = num % 10;
rev = rev * 10 + rem;
num /= 10;
}
if(og == rev)
printf("%d is a palindrome.\n", og);
else
printf("%d is not a palindrome.\n", og);
return 0;
}
OUTPUT:
7. Check whether a number is Armstrong
number.
CODE:
#include <stdio.h>
#include <math.h>
int main() {
int num, og, rem, n = 0;
float rslt = 0.0;
printf("Enter an integer: ");
scanf("%d", &num);
og = num;
while (og != 0) {
og/= 10;
n++;
}
og = num;
while (og!= 0) {
rem = og % 10;
rslt+= pow(rem, n);
og /= 10;
}
if ((int)rslt == num)
printf("%d is an Armstrong number.\n", num);
else
printf("%d is not an Armstrong number.\n", num);
return 0;
}
OUTPUT:
8. Generate Fibonacci series up to n terms.
CODE:
#include <stdio.h>
int main() {
int n, i;
int first = 0, second = 1, next;
printf("Enter the number of terms: ");
scanf("%d", &n);
printf("Fibonacci series up to %d terms:\n", n);
for(i = 1; i <= n; i++) {
printf("%d ", first);
next = first + second;
first = second;
second = next;
}
return 0;
}
OUTPUT:
Lab 5: Arrays (1D & 2D)
[Link] n numbers into array and display
them.
CODE:
#include <stdio.h>
int main() {
int n, i;
printf("Enter the number of elements: ");
scanf("%d", &n);
int arr[n];
printf("Enter %d array elements:", n);
for(i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
printf("Elements entered:");
for(i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
return 0;
}
OUTPUT:
2. Find sum of array elements.
CODE:
#include <stdio.h>
int main() {
int n, i,sum=0;
printf("Enter the number of elements: ");
scanf("%d", &n);
int arr[n];
printf("Enter %d array elements:", n);
for(i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
for(i = 0; i < n; i++) {
sum=sum+i;
}
printf("Sum of array elements: %d",sum);
return 0;
}
OUTPUT:
3. Find maximum and minimum in array.
CODE:
#include <stdio.h>
int main() {
int n, i, max ,min;
printf("Enter the number of elements: ");
scanf("%d", &n);
int arr[n];
printf("Enter %d array elements:", n);
for(i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
max = min = arr[0];
for(i = 1; i < n; i++) {
if(arr[i] > max)
max = arr[i];
if(arr[i] < min)
min = arr[i];
}
printf("Maximum = %d\n", max);
printf("Minimum = %d\n", min);
return 0;
}
OUTPUT:
4. Count even and odd numbers in array.
CODE:
#include <stdio.h>
int main() {
int n, i, even = 0, odd = 0;
printf("Enter number of elements: ");
scanf("%d", &n);
int arr[n];
printf("Enter %d array elements:\n", n);
for(i = 0; i < n; i++) {
scanf("%d", &arr[i]);
if(arr[i] % 2 == 0)
even++;
else
odd++;
}
printf("Even numbers = %d\n", even);
printf("Odd numbers = %d\n", odd);
return 0;
}
OUTPUT:
[Link] array in ascending order.
CODE:
#include <stdio.h>
int main() {
int n, i, j, temp;
printf("Enter number of elements: ");
scanf("%d", &n);
int arr[n];
printf("Enter %d array elements:\n", n);
for(i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
for(i = 0; i < n - 1; i++) {
for(j = 0; j < n - i - 1; j++) {
if(arr[j] > arr[j + 1]) {
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
printf("Sorted array in ascending order:\n");
for(i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
return 0;
}
OUTPUT:
6. Matrix multiplication (2D array).
CODE: #include <stdio.h>
int main() {
int a[10][10], b[10][10], prdm[10][10];
int r1, c1, r2, c2, i, j, k;
printf("Enter rows and columns of first matrix: ");
scanf("%d %d", &r1, &c1);
printf("Enter elements of first matrix:\n");
for(i = 0; i < r1; i++) {
for(j = 0; j < c1; j++) {
scanf("%d", &a[i][j]);
}
}
printf("Enter rows and columns of second matrix: ");
scanf("%d %d", &r2, &c2);
printf("Enter elements of second matrix:\n");
for(i = 0; i < r2; i++) {
for(j = 0; j < c2; j++) {
scanf("%d", &b[i][j]);
}
}
if(c1 != r2) {
printf("Matrix multiplication not possible.\n");
return 0;
}
for(i = 0; i < r1; i++) {
for(j = 0; j < c2; j++) {
prdm[i][j] = 0;
}
}
for(i = 0; i < r1; i++) {
for(j = 0; j < c2; j++) {
for(k = 0; k < c1; k++) {
prdm[i][j] += a[i][k] * b[k][j];
}
}
}
printf("Resultant matrix:\n");
for(i = 0; i < r1; i++) {
for(j = 0; j < c2; j++) {
printf("%d ", prdm[i][j]);
}
printf("\n");
}
return 0;
}
OUTPUT:
OUTPUT:
OUTPUT: