Basics of C Programming
Basics of C Programming
Problems set
1 BASICS OF C PROGRAMMING
1. Write a program to print "Hello, World!".
2. Write a program to take two integers as input and print their sum.
3. Write a program to find the area of a rectangle using its length and breadth.
4. Write a program to swap two numbers without using a temporary variable.
5. Write a program to check if a given number is even or odd.
2 CONTROL STATEMENTS
1. Write a program to find the largest of three numbers using if-else.
2. Write a program to check if a number is positive, negative, or zero.
3. Write a program to calculate the factorial of a number using a for loop.
4. Write a program to print the first n natural numbers using a while loop.
5. Write a program to display the multiplication table of a given number.
3 ARRAYS
1. Write a program to store 5 integers in an array and print them.
2. Write a program to find the largest and smallest elements in an array.
3. Write a program to calculate the sum and average of elements in an array.
4. Write a program to reverse an array.
5. Write a program to merge two arrays into a single array.
4 STRINGS
1. Write a program to take a string as input and display its length.
2. Write a program to concatenate two strings without using library functions.
3. Write a program to check if a string is a palindrome.
4. Write a program to count the number of vowels and consonants in a string.
5. Write a program to find the frequency of a character in a string.
5 FUNCTIONS
1. Write a function to find the GCD of two numbers.
2. Write a function to calculate the power of a number.
Page 1 of 28
L2 Info Revision DR NOURI Nabil
6 POINTERS
1. Write a program to demonstrate pointer arithmetic.
2. Write a program to swap two numbers using pointers.
3. Write a program to find the length of a string using a pointer.
4. Write a program to reverse an array using pointers.
5. Write a program to dynamically allocate memory for an array using malloc.
7 STRUCTURES
1. Write a program to define a structure for a student (name, roll number, marks) and
display the details.
2. Write a program to calculate the total and average marks of 5 students using an array of
structures.
3. Write a program to store and display information of 5 employees (name, age, salary).
4. Write a program to demonstrate passing structures to functions.
5. Write a program to store a date (day, month, year) and display it in "dd-mm-yyyy"
format.
8 FILE HANDLING
1. Write a program to create a file and write "Hello, World!" into it.
2. Write a program to read the contents of a file and display it on the screen.
3. Write a program to count the number of words in a file.
4. Write a program to append text to an existing file.
5. Write a program to copy the contents of one file to another.
Page 2 of 28
L2 Info Revision DR NOURI Nabil
10 ADVANCED ALGORITHMS
1. Write a program to generate the Fibonacci series up to n terms.
2. Write a program to find the sum of a geometric series.
3. Write a program to solve the Tower of Hanoi problem.
4. Write a program to find all prime numbers between two given numbers.
5. Write a program to implement the Sieve of Eratosthenes.
Page 3 of 28
L2 Info Revision DR NOURI Nabil
Solutions
1 BASICS OF C PROGRAMMING
#include <stdio.h>
int main() {
printf("Hello, World!\n");
return 0;
}
#include <stdio.h>
int main() {
int a, b;
printf("Enter two integers: ");
scanf("%d %d", &a, &b);
printf("Sum: %d\n", a + b);
return 0;
}
#include <stdio.h>
int main() {
float length, breadth;
printf("Enter length and breadth: ");
scanf("%f %f", &length, &breadth);
printf("Area: %.2f\n", length * breadth);
return 0;
}
#include <stdio.h>
int main() {
int a, b;
printf("Enter two numbers: ");
Page 4 of 28
L2 Info Revision DR NOURI Nabil
#include <stdio.h>
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
if (num % 2 == 0)
printf("%d is even\n", num);
else
printf("%d is odd\n", num);
return 0;
}
2 CONTROL STATEMENTS
#include <stdio.h>
int main() {
int a, b, c;
printf("Enter three numbers: ");
scanf("%d %d %d", &a, &b, &c);
if (a > b && a > c)
printf("%d is the largest\n", a);
else if (b > c)
printf("%d is the largest\n", b);
else
printf("%d is the largest\n", c);
return 0;
}
Page 5 of 28
L2 Info Revision DR NOURI Nabil
#include <stdio.h>
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
if (num > 0)
printf("%d is positive\n", num);
else if (num < 0)
printf("%d is negative\n", num);
else
printf("The number is zero\n");
return 0;
}
#include <stdio.h>
int main() {
int num, factorial = 1;
printf("Enter a number: ");
scanf("%d", &num);
for (int i = 1; i <= num; i++) {
factorial *= i;
}
printf("Factorial: %d\n", factorial);
return 0;
}
#include <stdio.h>
int main() {
int n, i = 1;
printf("Enter a number: ");
scanf("%d", &n);
while (i <= n) {
printf("%d ", i);
i++;
}
printf("\n");
return 0;
}
Page 6 of 28
L2 Info Revision DR NOURI Nabil
#include <stdio.h>
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
for (int i = 1; i <= 10; i++) {
printf("%d x %d = %d\n", num, i, num * i);
}
return 0;
}
3 ARRAYS
#include <stdio.h>
int main() {
int arr[5];
printf("Enter 5 integers: ");
for (int i = 0; i < 5; i++) {
scanf("%d", &arr[i]);
}
printf("Array elements: ");
for (int i = 0; i < 5; i++) {
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}
#include <stdio.h>
int main() {
int arr[5], largest, smallest;
printf("Enter 5 integers: ");
for (int i = 0; i < 5; i++) {
scanf("%d", &arr[i]);
}
largest = smallest = arr[0];
for (int i = 1; i < 5; i++) {
Page 7 of 28
L2 Info Revision DR NOURI Nabil
#include <stdio.h>
int main() {
int arr[5], sum = 0;
float avg;
printf("Enter 5 integers: ");
for (int i = 0; i < 5; i++) {
scanf("%d", &arr[i]);
}
for (int i = 0; i < 5; i++) {
sum += arr[i];
}
avg = sum / 5.0;
printf("Sum: %d, Average: %.2f\n", sum, avg);
return 0;
}
#include <stdio.h>
int main() {
int arr[5], temp;
printf("Enter 5 integers: ");
for (int i = 0; i < 5; i++) {
scanf("%d", &arr[i]);
}
for (int i = 0; i < 5 / 2; i++) {
temp = arr[i];
arr[i] = arr[4 - i];
arr[4 - i] = temp;
}
printf("Reversed array: ");
for (int i = 0; i < 5; i++) {
printf("%d ", arr[i]);
}
printf("\n");
Page 8 of 28
L2 Info Revision DR NOURI Nabil
return 0;
}
#include <stdio.h>
int main() {
int arr1[5], arr2[5], merged[10];
printf("Enter 5 integers for array 1: ");
for (int i = 0; i < 5; i++) {
scanf("%d", &arr1[i]);
}
printf("Enter 5 integers for array 2: ");
for (int i = 0; i < 5; i++) {
scanf("%d", &arr2[i]);
}
for (int i = 0; i < 5; i++) {
merged[i] = arr1[i];
merged[i + 5] = arr2[i];
}
printf("Merged array: ");
for (int i = 0; i < 10; i++) {
printf("%d ", merged[i]);
}
printf("\n");
return 0;
}
4 STRINGS
Page 9 of 28
L2 Info Revision DR NOURI Nabil
Page 10 of 28
L2 Info Revision DR NOURI Nabil
}
return 0;
}
Page 11 of 28
L2 Info Revision DR NOURI Nabil
5 FUNCTIONS
Page 12 of 28
L2 Info Revision DR NOURI Nabil
if (n == 0 || n == 1) {
return 1;
}
return n * factorial(n - 1);
}
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
printf("Factorial: %d\n", factorial(num));
return 0;
}
Page 13 of 28
L2 Info Revision DR NOURI Nabil
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
printf("Sum of digits: %d\n", sumOfDigits(num));
return 0;
}
6 POINTERS
return 0;
}
Page 14 of 28
L2 Info Revision DR NOURI Nabil
Page 15 of 28
L2 Info Revision DR NOURI Nabil
if (arr == NULL) {
printf("Memory allocation failed.\n");
return 1;
}
7 STRUCTURES
Page 16 of 28
L2 Info Revision DR NOURI Nabil
scanf("%d", &student.rollNumber);
printf("Enter marks: ");
scanf("%f", &student.marks);
printf("\nStudent Details:\n");
printf("Name: %s", student.name);
printf("Roll Number: %d\n", student.rollNumber);
printf("Marks: %.2f\n", student.marks);
return 0;
}
7.2 CALCULATE TOTAL AND AVERAGE MARKS OF 5 STUDENTS USING AN ARRAY OF STRUCTURES
#include <stdio.h>
struct Student {
char name[50];
int marks[5];
};
int main() {
struct Student students[5];
int totalMarks, sum;
float average;
Page 17 of 28
L2 Info Revision DR NOURI Nabil
float salary;
};
int main() {
struct Employee employees[5];
printf("\nEmployee Details:\n");
for (int i = 0; i < 5; i++) {
printf("\nEmployee %d:\n", i+1);
printf("Name: %s", employees[i].name);
printf("Age: %d\n", employees[i].age);
printf("Salary: %.2f\n", employees[i].salary);
}
return 0;
}
Page 18 of 28
L2 Info Revision DR NOURI Nabil
int day;
int month;
int year;
};
int main() {
struct Date date;
printf("Enter day: ");
scanf("%d", &date.day);
printf("Enter month: ");
scanf("%d", &date.month);
printf("Enter year: ");
scanf("%d", &date.year);
8 FILE HANDLING
Page 19 of 28
L2 Info Revision DR NOURI Nabil
putchar(ch);
}
fclose(file);
return 0;
}
Page 20 of 28
L2 Info Revision DR NOURI Nabil
return 1;
}
fprintf(file, "Appending new content!\n");
fclose(file);
printf("Text appended successfully.\n");
return 0;
}
fclose(source);
fclose(destination);
printf("File copied successfully.\n");
return 0;
}
Page 21 of 28
L2 Info Revision DR NOURI Nabil
Page 22 of 28
L2 Info Revision DR NOURI Nabil
printf("\n");
return 0;
}
Page 23 of 28
L2 Info Revision DR NOURI Nabil
}
int main() {
int arr[] = {1, 2, 4, 5, 8};
int target = 4;
int size = sizeof(arr) / sizeof(arr[0]);
int index = binarySearch(arr, size, target);
if (index != -1) {
printf("Element found at index: %d\n", index);
} else {
printf("Element not found\n");
}
return 0;
}
Page 24 of 28
L2 Info Revision DR NOURI Nabil
10 ADVANCED ALGORITHMS
Page 25 of 28
L2 Info Revision DR NOURI Nabil
void printBoard() {
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
printf("%d ", board[i][j]);
}
printf("\n");
}
}
int main() {
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
board[i][j] = 0; // Initialize board to 0
}
}
if (solveNQueens(0)) {
printf("Solution to N-Queens Problem:\n");
printBoard();
} else {
printf("Solution does not exist\n");
}
return 0;
}
Page 26 of 28
L2 Info Revision DR NOURI Nabil
Page 27 of 28
L2 Info Revision DR NOURI Nabil
return 0;
}
Page 28 of 28