PPS Lab Programs for B.Tech First Year
PPS Lab Programs for B.Tech First Year
Institute)
ग्रेटर नोएडा इंस्टीट्यूट ऑफ टेक्नोलॉजी
DEPARTMENT OF [Link] FIRST
YEAR
SESSION 2024 -2025, FIRST
SEMESTER
LAB RECORD
INDEX
13. Write a program to find the factorial of a given
number.
14. Write a program to print the sum of even and odd
numbers from 1 to N numbers.
15. Write a program to print the Fibonacci series.
Write a program that accepts the marks of 5 subjects and find the sum and percentage of
marks obtained by the student.
INPUT
#include <stdio.h>
#include<conio.h>
int main() {
float mark1, mark2, mark3, mark4, mark5, total_marks ,percentage; // Accept marks for 5 subjects
printf("Enter the marks for subject 1 (out of 100): ");
scanf("%f", &mark1);
printf("Enter the marks for subject 2 (out of 100): ");
scanf("%f", &mark2);
printf("Enter the marks for subject 3 (out of 100): ");
scanf("%f", &mark3);
printf("Enter the marks for subject 4 (out of 100): ");
scanf("%f", &mark4);
printf("Enter the marks for subject 5 (out of 100): ");
scanf("%f", &mark5); // Calculate the total marks and percentage
// Calculate the total marks and percentage
total_marks = mark1 + mark2 + mark3 + mark4 + mark5;
percentage = (total_marks / 500) * 100; // Since each subject is out of 100 // Display the results
printf("\nTotal Marks Obtained: %.2f out of 500\n", total_marks);
printf("Percentage: %.2f%%\n", percentage);
return 0;
}
. .
OUTPUT
Enter the marks for subject 1 (out of 100): 85
Enter the marks for subject 2 (out of 100): 90
Enter the marks for subject 3 (out of 100): 78
Enter the marks for subject 4 (out of 100): 88
Enter the marks for subject 5 (out of 100): 95
Total Marks Obtained: 436.00 out of 500
Percentage: 87.20%
Write a program that calculates the Simple Interest and Compound Interest. The Principal
Amount, Rate of Interest, and Time are entered through the keyboard.
INPUT
#include <stdio.h>
#include <math.h>
#include<conio.h>
int main() {
float principal, rate, time, simple_interest, compound_interest;
printf("Enter the Principal Amount: "); // Accept Principal Amount
scanf("%f", &principal);
printf("Enter the Rate of Interest (in percentage): "); // Accept Rate of Interest
scanf("%f", &rate);
// Accept Time (in years)
printf("Enter the Time (in years): ");
scanf("%f", &time);
// Calculate Simple Interest
simple_interest = (principal * rate * time) / 100;
compound_interest = principal * pow((1 + rate / 100), time) - principal; // Calculate Compound Interest
printf("\nSimple Interest: %.2f\n", simple_interest); // Display the results
printf("Compound Interest: %.2f\n", compound_interest);
return 0;
}
. .
OUTPUT
Enter the Principal Amount: 1000
Enter the Rate of Interest (in percentage): 5
Enter the Time (in years): 2
Simple Interest: 100.00
Compound Interest: 102.50
PROGRAM NO. 3 DATE: …………………………………..
. .
OUTPUT
Enter the radius of the circle: 5
Area of the Circle: 78.54
Circumference of the Circle: 31.42
PROGRAM NO. 4 DATE: …………………………………..
Write a program that accepts the temperature in Centigrade and converts it into Fahrenheit
using the formula C/5 = (F – 32) / 9.
INPUT
#include <stdio.h>
#include<conio.h>
int main() {
float centigrade, fahrenheit;
// Accept temperature in Centigrade from the user
printf("Enter temperature in Centigrade: ");
scanf("%f", ¢igrade);
// Convert Centigrade to Fahrenheit using the formula
fahrenheit = (centigrade * 9 / 5) + 32;
// Display the result
printf("\nTemperature in Fahrenheit: %.2f\n", fahrenheit);
return 0;
}
. .
OUTPUT
Write a program that swaps the values of two variables using a third variable.
INPUT
#include <stdio.h>
#include<conio.h>
int main() {
int a, b, c; // Accept values for the variables from the user
printf("Enter the first value (a): ");
scanf("%d", &a);
printf("Enter the second value (b): ");
scanf("%d", &b);
printf("\nBefore swapping:\n"); // Display values before swapping
printf("a = %d\n", a);
printf("b = %d\n", b); // Swap the values using a third variable
c = a; // Store the value of a in c
a = b; // Assign the value of b to a
b = c; // Assign the value of c (original a) to b
printf("\nAfter swapping:\n"); // Display values after swapping
printf("a = %d\n", a);
printf("b = %d\n", b);
return 0;
}
. .
OUTPUT
Enter the first value (a): 5
Before swapping:
a=5
b = 10
After swapping:
a = 10
b=5
Write a program that checks whether the two numbers entered by the user are equal or
not.
INPUT
#include <stdio.h>
#include<conio.h>
int main() {
int num1, num2;
// Accept values for the variables from the user
printf("Enter the first number: ");
scanf("%d", &num1);
printf("Enter the second number: ");
scanf("%d", &num2);
// Check if the numbers are equal
if (num1 == num2) {
printf("The two numbers are equal.\n");
} else {
printf("The two numbers are not equal.\n");
}
return 0;
}
. .
OUTPUT
CASE:1
CASE:2
#include<conio.h>
int main() {
scanf("%d", &num1);
scanf("%d", &num2);
scanf("%d", &num3);
} else {
return 0;
}
. .
OUTPUT
Enter the first number: 10
. .
OUTPUT
Enter an integer: 8
8 is an even number.
Enter an integer: 15
15 is an odd number.
Enter an integer: -4
-4 is an even number.
PROGRAM NO: 9 DATE: …………………………………..
Write a program that tells whether a given year is a leap year or not
INPUT
#include <stdio.h>
#include<conio.h>
int main() {
int year;
scanf("%d", &year);
// A year is a leap year if it is divisible by 4 and (not divisible by 100 or divisible by 400)
return 0;
. .
OUTPUT
Enter a year: 2020
2020 is a leap year.
Enter a year: 2019
2019 is not a leap year.
Enter a year: 2000
2000 is a leap year.
PROGRAM NO: 10 DATE: …………………………………..
Write a program that accepts marks of five subjects and finds percentage and prints grades
according to the following criteria:
Between
90-100% ------------------------------- Print “A”
80-90% --------------------------------- Print “B”
60-80% --------------------------------- Print “C”
Below 60% ---------------------------- Print “D”
INPUT
#include <stdio.h>
#input<conio.h>
int main() {
float marks[5], total = 0.0, percentage;
char grade;
// Accept marks for five subjects
printf("Enter marks for five subjects:\n");
for (int i = 0; i < 5; i++) {
printf("Subject %d: ", i + 1);
scanf("%f", &marks[i]);
total += marks[i]; // Sum up the marks
}
percentage = (total / 500) * 100; // Calculate percentage
// Determine grade based on percentage
grade = (percentage >= 90 && percentage <= 100) ? 'A' :
(percentage >= 80 && percentage < 90) ? 'B' :
(percentage >= 60 && percentage < 80) ? 'C' :
(percentage < 60) ? 'D' : 'F'; // 'F' for invalid percentage
// Print percentage and grade
printf("Total Marks: %.2f\n", total);
printf("Percentage: %.2f%%\n", percentage);
printf("Grade: %c\n", grade);
return 0;
}
. .
OUTPUT
Enter marks for five subjects:
Subject 1: 85
Subject 2: 90
Subject 3: 78
Subject 4: 88
Subject 5: 92
Total Marks: 433.00
Percentage: 86.60%
Grade: B
PROGRAM NO: 11 DATE: …………………………………..
Write a program that takes two operands and one operator from the user and performs the
operation and prints the result by using the Switch Statement.
INPUT
#include <stdio.h>
#include<conio.h>
int main() {
float num1, num2, result;
char operator;
// Accept two operands and an operator from the user
printf("Enter first operand: ");
scanf("%f", &num1);
printf("Enter second operand: ");
scanf("%f", &num2);
printf("Enter an operator (+, -, *, /): ");
scanf(" %c", &operator); // Note the space before %c to consume any whitespace
OUTPUT
[Link]
[Link]
[Link] BY ZERO
[Link] OPERATOR
OUTPUT
Write a program to print the sum of even and odd numbers from 1 to N numbers?
INPUT
#include <stdio.h>
#include<conio.h>
int main() {
int N;
int sum_even = 0, sum_odd = 0;
// Accept a positive integer from the user
printf("Enter a positive integer N: ");
scanf("%d", &N);
// Check if the input is a positive integer
if (N < 1) {
printf("Please enter a positive integer greater than 0.\n");
return 1; // Exit the program with an error code}
// Calculate the sum of even and odd numbers from 1 to N
for (int i = 1; i <= N; i++) {
if (i % 2 == 0) {
sum_even += i; // Add to sum_even if i is even
} else {
sum_odd += i; // Add to sum_odd if i is odd
}
}
// Print the results
printf("Sum of even numbers from 1 to %d is: %d\n", N, sum_even);
printf("Sum of odd numbers from 1 to %d is: %d\n", N, sum_odd);
return 0; // Exit the program successfully
}
. .
OUTPUT
Enter a positive integer N: 10
Sum of even numbers from 1 to 10 is: 30
Sum of odd numbers from 1 to 10 is: 25
Enter a positive integer N: 5
Sum of even numbers from 1 to 5 is: 6
Sum of odd numbers from 1 to 5 is: 9
PROGRAM NO. 15 DATE: …………………………………..
int main() {
int n, i;
unsigned long long first = 0, second = 1, next;
// Accept the number of terms from the user
printf("Enter the number of terms in the Fibonacci series: ");
scanf("%d", &n);
// Check if the number of terms is valid
if (n <= 0) {
printf("Please enter a positive integer greater than 0.\n");
return 1; // Exit the program with an error code
}
printf("Fibonacci Series: ");
for (i = 1; i <= n; i++) {
if (i == 1) {
printf("%llu, ", first); // Print the first term
continue;
}
if (i == 2) {
printf("%llu, ", second); // Print the second term
continue;
}
next = first + second; // Calculate the next term
first = second; // Update first to the second term
second = next; // Update second to the next term
printf("%llu, ", next); // Print the next term
}
printf("\n"); // New line after printing the series
return 0; // Exit the program successfully
}
. .
OUTPUT
Enter the number of terms in the Fibonacci series: 10
int main() {
int num, i, isPrime = 1; // Assume the number is prime
// Accept a positive integer from the user
printf("Enter a positive integer: ");
scanf("%d", &num);
// Check if the number is less than 2
if (num < 2) {
printf("%d is not a prime number.\n", num);
return 0; // Exit the program
}
// Check for factors from 2 to the square root of num
for (i = 2; i * i <= num; i++) {
if (num % i == 0) {
isPrime = 0; // Set isPrime to 0 if a factor is found
break; // No need to check further
}
}
// Output the result
if (isPrime) {
printf("%d is a prime number.\n", num);
} else {
printf("%d is not a prime number.\n", num);
}
return 0; // Exit the program successfully
}
. .
OUTPUT
Enter a positive integer: 7
7 is a prime number.
Enter a positive integer: 10
10 is not a prime number.
PROGRAM NO. 17 DATE: …………………………………..
int main() {
int num, sum = 0;
int main() {
int num, reversedNum = 0, remainder;
// Accept an integer from the user
printf("Enter an integer: ");
scanf("%d", &num);
// Handle negative numbers by storing the sign
int sign = (num < 0) ? -1 : 1;
num = (num < 0) ? -num : num; // Work with the absolute value
// Reverse the digits of the number
while (num != 0) {
remainder = num % 10; // Get the last digit
reversedNum = reversedNum * 10 + remainder; // Append it to the reversed number
num /= 10; // Remove the last digit
}
// Restore the sign if the original number was negative
reversedNum *= sign;
// Output the result
printf("The reverse of the number is: %d\n", reversedNum);
return 0; // Exit the program successfully
}
. .
OUTPUT
Enter an integer: 12345
int main() {
int num, originalNum, remainder, result, digits;
printf("Armstrong numbers between 1 and 1000 are:\n");
// Loop through numbers from 1 to 1000
for (num = 1; num <= 1000; num++) {
originalNum = num;
result = 0;
digits = 0;
// Count the number of digits
while (originalNum != 0) {
originalNum /= 10;
digits++;
}
originalNum = num; // Reset originalNum to the current number
// Calculate the sum of the digits raised to the power of 'digits'
while (originalNum != 0) {
remainder = originalNum % 10; // Get the last digit
result += pow(remainder, digits); // Raise to the power of digits and add to result
originalNum /= 10; // Remove the last digit
}
// Check if the sum of the digits raised to the power of 'digits' equals the original number
if (result == num) {
printf("%d ", num); // Print the Armstrong number
}
}
Write a program to convert the binary numbers into a decimal number and vice versa.
INPUT
#include <stdio.h>
#include<conio.h>
#include <math.h>
default:
printf("Invalid choice! Please try again.\n");
}
}
}
. .
OUTPUT
Menu:
1. Convert Decimal to Binary
2. Convert Binary to Decimal
3. Exit
Enter your choice: 1
Enter a decimal number: 10
Binary: 1010
Menu:
1. Convert Decimal to Binary
2. Convert Binary to Decimal
3. Exit
Enter your choice: 2
Enter a binary number: 1010
Decimal: 10
Menu:
1. Convert Decimal to Binary
2. Convert Binary to Decimal
3. Exit
Enter your choice: 3
Exiting...
PROGRAM NO: 21 DATE: …………………………………..
Write a program that simply takes elements of the array from the user and finds the sum of
these elements.
INPUT
#include <stdio.h>
#include<conio.h>
int main() {
int n, sum = 0;
// Ask the user for the number of elements
printf("Enter the number of elements in the array: ");
scanf("%d", &n);
int array[n]; // Declare an array of size n
// Input elements from the user
printf("Enter %d elements:\n", n);
for (int i = 0; i < n; i++) {
printf("Element %d: ", i + 1);
scanf("%d", &array[i]);
sum += array[i]; // Add each element to the sum
}
// Output the sum of the elements
printf("The sum of the elements in the array is: %d\n", sum);
return 0; // Exit the program successfully
}
. .
OUTPUT
Enter the number of elements in the array: 5
Enter 5 elements:
Element 1: 10
Element 2: 20
Element 3: 30
Element 4: 40
Element 5: 50
The sum of the elements in the array is: 150
PROGRAM NO: 22 DATE: …………………………………..
Write a program that inputs two arrays and saves the sum of corresponding elements of
these arrays in a third array and prints them.
INPUT
#include <stdio.h>
#include<conio.h>
int main() {
int n;
// Ask the user for the number of elements
printf("Enter the number of elements in the arrays: ");
scanf("%d", &n);
int array1[n], array2[n], sumArray[n]; // Declare three arrays
// Input elements for the first array
printf("Enter elements for the first array:\n");
for (int i = 0; i < n; i++) {
printf("Element %d: ", i + 1);
scanf("%d", &array1[i]);
}
// Input elements for the second array
printf("Enter elements for the second array:\n");
for (int i = 0; i < n; i++) {
printf("Element %d: ", i + 1);
scanf("%d", &array2[i]);
}
// Calculate the sum of corresponding elements
for (int i = 0; i < n; i++) {
sumArray[i] = array1[i] + array2[i];
} // Print the resulting sum array
printf("The sum of corresponding elements is:\n");
for (int i = 0; i < n; i++) {
printf("Sum of Element %d: %d\n", i + 1, sumArray[i]);
}
return 0; // Exit the program successfully
}
. .
OUTPUT
Enter the number of elements in the arrays: 3
Enter elements for the first array:
Element 1: 5
Element 2: 10
Element 3: 15
Enter elements for the second array:
Element 1: 2
Element 2: 3
Element 3: 4
The sum of corresponding elements is:
Sum of Element 1: 7
Sum of Element 2: 13
Sum of Element 3: 19
Write a program to find the minimum and maximum elements of the array.
INPUT
#include <stdio.h>
#include<conio.h>
int main() {
int n, i;
int min, max;
// Prompt the user to enter the size of the array
printf("Enter the number of elements in the array: ");
scanf("%d", &n);
// Check if the number of elements is valid
if (n <= 0) {
printf("Array size must be a positive integer.\n");
return 1;
}
int arr[n];
// Prompt the user to enter the elements of the array
printf("Enter %d elements:\n", n);
for (i = 0; i < n; i++) {
scanf("%d", &arr[i]);
} // Initialize min and max with the first element of the array
min = arr[0];
max = arr[0];
// Traverse the array to find min and max
for (i = 1; i < n; i++) {
if (arr[i] < min) {
min = arr[i]; }
if (arr[i] > max) {
max = arr[i]; }
}
// Display the results
printf("Minimum element: %d\n", min);
printf("Maximum element: %d\n", max);
return 0;
}
. .
OUTPUT
Enter the number of elements in the array: 5
Enter 5 elements:
10
20
5
40
15
Minimum element: 5
Maximum element: 40
Write a program to sort the elements of the array in ascending order using the Bubble Sort
technique.
INPUT
#include <stdio.h>
#include<conio.h>
void bubbleSort(int arr[], int n) {
int i, j, temp;
int swapped;
// Bubble Sort Algorithm
for (i = 0; i < n - 1; i++) {
swapped = 0; // Initialize swapped flag to check if any swapping occurred
return 0;
}
. .
OUTPUT
Enter the number of elements in the array: 5
Enter 5 elements:
64
34
25
12
22
Sorted array in ascending order:
12 22 25 34 64
PROGRAM NO. 26 DATE: …………………………
int main() {
int first[MAX_SIZE][MAX_SIZE], second[MAX_SIZE][MAX_SIZE], result[MAX_SIZE][MAX_SIZE];
int row1, col1, row2, col2;
// Matrix addition
if (row1 == row2 && col1 == col2) {
addMatrices(first, second, result, row1, col1);
printf("Result of addition:\n");
printMatrix(result, row1, col1);
} else {
printf("Matrices cannot be added due to dimension mismatch.\n");
}
// Matrix multiplication
if (col1 == row2) {
multiplyMatrices(first, second, result, row1, col1, col2);
printf("Result of multiplication:\n");
printMatrix(result, row1, col2);
} else {
printf("Matrices cannot be multiplied due to dimension mismatch.\n");
}
return 0;
}
. .
OUTPUT
Enter the number of rows and columns for the first matrix: 2 2
Enter elements of the first matrix:
12
34
Enter the number of rows and columns for the second matrix: 2 2
Enter elements of the second matrix:
56
78
Result of addition:
68
10 12
Result of multiplication:
19 22
43 50
Write a program that finds the sum of diagonal elements of a m*n matrix
INPUT
#include <stdio.h>
#include<conio.h>
int main() {
int m, n, sum = 0;
. .
OUTPUT
Enter the number of rows (m) and columns (n) of the matrix: 3 3
Enter the elements of the matrix:
123
456
789
Sum of diagonal elements: 15
PROGRAM NO. 28 DATE: …………………………
Write a program to implement strlen (), strcat (), strcpy () using the concept of Functions.
INPUT
#include <stdio.h>
#include<conio.h>
// Function to calculate the length of a string
int my_strlen(const char *str) {
int length = 0;
while (str[length] != '\0') {
length++;
}
return length;
}
int main() {
char str1[100], str2[100];
return 0;
}
. .
OUTPUT
Enter the first string: Hello
Enter the second string: World
int main() {
int num1, num2;
return 0;
}
. .
OUTPUT
Enter the first number: 10
Enter the second number: 20
Before swapping: num1 = 10, num2 = 20
After swapping: num1 = 20, num2 = 10