C Programs Sem II
C Programs Sem II
screen
#include <stdio.h>
// Function to add three numbers
int addThreeNumbers(int num1, int num2, int num3)
{
return num1 + num2 + num3;
}
int main()
{
int num1, num2, num3, sum;
// Ask the user for three numbers
printf("Enter the first number: ");
scanf("%d", &num1);
printf("Enter the second number: ");
scanf("%d", &num2);
printf("Enter the third number: ");
scanf("%d", &num3);
// Call the function to add the three numbers
sum = addThreeNumbers(num1, num2, num3);
// Display the output on the screen
printf("The sum of %d, %d, and %d is: %d\n", num1, num2, num3, sum);
return 0;
}
Program 2 - Write a function to find factorial of a given number.
#include <stdio.h>
// Function to find factorial of a given number
long long factorial(int num)
{
if (num < 0)
{
printf("Error: Factorial of a negative number is not defined.\n");
return -1;
}
else if (num == 0 || num == 1)
{
return 1;
}
else
{
long long fact = 1;
for (int i = 2; i <= num; i++)
{
fact *= i;
}
return fact;
}
}
int main()
{
int num;
printf("Enter a number: ");
scanf("%d", &num);
long long fact = factorial(num);
if (fact != -1)
{
printf("Factorial of %d is: %lld\n", num, fact);
}
return 0;
}
Program 3 - Write a function to reverse a given number
#include <stdio.h>
// Function to reverse a given number
int reverseNumber(int num)
{
int reversedNum = 0;
while (num != 0)
{
int digit = num % 10;
reversedNum = reversedNum * 10 + digit;
num /= 10;
}
return reversedNum;
}
int main()
{
int num;
printf("Enter a number: ");
scanf("%d", &num);
int reversedNum = reverseNumber(num);
printf("Reversed number is: %d\n", reversedNum);
return 0;
}
Program 4 -Write a program to calculate factorial of given number using
recursive function.
#include <stdio.h>
// Recursive function to calculate factorial
long long factorial(int num)
{
if (num < 0)
{
printf("Error: Factorial of a negative number is not defined.\n");
return -1;
}
else if (num == 0 || num == 1)
{
return 1;
} else
{
return num * factorial(num - 1);
}
}
int main()
{
int num;
printf("Enter a number: ");
scanf("%d", &num);
long long fact = factorial(num);
if (fact != -1)
{
printf("Factorial of %d is: %lld\n", num, fact);
}
return 0;
}
Program 5 - Write a program using function which swap two number using a) call
by value and b) call by reference.
#include <stdio.h>
// Function to swap two numbers using call by value
void swapByValue(int num1, int num2)
{
int temp = num1;
num1 = num2;
num2 = temp;
printf("Swap by value: num1 = %d, num2 = %d\n", num1, num2);
}
// Function to swap two numbers using call by reference
void swapByReference(int* num1, int* num2)
{
int temp = *num1;
*num1 = *num2;
*num2 = temp;
printf("Swap by reference: num1 = %d, num2 = %d\n", *num1, *num2);
}
int main()
{
int num1 = 10;
int num2 = 20;
printf("Original values: num1 = %d, num2 = %d\n", num1, num2);
// Call the function with call by value
swapByValue(num1, num2);
printf("After swap by value: num1 = %d, num2 = %d\n", num1, num2);
// Call the function with call by reference
swapByReference(&num1, &num2);
printf("After swap by reference: num1 = %d, num2 = %d\n", num1, num2);
return 0;
}
Program 6 - Write a program to dynamically allocate memory of n items to an
integer pointer, display their sum and average.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int n;
printf("Enter the number of items: ");
scanf("%d", &n);
// Dynamically allocate memory for the array
int* arr = (int*)malloc(n * sizeof(int));
if (arr == NULL)
{
printf("Memory allocation failed.\n");
return -1;
}
// Prompt the user to input values
printf("Enter %d integer values:\n", n);
for (int i = 0; i < n; i++)
{
scanf("%d", &arr[i]);
}
// Calculate the sum of the values
int sum = 0;
for (int i = 0; i < n; i++)
{
sum += arr[i];
}
// Calculate the average of the values
double average = (double)sum / n;
// Display the sum and average
printf("Sum: %d\n", sum);
printf("Average: %.2f\n", average);
// Deallocate the memory
free(arr);
return 0;
}
Program 7 - Write a program to dynamically allocate memory of n items to a
character array, end it with ‘\0’ and count number of vowels, consonants and
spaces in it
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
int main()
{
int n;
printf("Enter the length of the string: ");
scanf("%d", &n);
char* str = (char*)malloc((n + 1) * sizeof(char));
if (str == NULL)
{
printf("Memory allocation failed.\n");
return -1;
}
// Prompt the user to input a string
printf("Enter a string of length %d: ", n);
scanf(" %[^\n]", str);
// Count the number of vowels, consonants, and spaces
int vowels = 0, consonants = 0, spaces = 0;
for (int i = 0; str[i] != '\0'; i++)
{
if (isspace(str[i]))
{
spaces++;
}
else if (isalpha(str[i]))
{
if (str[i] == 'a' || str[i] == 'e' || str[i] == 'i' || str[i] == 'o' || str[i] == 'u' ||
str[i] == 'A' || str[i] == 'E' || str[i] == 'I' || str[i] == 'O' || str[i] == 'U')
{
vowels++;
}
else
{
consonants++;
}
}
}
// Display the counts
printf("Number of vowels: %d\n", vowels);
printf("Number of consonants: %d\n", consonants);
printf("Number of spaces: %d\n", spaces);
// Deallocate the memory
free(str);
return 0;
}
Program 8 - Write a program which creates student structure which accept-
students rollno, student name, address, subject marks, percentage and display
same on screen.
#include <stdio.h>
#include <string.h>
// Define the student structure
struct Student
{
int rollNo;
char name[50];
char address[100];
float subjectMarks[5];
float percentage;
};
// Function to calculate the percentage
float calculatePercentage(float marks[5])
{
float sum = 0;
for (int i = 0; i < 5; i++)
{
sum += marks[i];
}
return (sum / 500) * 100;
}
// Function to display the student details
void displayStudentDetails(struct Student student)
{
printf("Roll No: %d\n", student.rollNo);
printf("Name: %s\n", student.name);
printf("Address: %s\n", student.address);
printf("Subject Marks: ");
for (int i = 0; i < 5; i++)
{
printf("%.2f ", student.subjectMarks[i]);
}
printf("\nPercentage: %.2f%%\n", student.percentage);
}
int main()
{
struct Student student;
// Accept the student details
printf("Enter Roll No: ");
scanf("%d", &student.rollNo);
printf("Enter Name: ");
scanf(" %[^\n]", student.name);
printf("Enter Address: ");
scanf(" %[^\n]", student.address);
printf("Enter Subject Marks (5 subjects): ");
for (int i = 0; i < 5; i++) {
scanf("%f", &student.subjectMarks[i]);
}
// Calculate the percentage
student.percentage = calculatePercentage(student.subjectMarks);
// Display the student details
printf("\nStudent Details:\n");
displayStudentDetails(student);
return 0;
}
Program 9 - Write a file handling program which accept student information store
it into disk file.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// Define the student structure
struct Student
{
int rollNo;
char name[50];
char address[100];
float subjectMarks[5];
};
// Function to accept student information
void acceptStudentInfo(struct Student* student)
{
printf("Enter Roll No: ");
scanf("%d", &student->rollNo);
printf("Enter Name: ");
scanf(" %[^\n]", student->name);
printf("Enter Address: ");
scanf(" %[^\n]", student->address);
printf("Enter Subject Marks (5 subjects): ");
for (int i = 0; i < 5; i++) {
scanf("%f", &student->subjectMarks[i]);
}
}
// Function to display student information
void displayStudentInfo(struct Student student)
{
printf("Roll No: %d\n", student.rollNo);
printf("Name: %s\n", student.name);
printf("Address: %s\n", student.address);
printf("Subject Marks: ");
for (int i = 0; i < 5; i++) {
printf("%.2f ", student.subjectMarks[i]);
}
printf("\n");
}
int main()
{
struct Student student;
FILE* file;
// Open the file in write mode
file = fopen("student_info.txt", "w");
if (file == NULL)
{
printf("Error opening file!\n");
exit(1);
}
// Accept student information
acceptStudentInfo(&student);