0% found this document useful (0 votes)
7K views38 pages

PPS Lab Programs for B.Tech First Year

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7K views38 pages

PPS Lab Programs for B.Tech First Year

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

GREATER NOIDA INSTITUTE OF TECHNOLOGY (Engg.

Institute)
ग्रेटर नोएडा इंस्टीट्यूट ऑफ टेक्नोलॉजी
DEPARTMENT OF [Link] FIRST
YEAR
SESSION 2024 -2025, FIRST
SEMESTER

LAB RECORD

PROGRAMMING FOR PROBLEM SOLVING


BCS-151

SUBMITTED TO: SUBMITTED BY:

MR. ANUPAM GAUTAM SIR STUDENT NAME: SHAKTI SINGH SHEKHAWAT


S. NO.
ASSISTANT PROFESSOR PROGRAM DATE
STUDENT ID:2400310 SIGNTURE
DEPT. OF CSE SECTION: A7
1. Write a program that accepts the marks of 5
subjects and find the sum and percentage of marks
obtained by the student.
2. Write a program that calculates the Simple Interest
and Compound Interest. The Principal Amount,
Rate of Interest, and Time are entered through the
keyboard.
3. Write a program to calculate the area and
circumference of a Circle.
4. Write a program that accepts the temperature in
Centigrade and converts it into Fahrenheit using
the formula C/5 = (F – 32) / 9.

5. Write a program that swaps the values of two


variables using a third variable.
6. Write a program that checks whether the two
numbers entered by the user are equal or not.
7. Write a program to find the greatest of three
numbers.
8. Write a program that finds whether a given number
is even or odd.
9. Write a program that tells whether a given year is a
leap year or not.
10. 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”
11. 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.
12. Write a program to print the sum of all numbers up
to a given number.
PROGRAMMING FOR PROBLEM SOLVING LAB (BCS-251)

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.

16. Write a program to check whether the entered


number is a prime or not.
17. Write a program to find the sum of digits of the
entered number.
18. Write a program to find the reverse of a number.

19. Write a program to print Armstrong numbers from


1 to 1000.
20. Write a program to convert the binary numbers
into a decimal number and vice versa.
21. Write a program that simply takes elements of the
array from the user and finds the sum of these
elements.
22. Write a program that inputs two arrays and saves
the sum of corresponding elements of these arrays
in a third array and prints them.
23. Write a program to find the minimum and
maximum elements of the array.
24. Write a program to search an element in an array
using Linear Search.
25. Write a program to sort the elements of the array
in ascending order using the Bubble Sort
technique.
26. Write a program to add and multiply two matrices.

27. Write a program that finds the sum of diagonal


elements of a m*n matrix
28. Write a program to implement strlen (), strcat (),
strcpy () using the concept of Functions.
29. Write a program to find factorial of a number using
recursion.
30. Write a program to swap two elements using the
concept of pointers.

PROGRAM NO. 1 DATE:…………………..

NAME: SHAKTI SINGH SHEKHAWAT ROLL NO : A7-29

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%

PROGRAM NO. 2 DATE: …………………………………..

NAME: SHAKTI SINGH SHEKHAWAT ROLL NO: A7-29

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: …………………………………..

NAME:SHAKTI SINGH SHEKHAWAT ROLL NO: A7-29

Write a program to calculate the area and circumference of a Circle.


INPUT
#include <stdio.h>
#include<conio.h>
#define PI 3.14159 // Define the value of PI
int main() {
float radius, area, circumference;
// Accept the radius of the circle from the user
printf("Enter the radius of the circle: ");
scanf("%f", &radius);
// Calculate the area of the circle
area = PI * radius * radius;
// Calculate the circumference of the circle
circumference = 2 * PI * radius;
// Display the results
printf("\nArea of the Circle: %.2f\n", area);
printf("Circumference of the Circle: %.2f\n", circumference);
return 0;
}

. .
OUTPUT
Enter the radius of the circle: 5
Area of the Circle: 78.54
Circumference of the Circle: 31.42
PROGRAM NO. 4 DATE: …………………………………..

NAME:SHAKTI SINGH SHEKHAWAT ROLL NO: A7-29

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", &centigrade);
// 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

Enter temperature in Centigrade: 25


Temperature in Fahrenheit: 77.00
PROGRAM NO. 5 DATE: …………………………………..

NAME: SHAKTI SINGH SHEKHAWAT ROLL NO: A7-29

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

Enter the second value (b): 10

Before swapping:

a=5

b = 10

After swapping:

a = 10
b=5

PROGRAM NO. 6 DATE: …………………………………..

NAME: SHAKTI SINGH SHEKHAWAT ROLL NO: A7-29

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

Enter the first number: 10

Enter the second number: 10

The two numbers are equal.

CASE:2

Enter the first number: 15

Enter the second number: 20

The two numbers are not equal.


PROGRAM NO: 7 DATE: …………………………………..

NAME: SHAKTI SINGH SHEKHAWAT ROLL NO: A7-29

Write a program to find the greatest of three numbers.


INPUT
#include <stdio.h>

#include<conio.h>

int main() {

int num1, num2, num3;

// Accept values for the three numbers from the user

printf("Enter the first number: ");

scanf("%d", &num1);

printf("Enter the second number: ");

scanf("%d", &num2);

printf("Enter the third number: ");

scanf("%d", &num3);

// Determine the greatest number

if (num1 >= num2 && num1 >= num3) {

printf("The greatest number is: %d\n", num1);

} else if (num2 >= num1 && num2 >= num3) {

printf("The greatest number is: %d\n", num2);

} else {

printf("The greatest number is: %d\n", num3);

return 0;

}
. .
OUTPUT
Enter the first number: 10

Enter the second number: 20

Enter the third number: 15

The greatest number is: 20


PROGRAM NO: 8 DATE: …………………………………..

NAME: SHAKTI SINGH SHEKHAWAT ROLL NO: A7-29

Write a program that finds whether a given number is even or odd.


INPUT
#include <stdio.h>
#include<conio.h>
int main() {
int number;
// Accept a number from the user
printf("Enter an integer: ");
scanf("%d", &number);

// Check if the number is even or odd


if (number % 2 == 0) {
printf("%d is an even number.\n", number);
} else {
printf("%d is an odd number.\n", number);
}
return 0;
}

. .
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: …………………………………..

NAME: SHAKTI SINGH SHEKHAWAT ROLL NO: A7-29

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;

// Accept a year from the user

printf("Enter a year: ");

scanf("%d", &year);

// Check if the year is a leap year using the ternary operator

// A year is a leap year if it is divisible by 4 and (not divisible by 100 or divisible by 400)

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

? printf("%d is a leap year.\n", year)

: printf("%d is not a leap year.\n", year);

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: …………………………………..

NAME: SHAKTI SINGH SHEKHAWAT ROLL NO: A7-29

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: …………………………………..

NAME: SHAKTI SINGH SHEKHAWAT ROLL NO: A7-29

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

// Perform the operation based on the operator using switch statement


switch (operator) {
case '+':
result = num1 + num2;
printf("%.2f + %.2f = %.2f\n", num1, num2, result);
break;
case '-':
result = num1 - num2;
printf("%.2f - %.2f = %.2f\n", num1, num2, result);
break;
case '*':
result = num1 * num2;
printf("%.2f * %.2f = %.2f\n", num1, num2, result);
break;
case '/':
if (num2 != 0) {
result = num1 / num2;
printf("%.2f / %.2f = %.2f\n", num1, num2, result);
} else {
printf("Error: Division by zero is not allowed.\n");
}
break;
default:
printf("Error: Invalid operator.\n");
break;
}
return 0;
}

OUTPUT
[Link]

Enter first operand: 5

Enter second operand: 3

Enter an operator (+, -, *, /): +

5.00 + 3.00 = 8.00

[Link]

Enter first operand: 10

Enter second operand: 2

Enter an operator (+, -, *, /): /

10.00 / 2.00 = 5.00

[Link] BY ZERO

Enter first operand: 10

Enter second operand: 0

Enter an operator (+, -, *, /): /

Error: Division by zero is not allowed.

[Link] OPERATOR

Enter first operand: 10

Enter second operand: 5

Enter an operator (+, -, *, /): ^

Error: Invalid operator.


PROGRAM NO: 12 DATE: …………………………………..

NAME: SHAKTI SINGH SHEKHAWAT ROLL NO: A7-29

Write a program to print the sum of all numbers up to a given number.


INPUT
#include <stdio.h>
#include<conio.h>
int main() {
int n;
int sum = 0;
// Accept a positive integer from the user
printf("Enter a positive integer: ");
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 all numbers from 1 to n
for (int i = 1; i <= n; i++) {
sum += i; // Add i to sum
}
// Print the result
printf("The sum of all numbers from 1 to %d is: %d\n", n, sum);
return 0; // Exit the program successfully
}
. .

OUTPUT

Enter a positive integer: 10


The sum of all numbers from 1 to 10 is: 55
Enter a positive integer: 5
The sum of all numbers from 1 to 5 is: 15
PROGRAM NO: 13 DATE: …………………………………..

NAME: SHAKTI SINGH SHEKHAWAT ROLL NO: A7-29

Write a program to find the factorial of a given number.


INPUT
#include <stdio.h>
#include<conio.h>
unsigned long long factorial_iterative(int n) {
if (n < 0) {
printf("Factorial is not defined for negative numbers.\n");
return 0;
}
unsigned long long result = 1;
for (int i = 1; i <= n; i++) {
result *= i;
}
return result;
}
int main() {
int number;
printf("Enter a number to find its factorial: ");
scanf("%d", &number);
printf("The factorial of %d is: %llu\n", number, factorial_iterative(number));
return 0;
}
. .
OUTPUT

Enter a number to find its factorial: 5


The factorial of 5 is: 120
PROGRAM NO: 14 DATE: …………………………………..

NAME: SHAKTI SINGH SHEKHAWAT ROLL NO: A7-29

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: …………………………………..

NAME: SHAKTI SINGH SHEKHAWAT ROLL NO: A7-29

Write a program to print the Fibonacci series.


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

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

Fibonacci Series: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34,


PROGRAM NO. 16 DATE: …………………………………..

NAME: SHAKTI SINGH SHEKHAWAT ROLL NO: A7-29

Write a program to check whether the entered number is a prime or not.


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

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: …………………………………..

NAME: SHAKTI SINGH SHEKHAWAT ROLL NO: A7-29

Write a program to find the sum of digits of the entered number.


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

int main() {
int num, sum = 0;

// Accept an integer from the user


printf("Enter an integer: ");
scanf("%d", &num);

// Use the absolute value to handle negative numbers


num = (num < 0) ? -num : num;

// Calculate the sum of digits


while (num > 0) {
sum += num % 10; // Add the last digit to sum
num /= 10; // Remove the last digit
}

// Output the result


printf("The sum of the digits is: %d\n", sum);

return 0; // Exit the program successfully


}
. .
OUTPUT
Enter an integer: 12345

The sum of the digits is: 15

Enter an integer: -678

The sum of the digits is: 21


PROGRAM NO. 18 DATE: …………………………………..

NAME: SHAKTI SINGH SHEKHAWAT ROLL NO: A7-29

Write a program to find the reverse of a number. ROLL


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

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

The reverse of the number is: 54321

Enter an integer: -678

The reverse of the number is: -876


PROGRAM NO. 19 DATE: …………………………………..

NAME: SHAKTI SINGH SHEKHAWAT ROLL NO: A7-29

Write a program to print Armstrong numbers from 1 to 1000.


INPUT
#include <stdio.h>
#include<conio.h>
#include <math.h>

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
}
}

printf("\n"); // New line after printing all Armstrong numbers


return 0; // Exit the program successfully
}
. .
OUTPUT
Armstrong numbers between 1 and 1000 are:

1 2 3 4 5 6 7 8 9 153 370 371 407


PROGRAM NO. 20 DATE: …………………………………..

NAME: SHAKTI SINGH SHEKHAWAT ROLL NO: A7-29

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>

void decimalToBinary(int decimal) {


if (decimal == 0) {
printf("Binary: 0\n");
return;
}
int binary[32]; // Array to store binary digits
int index = 0;
// Convert decimal to binary
while (decimal > 0) {
binary[index] = decimal % 2; // Store remainder (binary digit)
decimal /= 2; // Divide by 2
index++;
}
// Print binary in reverse order
printf("Binary: ");
for (int i = index - 1; i >= 0; i--) {
printf("%d", binary[i]);
}
printf("\n");
}
int binaryToDecimal(const char* binary) {
int decimal = 0;
int length = strlen(binary);
// Convert binary string to decimal
for (int i = 0; i < length; i++) {
if (binary[length - 1 - i] == '1') {
decimal += pow(2, i);
}
}
return decimal;
}
int main() {
int choice, decimal;
char binary[32];
while (1) {
printf("Menu:\n");
printf("1. Convert Decimal to Binary\n");
printf("2. Convert Binary to Decimal\n");
printf("3. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter a decimal number: ");
scanf("%d", &decimal);
decimalToBinary(decimal);
break;
case 2:
printf("Enter a binary number: ");
scanf("%s", binary);
decimal = binaryToDecimal(binary);
printf("Decimal: %d\n", decimal);
break;
case 3:
printf("Exiting...\n");
return 0;

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: …………………………………..

NAME: SHAKTI SINGH SHEKHAWAT ROLL NO: A7-29

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: …………………………………..

NAME: SHAKTI SINGH SHEKHAWAT ROLL NO: A7-29

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

PROGRAM NO. 23 DATE: …………………………

NAME: SHAKTI SINGH SHEKHAWAT ROLL NO: A7-29

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

PROGRAM NO. 24 DATE: …………………………

NAME: SHAKTI SINGH SHEKHAWAT ROLL NO: A7-29

Write a program to search an element in an array using Linear Search.


INPUT
#include <stdio.h>
#include<conio.h>
int main() {
int n, i, target, found = 0;
// 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]);
}
// Prompt the user to enter the target element to search for
printf("Enter the element to search for: ");
scanf("%d", &target);
// Perform linear search
for (i = 0; i < n; i++) {
if (arr[i] == target) {
found = 1; // Element found
break; // Exit the loop }
}
// Display the result
if (found) {
printf("Element %d found at index %d.\n", target, i);
} else {
printf("Element %d not found in the array.\n", target);
}
return 0;
}
. .
OUTPUT
Enter the number of elements in the array: 4
Enter 4 elements:
10
20
30
50
Enter the element to search for: 30
Element 30 found at index 2.

PROGRAM NO. 25 DATE: …………………………

NAME: SHAKTI SINGH SHEKHAWAT ROLL NO: A7-29

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

// Last i elements are already sorted, no need to check them


for (j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
// Swap arr[j] and arr[j + 1]
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
swapped = 1; // Set swapped flag to indicate a swap occurred
}
}
// If no two elements were swapped, the array is already sorted
if (swapped == 0) {
break;
}
}
}
int main() {
int n, i;

// 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]);
}

// Call the bubble sort function


bubbleSort(arr, n);

// Display the sorted array


printf("Sorted array in ascending order:\n");
for (i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");

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: …………………………

NAME: SHAKTI SINGH SHEKHAWAT ROLL NO: A7-29

Write a program to add and multiply two matrices.


INPUT
#include <stdio.h>
#include<conio.h>
#define MAX_SIZE 10 // Define maximum size for matrices

void addMatrices(int first[MAX_SIZE][MAX_SIZE], int second[MAX_SIZE][MAX_SIZE], int result[MAX_SIZE]


[MAX_SIZE], int row, int col) {
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
result[i][j] = first[i][j] + second[i][j];
}
}
}

void multiplyMatrices(int first[MAX_SIZE][MAX_SIZE], int second[MAX_SIZE][MAX_SIZE], int result[MAX_SIZE]


[MAX_SIZE], int row1, int col1, int col2) {
for (int i = 0; i < row1; i++) {
for (int j = 0; j < col2; j++) {
result[i][j] = 0; // Initialize the result cell
for (int k = 0; k < col1; k++) {
result[i][j] += first[i][k] * second[k][j];
}
}
}
}

void printMatrix(int matrix[MAX_SIZE][MAX_SIZE], int row, int col) {


for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
printf("%d ", matrix[i][j]);
}
printf("\n");
}
}

int main() {
int first[MAX_SIZE][MAX_SIZE], second[MAX_SIZE][MAX_SIZE], result[MAX_SIZE][MAX_SIZE];
int row1, col1, row2, col2;

// Input for the first matrix


printf("Enter the number of rows and columns for the first matrix: ");
scanf("%d %d", &row1, &col1);

printf("Enter elements of the first matrix:\n");


for (int i = 0; i < row1; i++) {
for (int j = 0; j < col1; j++) {
scanf("%d", &first[i][j]);
}
}

// Input for the second matrix


printf("Enter the number of rows and columns for the second matrix: ");
scanf("%d %d", &row2, &col2);

printf("Enter elements of the second matrix:\n");


for (int i = 0; i < row2; i++) {
for (int j = 0; j < col2; j++) {
scanf("%d", &second[i][j]);
}
}

// 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

PROGRAM NO. 27 DATE: …………………………

NAME: SHAKTI SINGH SHEKHAWAT ROLL NO: A7-29

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;

// Input the dimensions of the matrix


printf("Enter the number of rows (m) and columns (n) of the matrix: ");
scanf("%d %d", &m, &n);
int matrix[m][n];
// Input the elements of the matrix
printf("Enter the elements of the matrix:\n");
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
scanf("%d", &matrix[i][j]);
}
}
// Calculate the sum of diagonal elements
for (int i = 0; i < m && i < n; i++) {
sum += matrix[i][i]; // Add the diagonal element
}
// Output the result
printf("Sum of diagonal elements: %d\n", sum);
return 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: …………………………

NAME: SHAKTI SINGH SHEKHAWAT ROLL NO: A7-29

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;
}

// Function to concatenate two strings


void my_strcat(char *dest, const char *src) {
// Move to the end of the destination string
while (*dest != '\0') {
dest++;
}
// Append the source string to the destination
while (*src != '\0') {
*dest = *src;
dest++;
src++;
}
*dest = '\0'; // Null-terminate the concatenated string
}

// Function to copy one string to another


void my_strcpy(char *dest, const char *src) {
while (*src != '\0') {
*dest = *src;
dest++;
src++;
}
*dest = '\0'; // Null-terminate the copied string
}

int main() {
char str1[100], str2[100];

// Input for the first string


printf("Enter the first string: ");
fgets(str1, sizeof(str1), stdin);
// Remove the newline character if present
str1[my_strlen(str1) - 1] = '\0';

// Input for the second string


printf("Enter the second string: ");
fgets(str2, sizeof(str2), stdin);
// Remove the newline character if present
str2[my_strlen(str2) - 1] = '\0';

// Calculate and display the length of the first string


int length = my_strlen(str1);
printf("Length of the first string: %d\n", length);

// Concatenate the two strings


my_strcat(str1, str2);
printf("Concatenated string: %s\n", str1);

// Copy the first string to a new string


char str3[100];
my_strcpy(str3, str1);
printf("Copied string: %s\n", str3);

return 0;
}
. .
OUTPUT
Enter the first string: Hello
Enter the second string: World

Length of the first string: 5


Concatenated string: HelloWorld
Copied string: HelloWorld
PROGRAM NO. 29 DATE: …………………………

NAME: SHAKTI SINGH SHEKHAWAT ROLL NO: A7-29

Write a program to find factorial of a number using recursion.


INPUT
#include <stdio.h>
// Function to calculate factorial using recursion
int factorial(int n) {
// Base case: factorial of 0 or 1 is 1
if (n == 0 || n == 1) {
return 1;
}
// Recursive case
return n * factorial(n - 1);
}
int main() {
int number;
// Input a number from the user
printf("Enter a positive integer: ");
scanf("%d", &number);
// Check if the number is negative
if (number < 0) {
printf("Factorial is not defined for negative numbers.\n");
} else {
// Calculate and display the factorial
printf("Factorial of %d is %d\n", number, factorial(number));
}
return 0;
}
. .
OUTPUT
Enter a positive integer: 5
Factorial of 5 is 120
PROGRAM NO. 30 DATE: …………………………

NAME: SHAKTI SINGH SHEKHAWAT ROLL NO: A7-29

Write a program to swap two elements using the concept of pointers.


INPUT
#include <stdio.h>

// Function to swap two integers using pointers


void swap(int *a, int *b) {
int temp; // Temporary variable for swapping
temp = *a; // Store the value pointed to by a in temp
*a = *b; // Assign the value pointed to by b to the location pointed to by a
*b = temp; // Assign the value stored in temp to the location pointed to by b
}

int main() {
int num1, num2;

// Input two integers from the user


printf("Enter the first number: ");
scanf("%d", &num1);
printf("Enter the second number: ");
scanf("%d", &num2);

// Display the values before swapping


printf("Before swapping: num1 = %d, num2 = %d\n", num1, num2);

// Call the swap function


swap(&num1, &num2);

// Display the values after swapping


printf("After swapping: num1 = %d, num2 = %d\n", 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

You might also like