0% found this document useful (0 votes)
19 views18 pages

PPS Experiential Learning

Uploaded by

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

PPS Experiential Learning

Uploaded by

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

EXPERIENTIAL LEARNING

1. a) Write a C program to perform the arithmetic operations on two integer


numbers.

void main() {
int num1, num2;
int sum, difference, product, quotient, remainder;
clrscr();

printf("Enter two integers: ");


scanf("%d %d", &num1, &num2);

sum = num1 + num2;


difference = num1 - num2;
product = num1 * num2;
quotient = num1 / num2;
remainder = num1 % num2;

printf("Sum: %d\n", sum);


printf("Difference: %d\n", difference);
printf("Product: %d\n", product);
printf("Quotient: %d\n", quotient);
printf("Remainder: %d\n", remainder);

getch();
}

b) Write a program to evaluate the following expressions by reading the necessary


values from the keyboard.
i. (ax + b)/(ax – b) ii. 2.5 log x + Cos 320+ | x2+ y2|
iii. ax5 + bx3 + c iv. aekt

#include <math.h>

void main() {
double a, b, x, y, k, t;
double expr1, expr2, expr3, expr4;

printf("Enter values for a, b, x, y, k, t: ");


scanf("%lf %lf %lf %lf %lf %lf", &a, &b, &x, &y, &k, &t);

expr1 = (a * x + b) / (a * x - b);
expr2 = 2.5 * log(x) + cos(320 * M_PI / 180) + fabs(pow(x, 2) + pow(y, 2));
expr3 = a * pow(x, 5) + b * pow(x, 3) + k;
expr4 = a * exp(k * t);

printf("Result of (ax + b)/(ax – b): %lf\n", expr1);


printf("Result of 2.5 log x + Cos 320+ | x2+ y2|: %lf\n", expr2);
printf("Result of ax5 + bx3 + c: %lf\n", expr3);
printf("Result of aekt: %lf\n", expr4);
}
2. a) Write a C program to find the roots of a quadratic equation.

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

void main() {
double a, b, c, discriminant, root1, root2, realPart, imaginaryPart;
printf("Enter coefficients a, b and 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("Roots are real and different.\n");
printf("Root 1 = %.2lf\n", root1);
printf("Root 2 = %.2lf\n", root2);
} else if (discriminant == 0) {
root1 = -b / (2*a);
printf("Roots are real and the same.\n");
printf("Root 1 = Root 2 = %.2lf\n", root1);
} else {
realPart = -b / (2*a);
imaginaryPart = sqrt(-discriminant) / (2*a);
printf("Roots are complex and different.\n");
printf("Root 1 = %.2lf + %.2lfi\n", realPart, imaginaryPart);
printf("Root 2 = %.2lf - %.2lfi\n", realPart, imaginaryPart);
}
}
b) In a town, the percentage of men is 52. The percentage of total literacy is 48 and
the total percentage of literate men is 35 of the total population. Write a C
program to find the total number of illiterate men and women if the population
of the town is 7000.

#include <stdio.h>

int main() {
int total_population = 7000;
int total_men = (52 * total_population) / 100;
int total_literacy = (48 * total_population) / 100;
int literate_men = (35 * total_population) / 100;
int illiterate_men = total_men - literate_men;
int illiterate_women = total_population - total_men - (total_literacy -
literate_men);

printf("Total illiterate men: %d\n", illiterate_men);


printf("Total illiterate women: %d\n", illiterate_women);

return 0;
}

3. a) Write a C Program to compute an electricity bill based on the following slab


rates.
Consumption units Rate (in Rupees/unit)
0-100 4.0
101-150 4.6
151-200 5.2
201-300 6.3
Above 300 8.0
(Hint: Take current and old meter readings from the user to get consumption
units)

#include <stdio.h>

int main() {
int old_reading, new_reading, units_consumed;
double bill_amount;

printf("Enter old meter reading: ");


scanf("%d", &old_reading);
printf("Enter new meter reading: ");
scanf("%d", &new_reading);

units_consumed = new_reading - old_reading;

if (units_consumed <= 100) {


bill_amount = units_consumed * 4.0;
} else if (units_consumed <= 150) {
bill_amount = 100 * 4.0 + (units_consumed - 100) * 4.6;
} else if (units_consumed <= 200) {
bill_amount = 100 * 4.0 + 50 * 4.6 + (units_consumed - 150) * 5.2;
} else if (units_consumed <= 300) {
bill_amount = 100 * 4.0 + 50 * 4.6 + 50 * 5.2 + (units_consumed - 200) *
6.3;
} else {
bill_amount = 100 * 4.0 + 50 * 4.6 + 50 * 5.2 + 100 * 6.3 +
(units_consumed - 300) * 8.0;
}

printf("Electricity Bill: Rs. %.2lf\n", bill_amount);

return 0;
}
b) An insurance company computes the premium amount based on the following;
i. If a person's health is excellent and the person is between 25 and 35 years of
age and lives in a city, and is a male then the premium is Rs.4 per thousand
and the policy amount cannot exceed Rs.2 lakhs.
ii. If a person satisfies all the above conditions and is female then thepremium
is Rs.3 per thousand and the policy amount cannot exceed Rs.1 lakh.
iii. If a person's health is poor and the person is between 25 and 35 years of
age and lives in a village and is a male then premium is Rs.6 per thousand
and the policy cannot exceed Rs. 10000.
iv. In all other cases the person is not insured.
Write a C program to determine whether the person should be insured or
not, his/her premium rate and maximum amount for which he/she can be insured.

#include <stdio.h>

int main() {
char health, gender, location;
int age;
double premium_rate;
double max_policy_amount;

printf("Enter health condition (E for Excellent, P for Poor): ");


scanf(" %c", &health);
printf("Enter gender (M for Male, F for Female): ");
scanf(" %c", &gender);
printf("Enter location (C for City, V for Village): ");
scanf(" %c", &location);
printf("Enter age: ");
scanf("%d", &age);

if (age >= 25 && age <= 35) {


if (health == 'E' && location == 'C') {
if (gender == 'M') {
premium_rate = 4.0;
max_policy_amount = 200000.0;
} else if (gender == 'F') {
premium_rate = 3.0;
max_policy_amount = 100000.0;
} else {
printf("Invalid gender input.\n");
return 1;
}
} else if (health == 'P' && location == 'V' && gender == 'M') {
premium_rate = 6.0;
max_policy_amount = 10000.0;
} else {
printf("Person is not insured.\n");
return 0;
}

printf("Premium Rate: Rs. %.2lf per thousand\n", premium_rate);


printf("Max Policy Amount: Rs. %.2lf\n", max_policy_amount);
} else {
printf("Person is not insured.\n");
}

return 0;
}

c) Write a C Program to find the grade for a student using a Switch case. The userneeds to
enter a subject score (varies from 0 to 100)and then display the grade as described below

Score Grade Score Grade


> = 90 O >=50 to < 60 D
>=80 to < 90 A >=40 to < 50 E
>=70 to < 80 B < 40 Fail
>=60 to < 70 C

#include <stdio.h>

int main() {
int score;
char grade;

printf("Enter the score (0-100): ");


scanf("%d", &score);

switch (score / 10) {


case 10:
case 9:
grade = 'O';
break;
case 8:
grade = 'A';
break;
case 7:
grade = 'B';
break;
case 6:
grade = 'C';
break;
case 5:
grade = 'D';
break;
case 4:
grade = 'E';
break;
default:
grade = 'F';
break;
}
printf("Grade: %c\n", grade);

return 0;
}

4. a) A Fibonacci sequence is defined as follows:


The first and second terms in the sequence are 0 and 1. Sub-sequent terms
are found by adding the preceding two terms in the sequence. Write a C program
to generate the first n terms of the sequence.
#include <stdio.h>

int main() {
int n, i;
int t1 = 0, t2 = 1, nextTerm;

printf("Enter the number of terms: ");


scanf("%d", &n);

printf("Fibonacci Sequence: ");


for (i = 1; i <= n; ++i) {
printf("%d, ", t1);
nextTerm = t1 + t2;
t1 = t2;
t2 = nextTerm;
}

return 0;
}

b) Write a C program to find the sum of individual digits of a positive integer.

#include <stdio.h>

int main() {
int n, sum = 0, remainder;

printf("Enter an integer: ");


scanf("%d", &n);

while (n != 0) {
remainder = n % 10;
sum += remainder;
n /= 10;
}

printf("Sum of digits: %d\n", sum);

return 0;

}
c) Write a C program to read two numbers x and n, and then compute the sum of
the geometric progression: 1+x+x2+x3+………….+xn. Show appropriate error
message for n<0. (Example: if n is 3 and x is 5, then the sum is: 1+5+25+125)

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

int main() {
int x, n, i;
double sum = 0.0;

printf("Enter the values for x and n: ");


scanf("%d %d", &x, &n);

if (n < 0) {
printf("Error: n must be non-negative.\n");
} else {
for (i = 0; i <= n; i++) {
sum += pow(x, i);
}
printf("Sum of the geometric progression: %.2lf\n", sum);
}

return 0;

d) Write a C program to print the following pattern.

1
1 2 1
1 2 3 2 1
1 2 3 4 3 2 1
1 2 3 4 5 4 3 2 1

void main() {
int i, j, n, space;

printf("Enter the number of rows: ");


scanf("%d", &n);

for (i = 1; i <= n; i++) {


for (space = 1; space <= n - i; space++) {
printf(" ");
}
for (j = 1; j <= i; j++) {
printf("%d ", j);
}
for (j = i - 1; j >= 1; j--) {
printf("%d ", j);
}
printf("\n");
}

}
5. a) Write a C program to find both the largest and smallest numbers in a list of
integers.
#include <stdio.h>

int main() {
int n, i;
int largest, smallest;

printf("Enter the number of elements: ");


scanf("%d", &n);

int arr[n];
printf("Enter %d integers: ", n);
for (i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}

largest = smallest = arr[0];


for (i = 1; i < n; i++) {
if (arr[i] > largest) {
largest = arr[i];
}
if (arr[i] < smallest) {
smallest = arr[i];
}
}

printf("Largest number: %d\n", largest);


printf("Smallest number: %d\n", smallest);

return 0;

b) Write a C program that uses function to perform the following:


i) Addition of Two Matrices ii) Multiplication of Two Matrices

#include <stdio.h>

void addMatrices(int row, int col, int a[row][col], int b[row][col], int result[row][col]) {
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
result[i][j] = a[i][j] + b[i][j];
}
}
}

void multiplyMatrices(int row1, int col1, int a[row1][col1], int row2, int col2, int
b[row2][col2], int result[row1][col2]) {
for (int i = 0; i < row1; i++) {
for (int j = 0; j < col2; j++) {
result[i][j] = 0;
for (int k = 0; k < col1; k++) {
result[i][j] += a[i][k] * b[k][j];
}
}
}
}

int main() {
int row1, col1, row2, col2;
printf("Enter rows and columns for the first matrix: ");
scanf("%d %d", &row1, &col1);
printf("Enter rows and columns for the second matrix: ");
scanf("%d %d", &row2, &col2);

if (col1 != row2) {
printf("Matrix multiplication not possible.\n");
return 1;
}

int a[row1][col1], b[row2][col2], sum[row1][col1], product[row1][col2];

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


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

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


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

addMatrices(row1, col1, a, b, sum);


multiplyMatrices(row1, col1, a, row2, col2, b, product);

printf("Sum of matrices:\n");
for (int i = 0; i < row1; i++) {
for (int j = 0; j < col1; j++) {
printf("%d ", sum[i][j]);
}
printf("\n");
}

printf("Product of matrices:\n");
for (int i = 0; i < row1; i++) {
for (int j = 0; j < col2; j++) {
printf("%d ", product[i][j]);
}
printf("\n");
}

return 0;
}

6. a) Write a C program to insert a sub-string in to a main string at a given position.


#include <stdio.h>
#include <string.h>

int main() {
char mainStr[100], subStr[50];
int position, lenMain, lenSub;

printf("Enter the main string: ");


gets(mainStr);
printf("Enter the substring to insert: ");
gets(subStr);
printf("Enter the position to insert the substring: ");
scanf("%d", &position);

lenMain = strlen(mainStr);
lenSub = strlen(subStr);

for (int i = lenMain - 1; i >= position - 1; i--) {


mainStr[i + lenSub] = mainStr[i];
}

for (int i = 0; i < lenSub; i++) {


mainStr[position - 1 + i] = subStr[i];
}

printf("String after insertion: %s\n", mainStr);

return 0;

b) Write a C program to count the lines, words and characters in a given text.
#include <stdio.h>

int main() {
char ch;
int characters = 0, words = 0, lines = 0;
int inWord = 0;

printf("Enter text (Ctrl+D to end):\n");

while ((ch = getchar()) != EOF) {


characters++;

if (ch == ' ' || ch == '\n' || ch == '\t') {


inWord = 0;
} else if (inWord == 0) {
inWord = 1;
words++;
}

if (ch == '\n') {
lines++;
}
}

printf("Characters: %d\n", characters);


printf("Words: %d\n", words);
printf("Lines: %d\n", lines);

return 0;

7. a) Write a C program to generate all the prime numbers between 1 and n, where nis
a value entered by the user. Define a separate function to generate prime
numbers.
#include <stdio.h>

int isPrime(int n) {
if (n <= 1) return 0;
for (int i = 2; i <= n / 2; i++) {
if (n % i == 0) return 0;
}
return 1;
}

int main() {
int n;
printf("Enter a number: ");
scanf("%d", &n);

printf("Prime numbers between 1 and %d are:\n", n);


for (int i = 2; i <= n; i++) {
if (isPrime(i)) {
printf("%d ", i);
}
}

return 0;

b) Write C program that uses recursive function to find the following.


i) Factorial of a given integer ii) GCD of two given integers

i)
#include <stdio.h>

int factorial(int n) {
if (n == 0)
return 1;
else
return n * factorial(n - 1);
}

int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
printf("Factorial of %d = %d\n", num, factorial(num));
return 0;
}

ii)
#include <stdio.h>

int gcd(int a, int b) {


if (b == 0)
return a;
else
return gcd(b, a % b);
}

int main() {
int num1, num2;
printf("Enter two numbers: ");
scanf("%d %d", &num1, &num2);
printf("GCD of %d and %d is %d\n", num1, num2, gcd(num1, num2));
return 0;
}
8. a) Write a C program to print the elements of an array in reverse order using pointers.

#include <stdio.h>

int main() {
int n;
printf("Enter the number of elements in the array: ");
scanf("%d", &n);

int arr[n];
printf("Enter %d elements:\n", n);
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}

printf("Array in reverse order:\n");


int *ptr = arr + n - 1;
for (int i = 0; i < n; i++) {
printf("%d ", *ptr);
ptr--;
}

return 0;

b) Write a C program to count the number of vowels and consonants in a string using
pointers.
#include <stdio.h>
#include <ctype.h>

int main() {
char str[100];
char *ptr;
int vowels = 0, consonants = 0;

printf("Enter a string: ");


gets(str);

ptr = str;

while (*ptr != '\0') {


char ch = tolower(*ptr);
if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') {
vowels++;
} else if ((ch >= 'a' && ch <= 'z')) {
consonants++;
}
ptr++;
}

printf("Vowels: %d\n", vowels);


printf("Consonants: %d\n", consonants);

return 0;

}
c) Write a C program to store n elements in an array and print the elements in sorted
order using pointers.
#include <stdio.h>

void sortArray(int *arr, int n) {


for (int i = 0; i < n - 1; i++) {
for (int j = i + 1; j < n; j++) {
if (*(arr + i) > *(arr + j)) {
int temp = *(arr + i);
*(arr + i) = *(arr + j);
*(arr + j) = temp;
}
}
}
}

int main() {
int n;
printf("Enter the number of elements in the array: ");
scanf("%d", &n);

int arr[n];
printf("Enter %d elements:\n", n);
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}

sortArray(arr, n);

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

return 0;

9. a) Write a C program that performs the following operations:


i. Reading a complex number ii. Writing a complex number
iii. Addition of two complex numbers iv. Multiplication of two complex
numbers
(Note: Represent complex number using a structure.)

#include <stdio.h>

typedef struct {
float real;
float imag;
} Complex;

Complex addComplex(Complex c1, Complex c2) {


Complex result;
result.real = c1.real + c2.real;
result.imag = c1.imag + c2.imag;
return result;
}
Complex multiplyComplex(Complex c1, Complex c2) {
Complex result;
result.real = c1.real * c2.real - c1.imag * c2.imag;
result.imag = c1.real * c2.imag + c1.imag * c2.real;
return result;
}

int main() {
Complex c1, c2, sum, product;

printf("Enter first complex number (real and imaginary): ");


scanf("%f %f", &c1.real, &c1.imag);

printf("Enter second complex number (real and imaginary): ");


scanf("%f %f", &c2.real, &c2.imag);

sum = addComplex(c1, c2);


product = multiplyComplex(c1, c2);

printf("Sum: %.2f + %.2fi\n", sum.real, sum.imag);


printf("Product: %.2f + %.2fi\n", product.real, product.imag);

return 0;
}

b) Define a structure to store employee details include Employee-Number, Employee-


Name, Basic-pay, Date-of-Joining. Write a C program for thefollowing

Employee-Name, Basic-pay, Date-of-Joining. Write a C program for the


following.
i. A function to store 10 employee details.
ii. A function to implement the following rules while revising the basic pay.
If Basic-pay<=Rs.5000 then increase it by 15%.
If Basic-pay> Rs.5000 and <=Rs.25000 then it increase by10%.
If Basic-pay>Rs.25000 then there is no change in Basic-pay.
iii. A function to print the details of employees whohave completed 20 years of
service from the Date-of-Joining.
#include <stdio.h>

typedef struct {
int empNo;
char name[50];
float basicPay;
int doj[3]; // Date of Joining as [day, month, year]
} Employee;

void storeEmployeeDetails(Employee *emp, int n) {


for (int i = 0; i < n; i++) {
printf("Enter details for employee %d\n", i + 1);
printf("Employee Number: ");
scanf("%d", &emp[i].empNo);
printf("Employee Name: ");
scanf("%s", emp[i].name);
printf("Basic Pay: ");
scanf("%f", &emp[i].basicPay);
printf("Date of Joining (dd mm yyyy): ");
scanf("%d %d %d", &emp[i].doj[0], &emp[i].doj[1], &emp[i].doj[2]);
}
}

void reviseBasicPay(Employee *emp, int n) {


for (int i = 0; i < n; i++) {
if (emp[i].basicPay <= 5000) {
emp[i].basicPay += emp[i].basicPay * 0.15;
} else if (emp[i].basicPay > 5000 && emp[i].basicPay <= 25000) {
emp[i].basicPay += emp[i].basicPay * 0.10;
}
}
}

void printEmployeesWith20YearsService(Employee *emp, int n) {


int currentYear = 2024; // Assuming current year is 2024

for (int i = 0; i < n; i++) {


if ((currentYear - emp[i].doj[2]) >= 20) {
printf("\nEmployee Number: %d\n", emp[i].empNo);
printf("Employee Name: %s\n", emp[i].name);
printf("Basic Pay: %.2f\n", emp[i].basicPay);
printf("Date of Joining: %02d-%02d-%04d\n", emp[i].doj[0],
emp[i].doj[1], emp[i].doj[2]);
}
}
}

int main() {
int n = 10;
Employee emp[n];

storeEmployeeDetails(emp, n);
reviseBasicPay(emp, n);
printEmployeesWith20YearsService(emp, n);

return 0;
}
10 a) Write a C program to reverse the first n characters of a given text file.
#include <stdio.h>

void reverseString(char *str, int n) {


for (int i = 0; i < n / 2; i++) {
char temp = str[i];
str[i] = str[n - i - 1];
str[n - i - 1] = temp;
}
}

int main() {
FILE *file;
char filename[100];
int n;

printf("Enter the filename: ");


scanf("%s", filename);
printf("Enter the number of characters to reverse: ");
scanf("%d", &n);

file = fopen(filename, "r+");


if (file == NULL) {
printf("File cannot be opened.\n");
return 1;
}

char buffer[100];
fread(buffer, sizeof(char), n, file);
reverseString(buffer, n);

fseek(file, 0, SEEK_SET);
fwrite(buffer, sizeof(char), n, file);

fclose(file);
printf("First %d characters reversed in the file %s.\n", n, filename);

return 0;
}
b) Write a C program to merge two files into a new file.
#include <stdio.h>

int main() {
FILE *file1, *file2, *file3;
char filename1[100], filename2[100], filename3[100];
char ch;

printf("Enter the first filename: ");


scanf("%s", filename1);
printf("Enter the second filename: ");
scanf("%s", filename2);
printf("Enter the name of the output file: ");
scanf("%s", filename3);

file1 = fopen(filename1, "r");


file2 = fopen(filename2, "r");
file3 = fopen(filename3, "w");

if (file1 == NULL || file2 == NULL || file3 == NULL) {


printf("Error opening files.\n");
return 1;
}

while ((ch = fgetc(file1)) != EOF) {


fputc(ch, file3);
}

while ((ch = fgetc(file2)) != EOF) {


fputc(ch, file3);
}

printf("Files %s and %s merged into %s.\n", filename1, filename2, filename3);

fclose(file1);
fclose(file2);
fclose(file3);

return 0;
}

You might also like