Cs Filnal Practical
Cs Filnal Practical
int main()
Question no 2
#include<stdio.h>
int main()
{
float r, perimeter,
area, volume;
printf("Enter the
radius of the circle/sphere: "); scanf("%f", &r);
perimeter = 2 * PI * r;
area = 3.14 * r * r;
volume = (4.0/3.0) *
3.14 * r * r * r;
printf("Perimeter of
the circle: %.2f\n", perimeter);
printf("Area of the
circle: %.2f\n", area);
printf("Volume of the
sphere: %.2f\n", volume);
return 0;
}
Question no 3
#include<stdio.h>
int main()
{
int num, right_shift,
left_shift;
printf("Enter an
integer: "); scanf("%d", &num);
right_shift = num >> 1;
left_shift = num << 1;
printf("%d divided by 2
using right shift operator: %d\n", num, right_shift);
printf("%d multiplied
by 2 using left shift operator: %d\n", num, left_shift);
return 0;
}
Question no 4
#include <stdio.h>
#include <math.h>
int main()
double a, b, c,
discriminant, root1, root2;
printf("Enter the
coefficients of the quadratic equation (a, b, c): ");
scanf("%lf%lf%lf", &a, &b, &c);
discriminant = b*b -
4*a*c;
if (discriminant > 0) {
root1 = (-b +
sqrt(discriminant)) / (2*a); root2 = (-b - sqrt(discriminant)) /
(2*a);
printf("The roots
are %lf and %lf\n", root1, root2);
} else if (discriminant == 0)
{ root1 = root2 = -b /
(2*a);
printf("The root is
%lf%lf\n", root1,root2);
} else {
printf("The equation
has no real roots\n");
}
return 0;
}
Question no 5
#include <stdio.h>
int main() {
unsigned int x = 0b1010, y = 0b0110;
Question no 6
#include<stdio.h>
#include<math.h>
int main()
{
int num, digits = 0, temp;
printf("Enter an integer: ");
scanf("%d", &num);
temp = num;
while (temp != 0)
{
digits++;
temp /= 10;
}
temp = num;
for (int i = 1; i <= digits; i++)
{
int divisor = pow(10, digits - i);
int quotient = temp / divisor;
int remainder = temp % divisor;
printf("All digits except the last %d digits: %d\n", i,
remainder);
temp = quotient;
}
printf("The last digit: %d\n", temp % 10); return 0;
Question no 7
#include<stdio.h>
int main()
{
int num, sum = 0, remainder;
printf("Enter an integer: "); scanf("%d", &num);
while (num != 0)
{
remainder = num % 10;
sum += remainder;
num /= 10;
}
printf("Sum of the digits: %d\n", sum);
return 0;
}
Question no 8
#include<stdio.h>
#include<math.h>
int main()
{
float investment, interest_rate = 7.75, interest, total_amount;
int years = 10;
printf("Enter the investment amount: ");
scanf("%f", &investment);
interest = investment * interest_rate * years / 100;
total_amount = investment + interest;
printf("Fixed deposit cumulative return after %d years at an
interest rate of %.2f%%: %.2f\n", years, interest_rate,
total_amount);
return 0;
}
Question no 9
#include <stdio.h>
#include <math.h>
int main()
{
double a, b, c, discriminant, root1, root2, realPart,
imaginaryPart;
printf("Enter the coefficients a, b and c: ");
scanf("%lf%lf%lf", &a, &b, &c);
discriminant = b * b - 4 * a * c;
return 0;
}
Question no 10
int main() {
char name[100];
int level, basic_pay;
int perks, tax, gross_salary, net_salary;
return 0;
}
Question no 11
#include <stdio.h>
int main() {
int n, reversed = 0;
while (n != 0) {
reversed = reversed * 10 + n % 10;
n /= 10;
}
printf("Reversed number: %d", reversed);
return 0;
}
Question no 12
#include <stdio.h>
#include <math.h>
int is_prime(int n) {
int i;
if (n <= 1) return 0;
for (i = 2; i <= sqrt(n); i++) { if (n % i == 0) return 0;
}
return 1;
}
int main() {
int low, high, i;
printf("Enter low and high: ");
scanf("%d%d", &low, &high);
return 0;
}
Question no 13
#include <stdio.h>
int main() {
int a, b, result;
return 0;
}
Question no 14
#include <stdio.h>
int main()
{
int n, first = 0, second = 1, next, c;
printf("Enter the number of terms: ");
scanf("%d", &n);
printf("First %d terms of Fibonacci series:\n", n);
c = 0;
do {
if (c <= 1) {
next = c;
}
else {
next = first + second;
first = second;
second = next;
}
printf("%d\n", next);
c++;
} while (c < n);
return 0;
}
Question no 15
#include <stdio.h>
#include <math.h>
double sinx(double x) {
double sin = x, term = x, x_pow = x * x;
int i = 1;
while (fabs(term) >= 0.00001) {
sin += term;
i += 2;
term *= -x_pow / (i * (i - 1));
}
return sin;
}
int main(void) {
double x;
printf("Enter x in radians: ");
scanf("%lf", &x);
printf("sin(%.4lf) = %.4lf\n", x, sinx(x));
return 0;
}
Question no 16
#include <stdio.h>
int main()
{
int i, j, k, n = 4;
for (i = 1; i <= n; i++)
{
for (j = 1; j <= i; j++)
{
printf("%d ", j);
}
for (k = i - 1; k >= 1; k--)
{
printf("%d ", k);
}
printf("\n");
}
return 0;
}
Question no 17
#include <stdio.h>
int main()
{
int i, n = 5, m = 5;
int a[5] = {1, 3, 5, 7, 9};
int b[5] = {2, 4, 6, 8, 10};
int c[10];
merge(a, b, c, n, m);
printf("Sorted Array: ");
for (i = 0; i < n + m; i++)
{
printf("%d ", c[i]);
}
return 0;
}
Question no 18
#include <stdio.h>
#include <string.h>
int main() {
char text[100], c;
int count = 0;
int main()
{
int a[3][3], b[3][3], c[3][3];
printf("Enter elements of first matrix\n");
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
scanf("%d", &a[i][j]);
}
}
printf("Enter elements of second matrix\n");
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
scanf("%d", &b[i][j]);
}
}
add(a, b, c);
printf("Sum of two matrices is\n");
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
printf("%d ", c[i][j]);
}
printf("\n");
}
return 0;
Question no 19(b)
#include <stdio.h>
int main() {
int a[3][3], b[3][3], c[3][3], i, j, k;
printf("Result matrix:\n");
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
printf("%d\t", c[i][j]);
}
printf("\n");
}
return 0;
}
Question 19(C)
#include <stdio.h>
return 0;
}
Question no 20
#include<stdio.h>
#include<ctype.h>
int main(){
char line[100];
int vowels = 0, consonants = 0,
digits = 0, i;
printf("Enter a line of text: ");
fgets(line, 100, stdin);
for(i = 0; line[i] != '\0'; i++){
if(isalpha(line[i])){
if(line[i] == 'A' || line[i] == 'E' || line[i] == 'I' || line[i] == 'O'
|| line[i] == 'U' ||
line[i] == 'a' || line[i] == 'e' || line[i] == 'i' || line[i] == 'o' ||
line[i] == 'u'){
vowels++;
}
else{
consonants++;
}
}
else if(isdigit(line[i])){
digits++;
}
}
printf("Vowels: %d\nConsonants: %d\nDigits: %d\n", vowels,
consonants, digits);
return 0;
}
Question no 21
#include<stdio.h>
#include<string.h>
int main(){
char line[100];
char old_sub[20], new_sub[20];
printf("Enter a line of text: ");
fgets(line, 100, stdin);
printf("Enter old substring: ");
scanf("%s", old_sub);
printf("Enter new substring: ");
scanf("%s", new_sub);
replace_substring(line, old_sub, new_sub);
printf("Modified line: %s\n", line);
return 0;
}
Question no 22
#include <stdio.h>
#include <ctype.h>
printf("Digit counts:\n");
for (int i = 0; i < 10; i++) {
printf("%d: %d\n", i, digit_counts[i]);
}
printf("Letter counts:\n");
for (int i = 0; i < 26; i++) {
printf("%c: %d\n", 'a' + i, letter_counts[i]);
}
return 0;
}
Question no 23
#include <stdio.h>
#include <math.h>
#define MAX_NUMBERS 100
int main()
{
int numbers[MAX_NUMBERS];
int count = 0;
int sum = 0;
float mean = 0.0;
float variance = 0.0;
float standard_deviation = 0.0;
// Input numbers
printf("Enter up to %d positive integers (0 to stop):\n",
MAX_NUMBERS);
while (count < MAX_NUMBERS) {
int n;
scanf("%d", &n);
if (n <= 0) break;
numbers[count++] = n;
sum += n;
}
// Calculate mean
mean = (float)sum / count;
// Calculate variance
for (int i = 0; i < count; i++) {
float diff = numbers[i] - mean;
variance += diff * diff;
}
variance /= count;
// Output results
printf("Sum: %d\n", sum);
printf("Mean: %.2f\n", mean);
return 0;
}
Question no 24
#include <stdio.h>
int main()
{
char c = 'A';
int num = 1;
return 0;
}
Question no 25
#include <stdio.h>
#include <string.h>
char *less_than_twenty[] = {
"", "ONE", "TWO", "THREE", "FOUR", "FIVE", "SIX", "SEVEN",
"EIGHT", "NINE", "TEN",
"ELEVEN", "TWELVE", "THIRTEEN", "FOURTEEN", "FIFTEEN",
"SIXTEEN", "SEVENTEEN", "EIGHTEEN", "NINETEEN"
};
char *tens_multiple[] = {
"", "", "TWENTY", "THIRTY", "FORTY", "FIFTY", "SIXTY",
"SEVENTY", "EIGHTY", "NINETY"
};
char *denom[] = {
"", "THOUSAND", "LAKH", "CRORE"
};
int i = x;
while (i > 0) {
if (i == 4 || i == 7 || i == 10 || i == 13) {
printf("%s ", denom[x - i]);
}
if (i % 2 == 0) {
if (digits[i - 2] > 0) {
printf("%s ", tens_multiple[digits[i - 2]]);
}
if (digits[i - 1] > 0) {
printf("%s ", less_than_twenty[digits[i - 1]]);
}
}
i--;
}
if (len > 0) {
printf("%s ", suffix);
}
}
int main() {
float cost;
int rupees, paise;
printf("Enter the cost of an item in the form RRRR.PP: ");
scanf("%f", &cost);
rupees = (int) cost;
paise = (int) ((cost - rupees) * 100);
print_word(rupees, "RUPEES");
print_word(paise, "PAISE");
return 0;
}
Question no 26
#include <stdio.h>
#define N 10
int main() {
int a[N], *p;
return 0;
}
Question no 27
#include <stdio.h>
int compare_arrays(int *arr1, int *arr2, int size) {
for (int i = 0; i < size; i++) {
if (arr1[i] != arr2[i]) {
return 0;
}
}
return 1;
}
int main() {
int arr1[5] = {1, 2, 3, 4, 5};
int arr2[5] = {1, 2, 3, 4, 5};
int result = compare_arrays(arr1, arr2, 5);
printf("The arrays are %s\n", result ? "identical" : "not
identical");
return 0;
}
Question no 28
#include <stdio.h>
#include <math.h>
int main() {
int num;
printf("Enter an integer: ");
scanf("%d", &num);
checkNumber(num);
return 0;
}
Question no 29
#include <stdio.h>
#include <string.h>
void main()
{
char string[] = "thermometer";
char sub[] = "mom";
printf("%s occurs in %s at index no. %d\n", sub, string,
substring(sub, string));
}
Question no 30
#include <stdio.h>
#include <stdlib.h>
#define MAX_SIZE 10
struct Queue {
int front, rear;
int items[MAX_SIZE];
};
int main() {
struct Queue* q = createQueue();
enqueue(q, 1);
enqueue(q, 2);
enqueue(q, 3);
printf("Dequeued item: %d\n", dequeue(q));
printf("Dequeued item: %d\n", dequeue(q));
return 0;
}
Question no 31
#include <stdio.h>
struct time_struct { int hour;
int minute;
int second;
};
int main() {
struct time_struct time;
return 0;
}
Question no 32
#include <stdio.h>
struct Address
{
int streetNumber;
char city[30], district[30], state[30];
};
struct Employee
{
char Emp_Name[30];
int Emp_Mobile; int Emp_Age;
char Emp_Degree[30];
float Emp_Exp;
struct Address Emp_Add;
};
void PrintEmployeeDetails(struct Employee employee){
printf("Name - %s, Mobile no. - %d, Age - %d, Degree - %s,
Experience
- %f\n", employee.Emp_Name, employee.Emp_Mobile,
employee.Emp_Age, employee.Emp_Degree,
employee.Emp_Exp);
printf("Address: %d, %s, %s, %s",
employee.Emp_Add.streetNumber, employee.Emp_Add.city,
employee.Emp_Add.district, employee.Emp_Add.state);
}
void main()
{
struct Employee employees[20];
int numberOfEmployees = 20, continueToEnter = 1;
for (int i = 0; i < 20; i++){
printf("Enter the employee name: ");
gets(employees[i].Emp_Name);
void main()
{
char str[100];
int i, j, count;
int letter[26] = {0};
int digit[10] = {0};
int k = 0;
char c = fgetc(filePtr);
while (c != EOF){
str[k] = c;
c = fgetc(filePtr);
k++;
}
str[k] = '\0';
fclose(filePtr);
fclose(filePtr);
}
Question no 34
#include <stdio.h>
void main()
{
int mat1[3][3] = {
{ 1, 2, 1 },
{ 3, 1, 6 },
{ 9, 6, 1 }
};
int mat2[3][3] = {
{ 5, 6, 3 },
{ 3, 5, 3 },
{ 1, 4, 5 }
};
int sum[3][3];
AddMatrices(sum, mat1, mat2);
Question no 35
#include <stdio.h>
#include <math.h>
#include <stdbool.h>
int main() {
bool is_prime[MAX];
int i, j;
FILE *fp;
Question no 36(10)
data = {}
perks = {1:[(7000 + 3000), 0.10], 2:[(6000 + 2000), 0.08], 3:[(5000 + 1500), 0.05],
4:[(5000 + 1500), 0]}
def calculate_everything(emp_name):
# gross
try:
data[emp_name]
except:
return False
gross_ = data[emp_name][1] + (0.25*data[emp_name][1]) + perks[data[emp_name][0]][0]
tax = gross_ * perks[data[emp_name][0]][1]
net = gross_ - tax
return [gross_, tax, net]
while True:
_name = input("Enter Emp name(enter q to exit): ")
if _name.lower() == 'q':
break
_level = int(input("Enter Emp level (1 to 4): "))
_base_salary = int(input("Enter Emp salary: "))
print("Searching...")
while True:
_name = input("Enter emp name to know gross and others: ")
if _name.lower() == 'q':
break
sals = calculate_everything(_name)
if sals != False:
print(f"Gross Salary: {sals[0]}\nTax: {sals[1]}\nNetSalary: {sals[2]}")
else:
print("No record found check the name entered!")
Question 36 (11)
num = input('Enter the number: ')
print(num[::-1])
Question 36(12)
# take input from the user
start = int(input("Enter the start of the range: "))
end = int(input("Enter the end of the range: "))
# iterate over the range and check each number for primality
print("The prime numbers between", start, "and", end, "are:")
for num in range(start, end+1):
if is_prime(num):
print(num)
Question 36(13)
# define a function to calculate HCF
def calculate_hcf(num1, num2):
# choose the smaller number
if num1 > num2:
smaller = num2
else:
smaller = num1
hcf = 1
# find the HCF
for i in range(1, smaller+1):
if((num1 % i == 0) and (num2 % i == 0)):
hcf = i
return hcf
Question 36(14)
# take input from the user
m = int(input("Enter the value of m: "))
# convert x to radians
x = math.radians(x)
# initialize variables
term = x
sin_x = x
n = 1
error = 1
Question 36(16)
n = 5 # size of the middle row
Question 36(17)
A = [1, 3, 5, 7, 9, 11, 13, 15, 17, 19]
B = [2, 4, 6, 8, 10, 12, 14, 16, 18, 20]
C = []
print(C)
Question 36(18)
def count_char_occurrences(text, char):
count = 0
for c in text:
if c == char:
count += 1
return count
# Example usage
text = "The quick brown fox jumps over the lazy dog"
char = 'o'
def calculate_percentage(marks):
total_marks = sum(marks)
percentage = (total_marks / (len(marks) * MAX_MARKS)) * 100
return percentage
# Example usage
try:
marks = [75, 85, 92]
# marks = [75, 85, 102]
for mark in marks:
if mark > MAX_MARKS:
raise ValueError(f"Mark {mark} is greater than the maximum marks
({MAX_MARKS})")
percentage = calculate_percentage(marks)
print(f"The percentage of marks is: {percentage:.2f}%")
except ValueError as e:
print(f"Error: {e}")
Question 38
# Define the employee records as a list of dictionaries
employees = [
{'name': 'Alice', 'age': 30, 'department': 'Sales', 'salary': 50000},
{'name': 'Bob', 'age': 40, 'department': 'Marketing', 'salary': 60000},
{'name': 'Charlie', 'age': 50, 'department': 'Finance', 'salary': 70000},
]
# Access and print the individual components of the first record using indexing
print("\nAccessing individual components of the first record:")
print(f"Name: {employees[0]['name']}")
print(f"Age: {employees[0]['age']}")
print(f"Department: {employees[0]['department']}")
print(f"Salary: {employees[0]['salary']}")
Question 39
# Open the input file in read mode
with open('input.txt', 'r') as f1:
# Read the contents of the input file
contents = f1.read()