0% found this document useful (0 votes)
6 views35 pages

PS Lab Assignment-1

The document contains a series of C programming exercises covering various topics such as variables, input/output, data types, operators, decision making, loops, and arrays. Each exercise includes code snippets and expected output for tasks like reading user input, performing arithmetic operations, calculating areas, and implementing control structures. The exercises are structured in a lab format, providing practical examples for learners to understand fundamental programming concepts.

Uploaded by

elrahul851
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)
6 views35 pages

PS Lab Assignment-1

The document contains a series of C programming exercises covering various topics such as variables, input/output, data types, operators, decision making, loops, and arrays. Each exercise includes code snippets and expected output for tasks like reading user input, performing arithmetic operations, calculating areas, and implementing control structures. The exercises are structured in a lab format, providing practical examples for learners to understand fundamental programming concepts.

Uploaded by

elrahul851
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

First Lab (Variables, Input/Output,

Data Types)
Program 1: Read and Display Name
Code:
#include<stdio.h>
int main()
{
char name[30];
printf("Enter your name: ");
scanf("%s", name);
printf("Your name is: %s\n", name);
return 0;
}
OUTPUT:

Program 2: Read Name and Age


Code:
#include<stdio.h>
int main()
{
char name[30];
int age; printf("Enter your name: ");
scanf("%s", name)
printf("Enter your age: ");
scanf("%d", &age);
printf("Name: %s, Age: %d\n", name, age);
return 0;
}
OUTPUT:

Program 3: Sum of Two Numbers


Code:
#include<stdio.h>
int main()
{
int a, b, sum;
printf("Enter two numbers: ");
scanf("%d %d", &a, &b);
sum = a + b;
printf("Sum = %d\n", sum);
return 0;
}
OUTPUT:
Program 4: Average of Two Numbers

Code:
#include
int main() {
int a, b;
float avg;
printf("Enter two numbers: ");
scanf("%d %d", &a, &b);
avg = (a + b) / 2.0;
printf("Average = %.2f\n", avg);
return 0;
}
OUTPUT:

5. Read Length and Breadth, Find Area of Rectangle

CODE:
#include <stdio.h>

int main() {

float length, breadth, area;

printf("Enter length and breadth of rectangle: ");

scanf("%f %f", &length, &breadth);


area = length * breadth;

printf("\nArea of rectangle = %.2f\n", area);

return 0;

OUTPUT:

6. Read Radius, Calculate Area and Circumference of


Circle
CODE:
#include <stdio.h>

#define PI 3.14159

int main() {

float radius, area, circumference;

printf("Enter radius of circle: ");

scanf("%f", &radius);

area = PI * radius * radius;

circumference = 2 * PI * radius;

printf("\nArea = %.2f", area);

printf("\nCircumference = %.2f\n", circumference);

return 0;

OUTPUT:
[Link] Principal, Rate, Time, Calculate Simple Interest

CODE:

#include <stdio.h>

int main() {

float principal, rate, time, si;

printf("Enter principal, rate, and time: ");

scanf("%f %f %f", &principal, &rate, &time);

si = (principal * rate * time) / 100;

printf("\nSimple Interest = %.2f\n", si);

return 0;

OUTPUT:

Second Lab(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("\nAddition = %d", a + b);

printf("\nSubtraction = %d", a - b);

printf("\nMultiplication = %d", a * b);

if (b != 0)

printf("\nDivision = %.2f", (float)a / b);

else

printf("\nDivision = Not possible (b = 0)");

printf("\nModulus = %d\n", a % b);

return 0;

OUTPUT:

2. Read a Number and Find Square and Cube


CODE:
#include <stdio.h>

int main() {

int num;

printf("Enter a number: ");

scanf("%d", &num);

printf("\nSquare = %d", num * num);

printf("\nCube = %d\n", num * num * num);

return 0;

OUTPUT:

3. Calculate Total and Average of 5 Subjects


CODE:

#include <stdio.h>

int main() {

float s1, s2, s3, s4, s5, total, average;

printf("Enter marks of 5 subjects: ");

scanf("%f %f %f %f %f", &s1, &s2, &s3, &s4, &s5);

total = s1 + s2 + s3 + s4 + s5;

average = total / 5.0;

printf("\nTotal = %.2f", total);

printf("\nAverage = %.2f\n", average);


return 0;

OUTPUT:

4. Find Area of Triangle Using Heron’s Formula


CODE:

#include <stdio.h>

#include <math.h>

int main() {

float a, b, c, s, area;

printf("Enter three sides of triangle: ");

scanf("%f %f %f", &a, &b, &c);

s = (a + b + c) / 2; // semi-perimeter

area = sqrt(s * (s - a) * (s - b) * (s - c));

printf("\nArea of triangle = %.2f\n", area);

return 0;

OUTPUT:
5. Read Salary, Calculate DA = 10%, HRA = 15%, Gross
Salary
CODE:

#include <stdio.h>

int main() {

float salary, da, hra, gross;

printf("Enter basic salary: ");

scanf("%f", &salary);

da = 0.10 * salary;

hra = 0.15 * salary;

gross = salary + da + hra;

printf("\nDA = %.2f", da);

printf("\nHRA = %.2f", hra);

printf("\nGross Salary = %.2f\n", gross);

return 0;

OUTPUT:
6. Swap Two Numbers Without Using Third Variable
CODE:
#include <stdio.h>

int main() {

int a, b;

printf("Enter two numbers: ");

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

printf("\nBefore swapping: a = %d, b = %d", a, b);

a = a + b;

b = a - b;

a = a - b;

printf("\nAfter swapping: 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 numbers: ");

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

printf("\n--- Relational Operators ---\n");

printf("a == b: %d\n", a == b);

printf("a != b: %d\n", a != b);

printf("a > b : %d\n", a > b);

printf("a < b : %d\n", a < b);

printf("a >= b: %d\n", a >= b);

printf("a <= b: %d\n", a <= b);

printf("\n--- Logical Operators ---\n");

printf("(a > 0 && b > 0): %d\n", (a > 0 && b > 0));

printf("(a > 0 || b > 0): %d\n", (a > 0 || b > 0));

printf("!(a > 0): %d\n", !(a > 0));

return 0;

OUTPUT:
Third Lab[Decision Making (if, else,
switch)]

1. Check Whether a Number is Even or Odd


CODE:

#include <stdio.h>

int main() {

int num;

printf("Enter a number: ");

scanf("%d", &num);

if (num % 2 == 0)

printf("%d is Even.\n", num);

else
printf("%d is Odd.\n", num);

return 0;

OUTPUT:

2. Find 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("%d is greater.\n", a);

else if (b > a)

printf("%d is greater.\n", b);

else

printf("Both numbers are equal.\n");

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("%d is the greatest.\n", a);

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

printf("%d is the greatest.\n", b);

else

printf("%d is the greatest.\n", c);

return 0;

OUTPUT:

4. Check Whether a Number is Positive, Negative or Zero


CODE:
#include <stdio.h>

int main() {

int num;

printf("Enter a number: ");

scanf("%d", &num);

if (num > 0)

printf("%d is Positive.\n", num);

else if (num < 0)

printf("%d is Negative.\n", num);

else

printf("The number is Zero.\n");

return 0;

OUTPUT:

5. Read a Year and Check Whether It is Leap Year


CODE:
#include <stdio.h>

int main() {

int year;

printf("Enter a year: ");


scanf("%d", &year);

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

printf("%d is a Leap Year.\n", year);

else

printf("%d is not a Leap Year.\n", year);

return 0;

OUTPUT:

6. Read Marks and Print Grade


CODE:
#include <stdio.h>

int main() {

float marks;

printf("Enter marks: ");

scanf("%f", &marks);

if (marks >= 90)

printf("Grade: A+ \n");

else if (marks >= 80)

printf("Grade: A \n");

else if (marks >= 70)

printf("Grade: B \n");

else if (marks >= 60)

printf("Grade: C \n");
else if (marks >= 50)

printf("Grade: D \n");

else

printf("Grade: F \n");

return 0;

OUTPUT:

7. Check Whether a Character is Vowel or Consonant


CODE:
#include <stdio.h>

int main() {

char ch;

printf("Enter a character: ");

scanf(" %c", &ch);

if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u' ||

ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U')

printf("%c is a Vowel.\n", ch);

else

printf("%c is a Consonant.\n", ch);

return 0;

}
OUTPUT:

[Link] Calculator Using Switch Case.


CODE:
#include <stdio.h>

int main() {

float num1, num2, result;

char op;

printf("Enter first number: ");

scanf("%f", &num1);

printf("Enter operator (+, -, *, /): ");

scanf(" %c", &op);

printf("Enter second number: ");

scanf("%f", &num2);

switch(op) {

case '+':

result = num1 + num2;

break;

case '-':

result = num1 - num2;

break;
case '*':

result = num1 * num2;

break;

case '/':

if (num2 != 0)

result = num1 / num2;

else {

printf("Division by zero is not allowed!\n");

return 0;

break;

default:

printf("Invalid operator!\n");

return 0;

printf("Result: %.2f %c %.2f = %.2f\n", num1, op, num2, result);

return 0;

OUTPUT:
Fourth Lab[Loops (while, do-while,
for)]
1. Print First n Natural Numbers
CODE:

#include <stdio.h>

int main() {

int n, i;

printf("Enter n: ");

scanf("%d", &n);

printf("First %d natural numbers:\n", n);


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

printf("%d ", i);

printf("\n");

return 0;

OUTPUT:

2. Print Even Numbers up to n


CODE:
#include <stdio.h>

int main() {

int n, i;

printf("Enter n: ");

scanf("%d", &n);

printf("Even numbers up to %d:\n", n);

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

printf("%d ", i);

printf("\n");

return 0;

}
OUTPUT:

3. Find Sum of First n Natural Numbers


CODE:
#include <stdio.h>

int main() {

int n, i, sum = 0;

printf("Enter n: ");

scanf("%d", &n);

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

sum += i;

printf("Sum of first %d natural numbers = %d\n", n, sum);

return 0;

OUTPUT:

4. Find Factorial of a Number


CODE:
#include <stdio.h>

int main() {
int n, i;

unsigned long long fact = 1;

printf("Enter a number: ");

scanf("%d", &n);

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

fact *= i;

printf("Factorial of %d = %llu\n", n, fact);

return 0;

OUTPUT:

5. Reverse a Number
CODE:
#include <stdio.h>

int main() {

int n, rev = 0, rem, temp;

printf("Enter a number: ");

scanf("%d", &n);

temp = n;

while(temp != 0) {

rem = temp % 10;

rev = rev * 10 + rem;

temp /= 10;
}

printf("Reverse of %d is %d\n", n, rev);

return 0;

OUTPUT:

6. Check Whether a Number is Palindrome


CODE:
#include <stdio.h>

int main() {

int n, rev = 0, rem, temp;

printf("Enter a number: ");

scanf("%d", &n);

temp = n;

while(temp != 0) {

rem = temp % 10;

rev = rev * 10 + rem;

temp /= 10;

if(n == rev)

printf("%d is a palindrome number.\n", n);

else

printf("%d is not a palindrome number.\n", n);

return 0;
}

OUTPUT:

7. Check Whether a Number is Armstrong Number


CODE:
#include <stdio.h>

#include <math.h>

int main() {

int n, temp, sum = 0, rem, digits = 0;

printf("Enter a number: ");

scanf("%d", &n);

temp = n;

while(temp != 0) {

digits++;

temp /= 10;

temp = n;

while(temp != 0) {

rem = temp % 10;

sum += pow(rem, digits);

temp /= 10;

if(sum == n)

printf("%d is an Armstrong number.\n", n);


else

printf("%d is not an Armstrong number.\n", n);

return 0;

OUTPUT:

8. Generate Fibonacci Series up to n Terms


CODE:
#include <stdio.h>

int main() {

int n, i;

unsigned long long t1 = 0, t2 = 1, next;

printf("Enter number of terms: ");

scanf("%d", &n);

printf("Fibonacci series up to %d terms:\n", n);

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

printf("%llu ", t1);

next = t1 + t2;

t1 = t2;

t2 = next;

}
printf("\n");

return 0;

OUTPUT:

Fifth Lab[ Arrays (1D & 2D)]


1. Read n Numbers into Array and Display Them
CODE:
#include <stdio.h>

int main() {

int n, i;

printf("Enter number of elements: ");

scanf("%d", &n);

int arr[n];

printf("Enter %d numbers: ", n);

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

scanf("%d", &arr[i]);

printf("Array elements: ");

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

printf("%d ", arr[i]);

printf("\n");

return 0;
}

OUTPUT:

2. Find Sum of Array Elements


CODE:

#include <stdio.h>

int main() {

int n, i, sum = 0;

printf("Enter number of elements: ");

scanf("%d", &n);

int arr[n];

printf("Enter %d numbers: ", n);

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

scanf("%d", &arr[i]);

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

sum += arr[i];

printf("Sum of array elements = %d\n", sum);

return 0;

OUTPUT:

\
3. Find Maximum and Minimum in Array
CODE:
#include <stdio.h>

int main() {

int n, i;

printf("Enter number of elements: ");

scanf("%d", &n);

int arr[n];

printf("Enter %d numbers: ", n);

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

scanf("%d", &arr[i]);

int max = arr[0], 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 numbers: ", n);

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

scanf("%d", &arr[i]);

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

if(arr[i] % 2 == 0)

even++;

else

odd++;

printf("Even numbers = %d\n", even);

printf("Odd numbers = %d\n", odd);

return 0;

OUTPUT:
5. Sort 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 numbers: ", 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: ");


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

printf("%d ", arr[i]);

printf("\n");

return 0;

OUTPUT:

6. Matrix Multiplication (2D Array)


CODE:
| #include <stdio.h>

int main() {

int r1, c1, r2, c2, i, j, k;

printf("Enter rows and columns of first matrix: ");

scanf("%d %d", &r1, &c1);

printf("Enter rows and columns of second matrix: ");

scanf("%d %d", &r2, &c2);

if(c1 != r2) {

printf("Matrix multiplication not possible!\n");

return 0;

int A[r1][c1], B[r2][c2], C[r1][c2];

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 elements of second matrix:\n");

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

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

scanf("%d", &B[i][j]);

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

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

C[i][j] = 0;

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

for(j = 0; j < c2; j++) {

for(k = 0; k < c1; k++)

C[i][j] += A[i][k] * B[k][j];

printf("Result of matrix multiplication:\n");

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

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

printf("%d ", C[i][j]);

printf("\n");

return 0;

OUTPUT:

You might also like