Assignment P3
Assignment P3
GROUP : 4
Problem Statement: Write a recursive function to compute the factorial of a given positive integer
n.
Function Prototype:
Requirements:
#include<stdio.h>
long computeFactorial(int n)
{
if(n==0)
return 1;
else
return n*computeFactorial(n-1);
int main()
int n;
long k;
scanf("%d",&n);
k = computeFactorial(n);
printf("The factorial is :%ld",k);
return 0;
Problem Statement: Develop a recursive function to determine the maximum and minimum
elements in an
integer array.
Function Prototypes:
#include<stdio.h>
{
if(index<n)
if(arr[index]>maxVal)
maxVal = arr[index];
index++;
findMax(arr,n,index,maxVal);
else
return maxVal;
if(index<n)
if(arr[index]<minVal)
{
minVal = arr[index];
index++;
findMin(arr,n,index,minVal);
else
return minVal;
int main()
int n,a,b,i,max,min;
scanf("%d",&n);
int nums[n];
for(i=0;i<n;i++)
{
scanf("%d",&nums[i]);
max=nums[0];
min = nums[0];
a=findMax(nums,n,0,max);
b=findMin(nums,n,0,min);
printf("MAXIMUM :%d\n",a);
printf("MINIMUM:%d\n",b);
return 0;
Function Prototype:
void reverseString(char str[], int start, int end);
#include<stdio.h>
if(start<end)
char c;
c = st[start];
st[start] = st[end];
st[end] = c;
start++;
end--;
reverseString(st,start,end);
}
else
int main()
int i,l=0;
char st[size];
scanf("%[^\n]s",st);
for(i=0;i<size;i++)
if(st[i]!='\0')
l++;
else
break;
}
reverseString(st,0,l-1);
Problem Statement: Create a structure to store student records and display the student details.
Structure Definition:
struct Student {
char name[50];
int rollNo;
float marks;
};
Requirements:
#include<stdio.h>
struct Student
char name[50];
int rollNo;
float marks;
};
int main()
struct Student c;
printf("Name : ");
scanf("%[^\n]s",c.name);
printf("Roll no :");
scanf("%d",&c.rollNo);
printf("Marks :");
scanf("%f",&c.marks);
printf("Roll no :%d\n",c.rollNo);
printf("Marks :%.2f",c.marks);
return 0;
Structure Definition:
struct Complex {
float real;
float imag;
};
Requirements:
• Take user input for two complex numbers and perform operations.
#include<stdio.h>
struct Complex
float real;
float imag;
};
struct Complex c;
scanf("%f %f",&c.real,&c.imag);
return c;
return result;
}
return result;
printf("%.1f + %.1fi",c.real,c.imag);
int main()
c1 = readComplex();
printf("Enter the 2nd complex number :\n");
c2 = readComplex();
sum = addComplex(c1,c2);
prod = multiplyComplex(c1,c2);
printf("Sum :");
displayComplex(sum);
printf("\nProduct :");
displayComplex(prod);
return 0;}
Problem Statement: Modify the Student structure to handle multiple student records using an
array of structures.
Requirements:
#include<stdio.h>
#define size 50
struct Student
char name[size];
int rollno;
float marks;
};
int main()
int n,i,max=0,p=0;
scanf("%d",&n);
for(i=0;i<n;i++)
printf("Name :");
scanf("%s",Students[i].name);
printf("Roll no:");
scanf("%d",&Students[i].rollno);
printf("Marks :");
scanf("%f",&Students[i].marks);
for(i=0;i<n;i++)
if(Students[i].marks>max)
max = Students[i].marks;
p =i;
return 0;
}
Problem Statement: Develop an Employee Payroll System using structures, arrays of structures,
and recursion.
Structure Definition:
struct Employee {
char name[50];
int empID;
float salary;
};
Requirements:
#include<stdio.h>
#define size 100
struct Employee
char name[size];
float salary;
int empID;
};
int main()
int n;
scanf("%d",&n);
int i;
for(i=0;i<n;i++)
scanf("%s",employees[i].name);
scanf("%d",&employees[i].empID);
printf("Salary: ");
scanf("%f",&employees[i].salary);
printf("Employee records:\n");
int max=0,p=0;
for(i=0;i<n;i++)
if(employees[i].salary>max)
max = employees[i].salary;
p =i;
}
printf("\nTop Earner :%s (ID: %d) - Salary: %.2f
\n",employees[p].name,employees[p].empID,employees[p].salary);
return 0;
Problem Statement: Write a program to determine whether a given year is a leap year or a
century year. If it is a
century year (divisible by 100), ensure it’s valid according to the Gregorian calendar (i.e., year ≥
1582). Return error
Function Prototype:
Requirements:
– Divisible by 4;
return -1;
return 1;
Else
return 0;
int main()
{
int year, result;
scanf("%d", &year);
result = isValidLeapYear(year);
if (result == -1)
else if (result == 1)
Else
return 0;
}
Problem Statement: Write a program to generate a Fibonacci-like series with custom first two
terms (user input), up
to n terms.
Function Prototype:
#include<stdio.h>
int i,t;
for(i=1;i<=n;i++)
t=a;
printf("%d ",a);
a=b;
b=t+b;
}
int main()
int a,b,n;
scanf("%d %d",&a,&b);
scanf("%d",&n);
generateGeneralFibonacci(a,b,n);
return 0;
Problem Statement: Print a center-aligned symmetric numeric pyramid. Each level should mirror
the numbers.
Expected Output (for n = 4):
121
12321
1234321
Function Prototype:
#include <stdio.h>
void printSymmetricPyramid(int n)
int width = (2 * n - 1) + (n - 1); // The last row has 2*n-1 numbers, plus (n-1) spaces
int spaces = width - (2 * i - 1); //space needed to center align the pyramid row
printf(" ");
if (j < i) //adding space between numbers, but not after the last one printf(" ");
printf("\n");
int main()
int n;
printSymmetricPyramid(n);
return 0;
Function Prototype:
void multiplyMatrices(int A[][10], int B[][10], int result[][10], int m1, int n1, int m2, int n2);
Requirements:
• Matrix A dimensions: m1 × n1
• Matrix B dimensions: m2 × n2
• Valid only if n1 = m2
#include <stdio.h>
void multiplyMatrices(int A[][10], int B[][10], int result[][10], int m1, int n1, int m2, int n2)
if (n1 != m2)
return;
}
result[i][j] = 0;
int main()
printf("enter the dimensions of Matrix A (rows and columns): "); scanf("%d %d", &m1, &n1);
printf("enter the dimensions of Matrix B (rows and columns): "); scanf("%d %d", &m2, &n2);
printf("error!: Matrix dimensions are not compatible for multiplication.\n"); printf("Re-enter the
dimensions of Matrix B (rows and columns): "); scanf("%d %d", &m2, &n2);
printf("enter elements of Matrix A (%dx%d):\n", m1, n1); for (int i = 0; i < m1; i++)
scanf("%d", &A[i][j]);
}
printf("enter elements of Matrix B (%dx%d):\n", m2, n2); for (int i = 0; i < m2; i++)
scanf("%d", &B[i][j]);
printf("\n");
}
return 0;
Problem Statement: Check if a given string is a palindrome, ignoring punctuation, spaces, and
case.
Function Prototype:
Sample Input/Output:
Requirements:
#include <stdio.h>
#include <string.h>
#include <ctype.h>
if(isalnum(str[i]))
newstr[newIndex] = '\0'; //for terminating the string int start = 0, end = newIndex - 1;
if (newstr[start] != newstr[end])
{
return 0;
start++; end--;
return 1;
int main()
if(isAdvancedPalindrome(str))
else
{
printf(“the string is not a palindrome \n”);
Problem Statement: Write a program that takes user input, encrypts it using Caesar cipher (shift
by 3), writes it to
Function Prototypes:
int i = 0;
if (isalpha(text[i]))
if (isupper(text[i]))
else
{
text[i] = 'a' + (text[i] - 'a' + 3) % 26;
fclose(file);
}
printf("Reading from file:\n"); char ch;
if (isalpha(ch))
if (isupper(ch))
else
printf("%c", ch);
printf("\n");
fclose(file);
int main()
char text[100];
return 0;
• Characters
• Words
• Lines
Function Prototype:
chars++;
if(ch == '\n')
lines++;
if (isspace(ch))
if(inWord)
words++; inWord = 0;
}
else
inWord = 1;
words++;
//if file doesn't end with a newline, count the last line if (chars > 0 && ch == EOF && inWord)
lines++;
fclose(file);
printf("File statistics for \"%s\":\n", filename); printf("Characters: %d\n", chars); printf("Words: %d
\n", words);
int main()
countFileStats(filename);
return 0;
Problem Statement: Write a program that reads all lines from a text file, sorts them in ascending
(alphabetical) order,
Function Prototype:
char ch;
int pos = 0;
if (input == NULL)
{
printf("Error opening input file.\n"); exit(1);
pos = 0;
else
lines[count][pos++] = ch;
fclose(input);
for (int i = 0; i < count - 1; i++)
fclose(output);
int main()
sortLinesInFile("input.txt", "sorted.txt");
putchar(ch);
fclose(input);
printf("\nSorted lines written to sorted.txt:\n"); FILE *output = fopen("sorted.txt", "r");
putchar(ch);
fclose(output);
return 0;
Problem Statement: Design a structure to store student details (Roll Number, Name, Marks).
Write a program to:
• Read the data back and display students with marks ¿ 80.
Structure Definition:
struct Student {
int roll;
char name[50];
float marks;
};
Function Prototype:
struct Student
int roll;
};
FILE *file = fopen(filename, "wb"); //to open the file in binary write mode if (file == NULL)
FILE *file = fopen(filename, "rb"); //to open the file in binary read mode if (file == NULL)
fclose(file);
int main()
int num, i;
printf("\n===enter details for student %d: ===\n", i + 1); printf("roll no: ");
scanf("%f", &students[i].marks);
return 0;