ENR106 Introduction to
Programming
Assignment 2: Programming using C
Hard Questions
1. Detect Palindromes in a Sentence
Write a C program to check which words in a sentence are palindromes.
Requirements:
Ask the user to enter a sentence.
Split the sentence into individual words.
Check each word and display the ones that are palindromes.
Co[Link]
#include <stdio.h>
#include <string.h>
int isPalindrome(char word[]) {
int i, j;
j = strlen(word) - 1;
for(i = 0; i < j; i++, j--) {
if(word[i] != word[j])
return 0;
}
return 1;
}
int main() {
char sentence[200], word[50];
int i = 0, j = 0;
printf("Enter a sentence: ");
gets(sentence);
printf("Palindrome words: ");
while(sentence[i] != '\0')
{
if(sentence[i] != ' ')
{
word[j++] = sentence[i];
} else
{
word[j] = '\0';
if(j > 0 && isPalindrome(word))
printf("%s ", word);
j = 0;
}
i++;
}
word[j] = '\0';
if(j > 0 && isPalindrome(word))
printf("%s", word);
return 0;
}
Output::::::
Enter a sentence: my name is naman
Palindrome words: naman
2. Create a Simple Calculator Using User-Defined Functions
Write a C program to implement a basic calculator using functions.
Requirements:
The calculator should perform addition, subtraction, multiplication, and division.
Provide a menu-based interface for users to choose an operation and enter
numbers.
Use function pointers to call the correct operation function.
Co[Link]
#include <stdio.h>
float add(float a, float b){ return a + b; }
float sub(float a, float b){ return a - b; }
float mul(float a, float b){ return a * b; }
float div(float a, float b){
if(b==0){
printf("Cannot divide by zero!\n");
return 0;
}
return a / b;
}
int main()
{
float x, y, result;
int choice;
float (*operation)(float, float);
printf("[Link]\[Link]\[Link]\[Link]\nEnter choice: ");
scanf("%d", &choice);
printf("Enter two numbers: ");
scanf("%f %f", &x, &y);
if(choice==1) operation=add;
else if(choice==2) operation=sub;
else if(choice==3) operation=mul;
else if(choice==4) operation=div;
else
{
printf("Invalid choice!\n");
return 0;
}
result = operation(x, y);
printf("Result = %.2f\n", result);
return 0;
}
Output::::::
[Link]
[Link]
[Link]
[Link]
Enter choice: 2
Enter two numbers: 50 40
Result = 10.00
Or
[Link]
[Link]
[Link]
[Link]
Enter choice: sub
Enter two numbers: Invalid choice!
3. Reverse a String In-Place
Write a C program to reverse a string using a function.
Requirements:
Prompt the user to enter a string.
Reverse the string without using any extra variables or data structures.
Co[Link]
#include <stdio.h>
void reverseString(char str[])
{
int i = 0, j = 0;
while (str[j] != '\0')
j++;
j--;
while (i < j)
{
str[i] = str[i] ^ str[j];
str[j] = str[i] ^ str[j];
str[i] = str[i] ^ str[j];
i++;
j--;
}
}
int main()
{
char str[100];
printf("Enter a string: ");
gets(str);
reverseString(str);
printf("Reversed string: %s", str);
return 0;
}
Output::::::::::
Enter a string: khushi
Reversed string: ihsuhk
4. Find the Second Largest Element in an Array without Sorting
Write a C program to find the second largest number in an array.
Requirements:
Ask the user to enter the size and elements of the array.
Display the second largest element.
Handle cases where a second largest element does not exist (e.g., all elements are
equal or array size is less than 2).
Co[Link]
#include <stdio.h>
int main() {
int a[100], n, i;
int max1, max2;
printf("Enter size of array: ");
scanf("%d", &n);
if (n < 2)
{
printf("Not enough elements!");
return 0;
}
printf("Enter elements: ");
for (i = 0; i < n; i++)
{
scanf("%d", &a[i]);
}
max1 = max2 = a[0];
for (i = 1; i < n; i++)
{
if (a[i] > max1)
{
max2 = max1;
max1 = a[i];
}
else if (a[i] > max2 && a[i] != max1)
{
max2 = a[i];
}
}
if (max1 == max2)
printf("All elements are equal, no second largest.\n");
else
printf("Second largest = %d\n", max2);
return 0;
}
Output::::::
Enter size of array: 3
Enter elements: 1 3 5
Second largest = 3
5. Generate Fibonacci Series Using Recursion
Write a C program that uses a recursive function to generate and display the Fibonacci
series up to a specified number of terms.
Co[Link]
#include <stdio.h>
int fibonacci(int n)
{
if (n == 0)
return 0;
else if (n == 1)
return 1;
else
return fibonacci(n - 1) + fibonacci(n - 2);
}
int main()
{
int n, i;
printf("Enter number of terms: ");
scanf("%d", &n);
printf("Fibonacci Series: ");
for (i = 0; i < n; i++)
{
printf("%d ", fibonacci(i));
}
return 0;
}
Output:::::
Enter number of terms: 10
Fibonacci Series: 0 1 1 2 3 5 8 13 21 34
6. Count Words, Lines and Characters in a Text File
Write a C program to read a text file and count the total number of words and characters
present in the file.
Co[Link]
#include <stdio.h>
int main()
{
FILE *fp;
char ch;
int chars = 0, words = 0, lines = 0;
int inWord = 0;
fp = fopen("[Link]", "r");
if (fp == NULL)
{
printf("File not found!");
return 0;
}
while ((ch = fgetc(fp)) != EOF)
{
chars++;
if (ch == '\n')
lines++;
if (ch == ' ' || ch == '\n' || ch == '\t')
inWord = 0;
else if (inWord == 0)
{
inWord = 1;
words++;
}
}
fclose(fp);
printf("Characters = %d\n", chars);
printf("Words = %d\n", words);
printf("Lines = %d\n", lines);
return 0;
}
Output:::::::::
Characters = 40
Words = 7
Lines = 2
Intermediate Questions
7. Matrix Multiplication
Write a C program that multiplies two matrices. The matrices can be of any order. The
program should:
Take the order of the matrix from the user.
Determine whether it is possible to multiply the matrices or not
If it is possible print the resultant matrices
Co[Link]
#include <stdio.h>
int main() {
int a[10][10], b[10][10], c[10][10];
int r1, c1, r2, c2;
int i, j, k;
printf("Enter rows and columns of first matrix: ");
scanf("%d %d", &r1, &c1);
printf("Enter rows and columns of second matrix: ");
scanf("%d %d", &r2, &c2);
// Check if multiplication is possible
if (c1 != r2) {
printf("Matrix multiplication not possible!");
return 0;
}
printf("Enter elements of first matrix:\n");
for (i = 0; i < r1; i++) {
for (j = 0; j < c1; j++) {
scanf("%d", &a[i][j]);
}
}
printf("Enter elements of second matrix:\n");
for (i = 0; i < r2; i++) {
for (j = 0; j < c2; j++) {
scanf("%d", &b[i][j]);
}
}
// Multiply matrices
for (i = 0; i < r1; i++) {
for (j = 0; j < c2; j++) {
c[i][j] = 0;
for (k = 0; k < c1; k++) {
c[i][j] += a[i][k] * b[k][j];
}
}
}
// Display result
printf("Resultant matrix:\n");
for (i = 0; i < r1; i++) {
for (j = 0; j < c2; j++) {
printf("%d ", c[i][j]);
}
printf("\n");
}
return 0;
}
Output:::::
Enter rows and columns of first matrix: 2 3
Enter rows and columns of second matrix: 2 3
Matrix multiplication not possible!
8. Print all Prime Numbers in a Range
Write a C program that prints all the prime numbers within a given range. The program
should:
Take the starting and ending numbers of the range from the user.
Identify and display all prime numbers between start and end.
Co[Link]
#include <stdio.h>
int main()
{
int start, end, i, j, isPrime;
printf("Enter the starting number: ");
scanf("%d", &start);
printf("Enter the ending number: ");
scanf("%d", &end);
printf("Prime numbers between %d and %d are:\n", start, end);
for (i = start; i <= end; i++)
{
if (i < 2)
continue;
isPrime = 1;
for (j = 2; j * j <= i; j++)
{
if (i % j == 0)
{
isPrime = 0;
break;
}
}
if (isPrime)
printf("%d ", i);
}
return 0;
}
Output:::::::
Enter the starting number: 1
Enter the ending number: 20
Prime numbers between 1 and 20 are:
2 3 5 7 11 13 17 19
9. Swap Two Numbers Without a Temporary Variable.
Write a C program to swap two integers without using a temporary variable. The
program should:
Take two numbers as input from the user.
Swap the numbers and display it without using any additional variable.
Co[Link]
#include<stdio.h>
int main()
{
int a,b;
printf("enter no.1 : ");
scanf("%d",&a);
printf("enter no.2 : ");
scanf("%d",&b);
a = a + b;
b = a - b;
a = a - b;
printf("After swapping no.1 = %d\n",a);
printf("After swapping no.2 = %d",b);
return 0;
}
Output:::::::
enter no.1 : 4
enter no.2 : 5
After swapping no.1 = 5
After swapping no.2 = 4
10. Reverse a Number
Write a C program to reverse the digits of a given integer without typecasting the
integer. The program should:
Take an integer from the user.
Reverse the integer and display it without using any typecasting.
Co[Link]
#include <stdio.h>
int main()
{
int n, rev = 0, r;
printf("Enter an integer: ");
scanf("%d", &n);
while (n != 0)
{
r = n % 10;
rev = rev * 10 + r;
n = n / 10;
}
printf("Reversed number = %d\n", rev);
return 0;
}
Output:::::::::
Enter an integer: 12345
Reversed number = 54321
11. Write a C program to print a special character series pattern
as shown below. For N=5,
generate a pattern as under:
Co[Link]
#include <stdio.h>
int main()
{
int i, j;
for(i = 1; i <= 5; i++)
{
for(j = i; j < 5; j++)
printf(" ");
for(j = 0; j < i; j++)
printf("%c", 'A' + j);
for(j = i - 2; j >= 0; j--)
printf("%c", 'A' + j);
printf("\n");
}
return 0;
}
Output:::::
12. Write a C program to print a special number series pattern as
shown below. For N=5,
generate a pattern as under:
Co[Link]
#include<stdio.h>
int main()
{
int i,j,n=5;
for(i = n; i >= 1; i--)
{
for(j = n; j > i; j--)
printf(" ");
for(j = 1; j <= i; j++)
printf("* ");
printf("\n");
}
return 0;
}
Output:::::::::::
Easy Questions
13. Find the Largest of Three Numbers
Write a C program that takes three integers as input and prints the largest number
among them.
Co[Link]
#include<stdio.h>
int main()
{
int a,b,c;
printf("Enter the first number : ");
scanf("%d",&a);
printf("Enter the second number : ");
scanf("%d",&b);
printf("Enter the third number : ");
scanf("%d",&c);
if (a>b && a>c)
{
printf("%d is largest number.",a);
}
else if(b>a && b>c)
{
printf("%d is largest number.",b);
}
else
{
printf("%d is largest number.",c);
}
return 0;
}
Output:::::::
Enter the first number : 14
Enter the second number : 14
Enter the third number : 12
any two numbers are same
14. Prime Number Check
Write a C program to check whether a number entered by the user is prime or not.
Co[Link]
#include <stdio.h>
int main()
{
int a, i, count = 0;
printf("Enter a number: ");
scanf("%d", &a);
if (a <= 1)
{
printf("%d is not prime", a);
}
else
{
for (i = 2; i < a; i++)
{
if (a % i == 0)
{
count = 1;
break;
}
}
if (count == 0)
printf("%d is prime", a);
else
printf("%d is not prime", a);
}
return 0;
}
Output:::::
Enter a number: 3
3 is prime
15. Sum of Digits of a Number (Using Loop):
Write a C program that takes an integer input
from the user and calculates the sum of the digits of a given number.
Co[Link]
#include <stdio.h>
int main()
{
int n, s = 0, d;
printf("Enter a number: ");
scanf("%d", &n);
while (n > 0)
{
d = n % 10;
s = s + d;
n = n / 10;
}
printf("Sum of digits = %d", s);
return 0;
}
Output::::::
Enter a number: 12345
Sum of digits = 15
16. Armstrong Number:
Write a C program to check whether a number is an Armstrong
number or not. The program should take an integer input and print whether it is an
Armstrong number or not.
Note: An Armstrong number (also known as a narcissistic number) for a given number of
digits is a number that is equal to the sum of its own digits each raised to the power of
the number of digits.
Example: 153 is an Armstrong number because: 1^3 + 5^3 + 3^3 = 153
Co[Link]
#include <stdio.h>
int main()
{
int n, t, d, len = 0, sum = 0, i, p;
printf("Enter a number: ");
scanf("%d", &n);
t = n;
while (t > 0)
{
len++;
t /= 10;
}
t = n;
while (t > 0)
{
d = t % 10;
p = 1;
for (i = 0; i < len; i++)
p *= d;
sum += p;
t /= 10;
}
if (sum == n)
printf("Armstrong number");
else
printf("Not Armstrong number");
return 0;
}
Output:::::
Enter a number: 153
Armstrong number
Enter a number: 154
Not Armstrong number
17. Matrix Addition and Subtraction
Write a C program to create two 3×3 matrices to store integer values. Perform and
display the results of matrix addition and matrix subtraction operations.
Co[Link]
#include <stdio.h>
int main() {
int a[3][3], b[3][3], i, j;
printf("Enter 9 numbers for matrix1:\n");
for(i = 0; i < 3; i++)
for(j = 0; j < 3; j++)
scanf("%d", &a[i][j]);
printf("Enter 9 numbers for second matrix:\n");
for(i = 0; i < 3; i++)
for(j = 0; j < 3; j++)
scanf("%d", &b[i][j]);
printf("\nAddition of matrices:\n");
for(i = 0; i < 3; i++)
{
for(j = 0; j < 3; j++)
printf("%d ", a[i][j] + b[i][j]);
printf("\n");
}
printf("\nSubtraction of matrices:\n");
for(i = 0; i < 3; i++)
{
for(j = 0; j < 3; j++)
printf("%d ", a[i][j] - b[i][j]);
printf("\n");
}
return 0;
}
Output:::::
Enter 9 numbers for matrix1:
123456789
Enter 9 numbers for second matrix:
123456789
Addition of matrices:
246
8 10 12
14 16 18
Subtraction of matrices:
000
000
000
18. Calculate Factorial Using a Function
Write a C program that defines a function to calculate and return the factorial of a given
number.
Co[Link]
#include <stdio.h>
int factorial(int n)
{
int i, f = 1;
for(i = 1; i <= n; i++)
f = f * i;
return f;
}
int main()
{
int num;
printf("Enter a number: ");
scanf("%d", &num);
printf("Factorial of %d is %d\n", num, factorial(num));
return 0;
}
Output::::::
Enter a number: 5
Factorial of 5 is 120
19. Count Vowels in a String
Write a program to count the number of vowels in a given string.
Co[Link]
#include <stdio.h>
int main() {
char s[100];
int i, v = 0;
printf("Enter a string: ");
gets(s);
for(i = 0; s[i] != '\0'; i++) {
if(s[i]=='a'||s[i]=='e'||s[i]=='i'||s[i]=='o'||s[i]=='u'||
s[i]=='A'||s[i]=='E'||s[i]=='I'||s[i]=='O'||s[i]=='U')
v++;
}
printf("Vowels = %d", v);
return 0;
}
Output::::::
Enter a string: khushi
Vowels = 2
20. Student Information Using Structures
Define a structure to represent a student’s information, including name, roll number,
and marks. Write a C program to:
Create an array of students.
Accept their details from the user.
Display all student information.
Co[Link]
#include <stdio.h>
struct Student
{
char name[50];
int roll;
float marks;
};
int main()
{
int n, i;
printf("Enter number of students: ");
scanf("%d", &n);
struct Student students[n];
for (i = 0; i < n; i++) {
printf("\nEnter details of student %d:\n", i + 1);
printf("Enter name: ");
scanf("%s", students[i].name);
printf("Enter roll number: ");
scanf("%d", &students[i].roll);
printf("Enter marks: ");
scanf("%f", &students[i].marks);
}
printf("\n--- Student Information ---\n");
for (i = 0; i < n; i++) {
printf("\nStudent %d:\n", i + 1);
printf("Name: %s\n", students[i].name);
printf("Roll Number: %d\n", students[i].roll);
printf("Marks: %.2f\n", students[i].marks);
}
return 0;
}
Output::::::::
Enter number of students: 2
Enter details of student 1:
Enter name: khushi
Enter roll number: 1
Enter marks: 10
Enter details of student 2:
Enter name: dattavi
Enter roll number: 2
Enter marks: 11
--- Student Information ---
Student 1:
Name: khushi
Roll Number: 1
Marks: 10.00
Student 2:
Name: dattavi
Roll Number: 2
Marks: 11.00
*****