Ps New
Ps New
C programs
1. Write a program to create 2D array of size 3*3 and print the
matrix.
Input:
#include <stdio.h>
int main() {
int matrix[3][3];
// Taking input from the user
printf("Enter 9 elements for the 3x3 matrix:\n");
for (int i = 0; i < 3; i++) {
return 0;
}
1|Page
Sascmabca college
FYBCA SEM-2 Patel Ishan Rakeshbhai
DIV: G ROLL NO: 1637
Output:
Enter 9 elements for the 3x3 matrix:
123
456
789
int main() {
}
}
2|Page
Sascmabca college
FYBCA SEM-2 Patel Ishan Rakeshbhai
DIV: G ROLL NO: 1637
// Multiplying matrices
for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < SIZE; j++) {
for (int k = 0; k < SIZE; k++) {
result[i][j] += A[i][k] * B[k][j];
}
}
}
}
printf("\n");
}
return 0;
}
Output:
Enter elements of Matrix A (3x3):
123
456
789
3|Page
Sascmabca college
FYBCA SEM-2 Patel Ishan Rakeshbhai
DIV: G ROLL NO: 1637
#define SIZE 5
#define MAX_LENGTH 50
int main() {
char names[SIZE][MAX_LENGTH], surnames[SIZE][MAX_LENGTH],
fullNames[SIZE][MAX_LENGTH * 2];
4|Page
Sascmabca college
FYBCA SEM-2 Patel Ishan Rakeshbhai
DIV: G ROLL NO: 1637
return 0;
Output:
Enter 5 first names:
John
Alice
Michael
Emma
David
Enter 5 surnames:
Smith
Johnson
5|Page
Sascmabca college
FYBCA SEM-2 Patel Ishan Rakeshbhai
DIV: G ROLL NO: 1637
Brown
Williams
Jones
Full Names:
John Smith
Alice Johnson
Michael Brown
Emma Williams
David Jones
4. Define a structure item with following fields item code, name and
quantity. Write a program that takes input of structure and display
in proper format.
Input:
#include <stdio.h>
int main() {
struct item product; // Declare a structure variable
6|Page
Sascmabca college
FYBCA SEM-2 Patel Ishan Rakeshbhai
DIV: G ROLL NO: 1637
return 0;
}
Output:
Enter Item Code: 101
Enter Item Name: Wireless Mouse
Enter Quantity: 25
Item Details:
Item Code: 101
Item Name: Wireless Mouse
Quantity: 25
7|Page
Sascmabca college
FYBCA SEM-2 Patel Ishan Rakeshbhai
DIV: G ROLL NO: 1637
#include <string.h>
#define SIZE 5
int main() {
struct book books [SIZE]; // Array of structures to store 5 books
float total_amount[SIZE];
float max_amount = 0;
int max_index = 0;
8|Page
Sascmabca college
FYBCA SEM-2 Patel Ishan Rakeshbhai
DIV: G ROLL NO: 1637
scanf("%d", &books[i].qty);
return 0;
}
Output:
9|Page
Sascmabca college
FYBCA SEM-2 Patel Ishan Rakeshbhai
DIV: G ROLL NO: 1637
Book 1:
Book 2:
Enter book ID: 102
Enter book name: Data Structures
Enter book price: 400.75
Enter quantity: 5
Book 3:
Enter book ID: 103
Enter book name: Operating Systems
Enter book price: 300.25
Enter quantity: 2
Book 4:
Enter book ID: 104
Enter book name: Computer Networks
Enter book price: 450.00
Enter quantity: 4
Book 5:
Enter book ID: 105
Enter book name: Algorithms
Enter book price: 500.90
Enter quantity: 6
10 | P a g e
Sascmabca college
FYBCA SEM-2 Patel Ishan Rakeshbhai
DIV: G ROLL NO: 1637
----------------------------------------------------
101 | C Programming | 350.50 | 3 | 1051.50
102 | Data Structures | 400.75 | 5 | 2003.75
103 | Operating Systems | 300.25 | 2 | 600.50
104 | Computer Networks | 450.00 | 4 | 1800.00
105 | Algorithms | 500.90 | 6 | 3005.40
----------------------------------------------------
11 | P a g e
Sascmabca college
FYBCA SEM-2 Patel Ishan Rakeshbhai
DIV: G ROLL NO: 1637
originalNum = num;
originalNum = num;
originalNum /= 10;
}
12 | P a g e
Sascmabca college
FYBCA SEM-2 Patel Ishan Rakeshbhai
DIV: G ROLL NO: 1637
int main() {
int number;
return 0;
}
Output:
Enter a number: 9474
13 | P a g e
Sascmabca college
FYBCA SEM-2 Patel Ishan Rakeshbhai
DIV: G ROLL NO: 1637
else
return n * factorial(n - 1); // Recursive call
}
int main() {
int num;
// Input number from user
printf("Enter a number: ");
scanf("%d", &num);
// Check for negative numbers
if (num < 0)
printf("Factorial of a negative number is not defined.\n");
else
printf("Factorial of %d is %lld\n", num, factorial(num));
return 0;
}
Output:
Test 1: Positive Number
Enter a number: 5
Factorial of 5 is 120
Factorial of 5 is 120
14 | P a g e
Sascmabca college
FYBCA SEM-2 Patel Ishan Rakeshbhai
DIV: G ROLL NO: 1637
Input:
#include <stdio.h>
printf("%d\t", matrix[i][j]);
}
printf("\n");
}
}
15 | P a g e
Sascmabca college
FYBCA SEM-2 Patel Ishan Rakeshbhai
DIV: G ROLL NO: 1637
}
}
}
int main() {
int A[SIZE][SIZE], B[SIZE][SIZE], result[SIZE][SIZE];
int choice;
// Menu-driven operations
16 | P a g e
Sascmabca college
FYBCA SEM-2 Patel Ishan Rakeshbhai
DIV: G ROLL NO: 1637
do {
printf("\nChoose a Matrix Operation:\n");
printf("1. Addition\n");
printf("2. Subtraction\n");
printf("3. Multiplication\n");
printf("4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
addition(A, B, result);
printf("\nResultant Matrix after Addition:\n");
displayMatrix(result);
break;
case 2:
subtraction(A, B, result);
printf("\nResultant Matrix after Subtraction:\n");
displayMatrix(result);
break;
case 3:
multiplication(A, B, result);
printf("\nResultant Matrix after Multiplication:\n");
displayMatrix(result);
break;
case 4:
printf("Exiting program...\n");
break;
17 | P a g e
Sascmabca college
FYBCA SEM-2 Patel Ishan Rakeshbhai
DIV: G ROLL NO: 1637
default:
printf("Invalid choice! Please try again.\n");
}
} while (choice != 4);
return 0;
}
Output:
Enter elements for Matrix A (3x3):
123
456
789
10 10 10
10 10 10
10 10 10
18 | P a g e
Sascmabca college
FYBCA SEM-2 Patel Ishan Rakeshbhai
DIV: G ROLL NO: 1637
Python
9. Write a python program to check given number is even or odd
using user define function.
Input:
else:
print("{0} is Odd".format(num))
Output 1:
Enter a number: 43
43 is Odd
Output 2:
Enter a number: 18
18 is Even
19 | P a g e
Sascmabca college
FYBCA SEM-2 Patel Ishan Rakeshbhai
DIV: G ROLL NO: 1637
Input:
# Program to check if a number is prime or not
num = 29
if num == 0 or num == 1:
print(num, "is not a prime number")
elif num > 1:
# check for factors
for i in range(2, num):
if (num % i) == 0:
# If factor is found, set flag to True
flag = True
Output:
29 is a prime number
11. Write a program to convert a ASCII value to a char. & char. to ASCII value.
20 | P a g e
Sascmabca college
FYBCA SEM-2 Patel Ishan Rakeshbhai
DIV: G ROLL NO: 1637
Input:
# Python program to print
# ASCII Value of Character
c = 'g'
# print the ASCII value of assigned character in c
print("The ASCII value of '" + c + "' is", ord(c))
Output:
("The ASCII value of 'g' is", 103)
num = 16
if num < 0:
print("Enter a positive number")
else:
sum = 0
# Use while loop to iterate until zero
while(num > 0):
sum += num
num -= 1
print("The sum is", sum)
Output:
The sum is 136
21 | P a g e
Sascmabca college
FYBCA SEM-2 Patel Ishan Rakeshbhai
DIV: G ROLL NO: 1637
[2, 4, 6, 8, 10]
words.sort()
22 | P a g e
Sascmabca college
FYBCA SEM-2 Patel Ishan Rakeshbhai
DIV: G ROLL NO: 1637
print(word)
Output:
The sorted words are:
an
cased
example
hello
is
letters
this
with
15. Write a python script to read CSV file and display first 3 records
and last 3 records from data.
Input:
# Python program to read CSV file without header
# Open file
23 | P a g e
Sascmabca college
FYBCA SEM-2 Patel Ishan Rakeshbhai
DIV: G ROLL NO: 1637
Output:
['1', 'Akhil', '4']
['2', 'Babu', '3']
['3', 'Nikhil', '5']
16. Write a python script to read CSV file of Employee data with
field (eno, ename, salary). Perform the following operations:
1. Retrieve the maximum and minimum salary.
2. Display range of rows from [2:5]
3. Display all rows in reverse order.
4.Display all Odd number rows.
Input:
import pandas as pd
df = pd.read_csv(file_path)
24 | P a g e
Sascmabca college
FYBCA SEM-2 Patel Ishan Rakeshbhai
DIV: G ROLL NO: 1637
print(df.iloc[::-1])
# 4. Display all Odd number rows (assuming 1-based indexing for human readability)
print("Odd number rows:")
print(df.iloc[::2])
Output:
eno,ename,salary
101,John,50000
102,Alice,60000
103,Bob,55000
104,Eve,65000
105,Charlie,70000
106,David,72000
25 | P a g e
Sascmabca college
FYBCA SEM-2 Patel Ishan Rakeshbhai
DIV: G ROLL NO: 1637
26 | P a g e
Sascmabca college