DIv:- 5 Roll No:- 472
ASSIGNMENT 1 – C PROGRAMMING
Question:-1 Write a program to find maximum from 3*3 matrix.
Answer:-
#include <stdio.h>
int main()
int matrix[3][3], max;
printf("Enter 9 elements for the 3x3 matrix:\n");
for (int i = 0; i < 3; i++){
for (int j = 0; j < 3; j++){
printf("Enter element at position [%d][%d]: ", i + 1, j + 1);
scanf("%d", &matrix[i][j]);
max = matrix[0][0];
for (int i = 0; i < 3; i++){
for (int j = 0; j < 3; j++){
if (matrix[i][j] > max) {
max = matrix[i][j];
printf("The maximum value in the matrix is: %d\n", max);
return 0;
1|Page
DIv:- 5 Roll No:- 472
Question 2:- Write a program to transpose of matrix and store it into 2nd matrix.
Answer:-
#include <stdio.h>
int main()
int matrix[3][3], transpose[3][3];
printf("Enter 9 elements for the 3x3 matrix:\n");
for (int i = 0; i < 3; i++){
for (int j = 0; j < 3; j++){
printf("Enter element at position [%d][%d]: ", i + 1, j + 1);
scanf("%d", &matrix[i][j]);
for (int i = 0; i < 3; i++){
for (int j = 0; j < 3; j++){
transpose[j][i] = matrix[i][j];
printf("The transposed matrix is:\n");
for (int i = 0; i < 3; i++){
for (int j = 0; j < 3; j++){
printf("%d ", transpose[i][j]);
printf("\n");}
return 0;
2|Page
DIv:- 5 Roll No:- 472
Question 3:- Write a program to sort list of cities in 2D char array.
Answer:-
#include <stdio.h>
#include <string.h>
int main()
char cities[5][20], temp[20];
int n = 5;
printf("Enter 5 city names:\n");
for (int i = 0; i < n; i++){
printf("City %d: ", i + 1);
scanf("%s", cities[i]);
for (int i = 0; i < n - 1; i++){
for (int j = i + 1; j < n; j++){
if (strcmp(cities[i], cities[j]) > 0){
strcpy(temp, cities[i]);
strcpy(cities[i], cities[j]);
strcpy(cities[j], temp);
printf("\nSorted list of cities:\n");
for (int i = 0; i < n; i++){
printf("%s\n", cities[i]);
return 0;
3|Page
DIv:- 5 Roll No:- 472
Question 4:- .Create a structure student having data member like rollno, name and array of three
subject marks. Write a program to list the name of students who have failed in any one of the
subjects. (Failing Criteria: Marks < 35)
Answer:-
#include <stdio.h>
#include <string.h>
struct Student
int rollno;
char name[50];
int marks[3];
};
int main()
int n;
printf("Enter number of students: ");
scanf("%d", &n);
struct Student students[n];
for (int i = 0; i < n; i++){
printf("\nEnter details for student %d:\n", i + 1);
printf("Roll No: ");
scanf("%d", &students[i].rollno);
printf("Name: ");
scanf("%s", students[i].name);
printf("Enter marks of 3 subjects: ");
for (int j = 0; j < 3; j++)
scanf("%d", &students[i].marks[j]);
4|Page
DIv:- 5 Roll No:- 472
printf("\nList of students who failed in at least one subject:\n");
for (int i = 0; i < n; i++){
for (int j = 0; j < 3; j++){
if (students[i].marks[j] < 35){
printf("%s (Roll No: %d)\n", students[i].name, students[i].rollno);
break;
}}}
return 0;
Question 5:- Write a recursive function for finding factorial of given number.
Answer:-
#include <stdio.h>
long long factorial(int n) {
if (n == 0 || n == 1) {
return 1;
return n * factorial(n - 1);
int main()
int num;
printf("Enter a number to find its factorial: ");
scanf("%d", &num);
if (num < 0){
printf("Factorial is not defined for negative numbers.\n");
else
{printf("Factorial of %d is %lld\n", num, factorial(num));
5|Page
DIv:- 5 Roll No:- 472
}return 0;
Question 6:- Write menu-driven program that will perform following tasks using UDFs by
passing a string argument to each function. a. Exit | b. Print String | c. Length of
String | d. Copy First String into Second String | e. Copy Second String into First String | f.
Compare Two String | g. Reverse String h. Concat Two String
Answer:-
#include <stdio.h>
#include <string.h>
int main()
char str1[100], str2[100], temp[100];
int choice;
printf("Enter first string: ");
scanf("%s", str1);
printf("Enter second string: ");
scanf("%s", str2);
do{
printf("\nMenu:\n"); printf("1. Print the first String\n");
printf("2. Print the second String\n");
printf("3. Length of the First String\n");
printf("4. Length of the Second String\n");
printf("5. Copy First String into Second String\n");
printf("6. Copy Second String into First String\n");
printf("7. Compare Two Strings\n");
printf("8. Reverse First String\n");
printf("9. Reverse Second String\n");
printf("10. Concatenate Two Strings\n");
6|Page
DIv:- 5 Roll No:- 472
printf("0. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice){
case 1:
printf("String: %s\n", str1);
break;
case 2:
printf("String: %s\n", str2);
break;
case 3:
printf("Length: %lu\n", strlen(str1));
break;
case 4:
printf("Length: %lu\n", strlen(str2));
break;
case 5:
strcpy(str2, str1);
printf("Copied String: %s\n", str2);
break;
case 6:
strcpy(str1, str2);
printf("Copied String: %s\n", str1);
break;
case 7:
printf("Comparison Result: %d\n", strcmp(str1, str2));
break;
case 8:
strcpy(temp, str1);
strrev(temp);
printf("Reversed String: %s\n", temp);
7|Page
DIv:- 5 Roll No:- 472
break;
case 9:
strcpy(temp, str2);
strrev(temp);
printf("Reversed String: %s\n", temp);
break;
case 10:
strcat(str1, str2);
printf("Concatenated String: %s\n", str1);
break;
case 0:
printf("Exiting...\n");
break;
default:
printf("Invalid choice!\n");
}} while (choice != 0);
return 0;
8|Page
DIv:- 5 Roll No:- 472
ASSIGNMENT 2 – PS ASSIGNMENT 1
Question 1:- Write a program to multiply 2 matrix and store it into 3rd matrix.
Answer:-
def matrix_multiply(A, B):
r1, c1 = len(A), len(A[0])
r2, c2 = len(B), len(B[0])
if c1 != r2:
return "Matrix multiplication not possible! Number of columns of first matrix
must be equal to number of rows of second matrix."
C = [[0 for _ in range(c2)] for _ in range(r1)]
for i in range(r1):
for j in range(c2):
for k in range(c1):
C[i][j] += A[i][k] * B[k][j]
return C
def matrix_display(matrix, name):
print(f"{name} matrix:")
for row in matrix:
print(" ".join(map(str, row)))
if __name__ == "__main__":
A = [[1, 2, 3], [4, 5, 6]]
B = [[7, 8], [9, 10], [11, 12]]
result = matrix_multiply(A, B)
if isinstance(result, str):
print(result)
else:
matrix_display(A, "First")
matrix_display(B, "Second")
9|Page
DIv:- 5 Roll No:- 472
matrix_display(result, "Resultant")
Question 2:- Write a program to transpose of matrix and store it into 2nd matrix.
Answer:-
def matrix_transpose(A):
r, c = len(A), len(A[0])
B = [[0 for _ in range(r)] for _ in range(c)]
for i in range(r):
for j in range(c):
B[j][i] = A[i][j]
return B
def matrix_display(matrix, name):
print(f"{name} matrix:")
for row in matrix:
print(" ".join(map(str, row)))
if __name__ == "__main__":
A = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
B = matrix_transpose(A)
matrix_display(A, "Original")
matrix_display(B, "Transposed")
Question 3:- Write a program to find length of a given string.
Answer:-
def string_length(s):
return len(s)
if __name__ == "__main__":
10 | P a g e
DIv:- 5 Roll No:- 472
s = "Hello, World!"
print(f"The length String is: {string_length(s)}")
Question 4:- Write a program to search string from a listed string.
Answer:-
def search_string(string_list, target):
return target in string_list
if __name__ == "__main__":
string_list = ["apple", "banana", "cherry", "date", "elderberry"]
target = "banana"
if search_string(string_list, target):
print(f"'{target}' found in the list.")
else:
print(f"'{target}' not found in the list.")
Question 5:- Write a program to copy 5 strings into another array and print new array.
Answer:-
def copy_strings(original_list):
return original_list[:]
if __name__ == "__main__":
original_list = ["banana", "mango", "cherry", "watermelon", "grapes"]
copied_list = copy_strings(original_list)
print("Original list:", original_list)
print("Copied list:", copied_list)
11 | P a g e
DIv:- 5 Roll No:- 472
ASSIGNMENT 3 – PS ASSIGNMENT 2
Question 1:- .Write a recursive function for finding factorial of given number.
Answer:-
def factorial(n):
if n == 0 or n == 1:
return 1
else:
return n * factorial(n - 1)
if __name__ == "__main__":
num = 15
print(f"Factorial of {num} is: {factorial(num)}")
Question 2:- Write a recursive function for print Fibonacci series
Answer:-
def fibonacci(n):
if n <= 0:
return []
elif n == 1:
return [0]
elif n == 2:
return [0, 1]
else:
series = fibonacci(n - 1)
series.append(series[-1] + series[-2])
return series
if __name__ == "__main__":
12 | P a g e
DIv:- 5 Roll No:- 472
num = 15
print(f"Fibonacci series up to {num} terms: {fibonacci(num)}")
Question 3:- Write menu-driven program that will perform following tasks using UDFs by passing a
string argument to each function.
a. Exit b. Print String
c. Length of String d. Copy First String into Second String
e. Copy Second String into First String f. Compare Two String
g. Reverse String h. Concatenating Two String
Answer:-
def print_string(s):
print(f"String: {s}")
def string_length(s):
return len(s)
def copy_string(s):
return s[:]
def compare_strings(s1, s2):
return s1 == s2
def reverse_string(s):
return s[::-1]
def concatenate_strings(s1, s2):
return s1 + s2
def menu():
s1 = input("Enter first string: ")
13 | P a g e
DIv:- 5 Roll No:- 472
s2 = input("Enter second string: ")
while True:
print("\nMenu:")
print("1. Print the String")
print("2. Find Length of String")
print("3. Copy The First String into Second The String")
print("4. Copy The Second String into The First String")
print("5. Compare Two Strings")
print("6. Reverse The String")
print("7. Concatenate Two Strings")
print("8. Exit")
choice = int(input("Enter your choice: "))
if choice == 1:
print_string(s1)
elif choice == 2:
print(f"Length of string: {string_length(s1)}")
elif choice == 3:
s2 = copy_string(s1)
print("First string copied into second string.")
elif choice == 4:
s1 = copy_string(s2)
print("Second string copied into first string.")
elif choice == 5:
print("Strings are equal" if compare_strings(s1, s2) else "Strings are not equal")
elif choice == 6:
print(f"Reversed string: {reverse_string(s1)}")
elif choice == 7:
print(f"Concatenated string: {concatenate_strings(s1, s2)}")
elif choice == 8:
14 | P a g e
DIv:- 5 Roll No:- 472
break
else:
print("Invalid choice, please try again.")
if __name__ == "__main__":
menu()
Question 9:- Write a program that will pass an array of numbers to the function and returns count of
odd numbers from the function.
Answer:-
def count_odd_numbers(arr):
return sum(1 for num in arr if num % 2 != 0)
if __name__ == "__main__":
numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
print(f"Count of odd numbers: {count_odd_numbers(numbers)}")
15 | P a g e
DIv:- 5 Roll No:- 472
ASSIGNMENT 4 – PS ASSIGNMENT 3
Question 1:- Python program to convert Celsius to Fahrenheit (Note : f= C x 1.8) + 32 )
Answer:-
def celsius_to_fahrenheit(celsius):
return (celsius * 1.8) + 32
if __name__ == "__main__":
celsius = float(input("Enter temperature in Celsius: "))
print(f"Temperature in Fahrenheit: {celsius_to_fahrenheit(celsius)}")
Question 2:- Python Program to Check if a Number is Positive, Negative or Zero.
Answer:-
def check_number(n):
if n > 0:
return "Positive"
elif n < 0:
return "Negative"
else:
return "Zero"
if __name__ == "__main__":
num = float(input("Enter a number: "))
print(f"The number is: {check_number(num)}")
16 | P a g e
DIv:- 5 Roll No:- 472
Question 3:- Python Program to Check if a Number is Odd or Even.
Answer:-
def check_odd_even(n):
if n % 2 == 0:
return "Even"
else:
return "Odd"
if __name__ == "__main__":
num = int(input("Enter a number: "))
print(f"The number is: {check_odd_even(num)}")
17 | P a g e
DIv:- 5 Roll No:- 472
Question 4:- Python Program to Check Leap Year (Formula : (Year % 400 == 0) or (Year % 100 != 0)
and (Year % 4 == 0) )
Answer:-
def is_leap_year(year):
return (year % 400 == 0) or (year % 100 != 0 and year % 4 == 0)
if __name__ == "__main__":
year = int(input("Enter a year: "))
if is_leap_year(year):
print(f"{year} is a Leap Year.")
else:
print(f"{year} is not a Leap Year.")
18 | P a g e