UNIT 1
3. Check if a number lies between 1 and 100 using logical operators
#include <stdio.h>
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
if (num >= 1 && num <= 100)
printf("Number lies between 1 and 100");
else
printf("Number does not lie between 1 and 100");
return 0;
}
4. Area and perimeter of a circle
#include <stdio.h>
int main() {
float r, area, perimeter;
printf("Enter radius of circle: ");
scanf("%f", &r);
area = 3.14 * r * r;
perimeter = 2 * 3.14 * r;
printf("Area = %.2f\n", area);
printf("Perimeter = %.2f", perimeter);
return 0;
}
5. Area of a circle and perimeter of a triangle
#include <stdio.h>
int main() {
float r, a, b, c, area, perimeter;
printf("Enter radius of circle: ");
scanf("%f", &r);
area = 3.14 * r * r;
printf("Enter sides of triangle: ");
scanf("%f %f %f", &a, &b, &c);
perimeter = a + b + c;
printf("Area of Circle = %.2f\n", area);
printf("Perimeter of Triangle = %.2f", perimeter);
return 0;
}
7. Largest of three numbers using ternary operator
#include <stdio.h>
int main() {
int a, b, c, largest;
printf("Enter three numbers: ");
scanf("%d %d %d", &a, &b, &c);
largest = (a > b) ? ((a > c) ? a : c) : ((b > c) ? b : c);
printf("Largest number = %d", largest);
return 0;
}
8. Swap two numbers using a temporary variable
#include <stdio.h>
int main() {
int a, b, temp;
printf("Enter two numbers: ");
scanf("%d %d", &a, &b);
temp = a;
a = b;
b = temp;
printf("After swapping:\n");
printf("a = %d\nb = %d", a, b);
return 0;
}
UNIT 2
Write a program to check whether a given number is even or odd
#include <stdio.h>
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
if (num % 2 == 0)
printf("The number is Even");
else
printf("The number is Odd");
return 0;
}
3. Vowel or Consonant using switch
#include <stdio.h>
int main() {
char ch;
printf("Enter a character: ");
scanf("%c", &ch);
switch(ch) {
case 'a': case 'e': case 'i': case 'o': case 'u':
case 'A': case 'E': case 'I': case 'O': case 'U':
printf("Vowel");
break;
default:
printf("Consonant");
}
return 0;
}
4. Numbers between 1 and 50 divisible by 7
#include <stdio.h>
int main() {
int i;
printf("Numbers divisible by 7 between 1 and 50: \n");
for(i = 1; i <= 50; i++) {
if(i % 7 == 0)
printf("%d ", i);
}
return 0;
}
5. Electricity bill using else-if ladder
#include <stdio.h>
int main() {
int units;
float bill;
printf("Enter the number of units consumed: ");
scanf("%d", &units);
if(units <= 50)
bill = units * 3;
else if(units <= 150)
bill = 50*3 + (units-50)*5;
else if(units <= 250)
bill = 50*3 + 100*5 + (units-150)*7;
else
bill = 50*3 + 100*5 + 100*7 + (units-250)*10;
printf("Electricity Bill = ₹%.2f", bill);
return 0;
}
7. Factorial using while, do-while, for loops
#include <stdio.h>
int main() {
int n, i;
unsigned long fact = 1;
printf("Enter a number: ");
scanf("%d", &n);
// Using for loop
for(i=1; i<=n; i++)
fact *= i;
printf("Factorial using for loop = %lu\n", fact);
// Reset fact
fact = 1;
i = 1;
// Using while loop
while(i <= n) {
fact *= i;
i++;
}
printf("Factorial using while loop = %lu\n", fact);
// Reset fact
fact = 1;
i = 1;
// Using do-while loop
do {
fact *= i;
i++;
} while(i <= n);
printf("Factorial using do-while loop = %lu\n", fact);
return 0;
}
8. Fibonacci series using while, do-while, for loops
#include <stdio.h>
int main() {
int n, i, t1=0, t2=1, next;
printf("Enter number of terms: ");
scanf("%d", &n);
printf("Fibonacci using for loop: ");
for(i=1; i<=n; i++) {
printf("%d ", t1);
next = t1 + t2;
t1 = t2;
t2 = next;
}
printf("\n");
// Reset for while loop
t1 = 0; t2 = 1; i=1;
printf("Fibonacci using while loop: ");
while(i <= n) {
printf("%d ", t1);
next = t1 + t2;
t1 = t2;
t2 = next;
i++;
}
printf("\n");
// Reset for do-while loop
t1 = 0; t2 = 1; i=1;
printf("Fibonacci using do-while loop: ");
if(n > 0) {
do {
printf("%d ", t1);
next = t1 + t2;
t1 = t2;
t2 = next;
i++;
} while(i <= n);
}
printf("\n");
return 0;
}
9. Sum of first n numbers
#include <stdio.h>
int main() {
int n, sum=0, i;
printf("Enter n: ");
scanf("%d", &n);
for(i=1; i<=n; i++)
sum += i;
printf("Sum = %d", sum);
return 0;
}
10. Check palindrome number
#include <stdio.h>
int main() {
int num, rev=0, temp, digit;
printf("Enter a number: ");
scanf("%d", &num);
temp = num;
while(temp != 0) {
digit = temp % 10;
rev = rev*10 + digit;
temp /= 10;
}
if(rev == num)
printf("Number is a palindrome");
else
printf("Number is not a palindrome");
return 0;
}
11. Leap year check
#include <stdio.h>
int main() {
int year;
printf("Enter year: ");
scanf("%d", &year);
if((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
printf("Leap year");
else
printf("Not a leap year");
return 0;
}
12. Sum of digits and occurrence of a digit
#include <stdio.h>
int main() {
int num, sum=0, digit, count=0, search;
printf("Enter number: ");
scanf("%d", &num);
printf("Enter digit to find occurrence: ");
scanf("%d", &search);
int temp = num;
while(temp != 0) {
digit = temp % 10;
sum += digit;
if(digit == search)
count++;
temp /= 10;
}
printf("Sum of digits = %d\n", sum);
printf("Occurrence of %d = %d", search, count);
return 0;
}
13. GCD and LCM using Euclid’s method
#include <stdio.h>
int main() {
int a, b, x, y, gcd, lcm;
printf("Enter two numbers: ");
scanf("%d %d", &a, &b);
x = a; y = b;
while(y != 0) {
int temp = y;
y = x % y;
x = temp;
}
gcd = x;
lcm = (a*b)/gcd;
printf("GCD = %d\n", gcd);
printf("LCM = %d", lcm);
return 0;
}
UNIT 3
1. Linear Search
#include <stdio.h>
int main() {
int n, i, key, found = 0;
printf("Enter number of elements: ");
scanf("%d", &n);
int arr[n];
printf("Enter elements: ");
for(i=0; i<n; i++)
scanf("%d", &arr[i]);
printf("Enter key to search: ");
scanf("%d", &key);
for(i=0; i<n; i++) {
if(arr[i] == key) {
printf("Element found at position %d\n", i+1);
found = 1;
break;
}
}
if(!found)
printf("Element not found");
return 0;
}
2. Binary Search (sorted array)
#include <stdio.h>
int main() {
int n, i, key, low, high, mid, found=0;
printf("Enter number of elements: ");
scanf("%d", &n);
int arr[n];
printf("Enter elements in sorted order: ");
for(i=0; i<n; i++)
scanf("%d", &arr[i]);
printf("Enter key to search: ");
scanf("%d", &key);
low = 0; high = n-1;
while(low <= high) {
mid = (low + high)/2;
if(arr[mid] == key) {
printf("Element found at position %d\n", mid+1);
found = 1;
break;
} else if(arr[mid] < key)
low = mid + 1;
else
high = mid - 1;
}
if(!found)
printf("Element not found");
return 0;
}
3. Selection Sort (Ascending)
#include <stdio.h>
int main() {
int n, i, j, min_idx, temp;
printf("Enter number of elements: ");
scanf("%d", &n);
int arr[n];
printf("Enter elements: ");
for(i=0; i<n; i++)
scanf("%d", &arr[i]);
for(i=0; i<n-1; i++) {
min_idx = i;
for(j=i+1; j<n; j++) {
if(arr[j] < arr[min_idx])
min_idx = j;
}
temp = arr[i];
arr[i] = arr[min_idx];
arr[min_idx] = temp;
}
printf("Sorted array: ");
for(i=0; i<n; i++)
printf("%d ", arr[i]);
return 0;
}
4. Bubble Sort
#include <stdio.h>
int main() {
int n, i, j, temp;
printf("Enter number of elements: ");
scanf("%d", &n);
int arr[n];
printf("Enter elements: ");
for(i=0; i<n; i++)
scanf("%d", &arr[i]);
for(i=0; i<n-1; i++) {
for(j=0; j<n-i-1; j++) {
if(arr[j] > arr[j+1]) {
temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
printf("Sorted array: ");
for(i=0; i<n; i++)
printf("%d ", arr[i]);
return 0;
}
5. Sum of two matrices
#include <stdio.h>
int main() {
int r, c, i, j;
printf("Enter rows and columns: ");
scanf("%d %d", &r, &c);
int A[r][c], B[r][c], sum[r][c];
printf("Enter elements of matrix A:\n");
for(i=0; i<r; i++)
for(j=0; j<c; j++)
scanf("%d", &A[i][j]);
printf("Enter elements of matrix B:\n");
for(i=0; i<r; i++)
for(j=0; j<c; j++)
scanf("%d", &B[i][j]);
for(i=0; i<r; i++)
for(j=0; j<c; j++)
sum[i][j] = A[i][j] + B[i][j];
printf("Sum of matrices:\n");
for(i=0; i<r; i++) {
for(j=0; j<c; j++)
printf("%d ", sum[i][j]);
printf("\n");
}
return 0;
}
6. Multiply two matrices
#include <stdio.h>
int main() {
int r1, c1, r2, c2, i, j, k;
printf("Enter rows and columns of matrix A: ");
scanf("%d %d", &r1, &c1);
printf("Enter rows and columns of matrix B: ");
scanf("%d %d", &r2, &c2);
if(c1 != r2) {
printf("Matrix multiplication not possible.");
return 0;
}
int A[r1][c1], B[r2][c2], C[r1][c2];
printf("Enter elements of matrix A:\n");
for(i=0; i<r1; i++)
for(j=0; j<c1; j++)
scanf("%d", &A[i][j]);
printf("Enter elements of matrix B:\n");
for(i=0; i<r2; i++)
for(j=0; j<c2; j++)
scanf("%d", &B[i][j]);
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];
}
}
printf("Product of matrices:\n");
for(i=0; i<r1; i++) {
for(j=0; j<c2; j++)
printf("%d ", C[i][j]);
printf("\n");
}
return 0;
}
7. Binary Search in sorted array (same as question 2)
8. Transpose of a matrix
#include <stdio.h>
int main() {
int r, c, i, j;
printf("Enter rows and columns: ");
scanf("%d %d", &r, &c);
int A[r][c], T[c][r];
printf("Enter elements of matrix:\n");
for(i=0; i<r; i++)
for(j=0; j<c; j++)
scanf("%d", &A[i][j]);
for(i=0; i<r; i++)
for(j=0; j<c; j++)
T[j][i] = A[i][j];
printf("Transpose of matrix:\n");
for(i=0; i<c; i++) {
for(j=0; j<r; j++)
printf("%d ", T[i][j]);
printf("\n");
}
return 0;
}
9. Reverse a string without strrev()
#include <stdio.h>
#include <string.h>
int main() {
char str[100];
int i, len;
printf("Enter a string: ");
gets(str);
len = strlen(str);
printf("Reversed string: ");
for(i=len-1; i>=0; i--)
printf("%c", str[i]);
return 0;
}
11. Check string palindrome
#include <stdio.h>
#include <string.h>
int main() {
char str[100];
int i, len, flag=1;
printf("Enter a string: ");
gets(str);
len = strlen(str);
for(i=0; i<len/2; i++) {
if(str[i] != str[len-i-1]) {
flag = 0;
break;
}
}
if(flag)
printf("Palindrome");
else
printf("Not a palindrome");
return 0;
}
12. Length of string without strlen()
#include <stdio.h>
int main() {
char str[100];
int len=0;
printf("Enter a string: ");
gets(str);
while(str[len] != '\0')
len++;
printf("Length = %d", len);
return 0;
}
13. Concatenate strings without strcat()
#include <stdio.h>
int main() {
char str1[100], str2[100];
int i, j;
printf("Enter first string: ");
gets(str1);
printf("Enter second string: ");
gets(str2);
for(i=0; str1[i] != '\0'; i++); // move to end of str1
for(j=0; str2[j] != '\0'; j++, i++)
str1[i] = str2[j];
str1[i] = '\0';
printf("Concatenated string: %s", str1);
return 0;
}
14. Count vowels in a string
#include <stdio.h>
#include <string.h>
int main() {
char str[100];
int i, count=0;
printf("Enter a string: ");
gets(str);
for(i=0; str[i]!='\0'; 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')
count++;
}
printf("Number of vowels = %d", count);
return 0;
}
UNIT 4
a) Sum of array elements using pointers
#include <stdio.h>
int main() {
int n, i, sum = 0;
printf("Enter number of elements: ");
scanf("%d", &n);
int arr[n];
int *ptr = arr;
printf("Enter elements: ");
for(i=0; i<n; i++)
scanf("%d", ptr + i); // using pointer
for(i=0; i<n; i++)
sum += *(ptr + i); // sum using pointer
printf("Sum of elements = %d", sum);
return 0;
}
MODEL PAPER 7 B
b) Search for an element in an array using pointers
#include <stdio.h>
int main() {
int n, i, key, found=0;
printf("Enter number of elements: ");
scanf("%d", &n);
int arr[n];
int *ptr = arr;
printf("Enter elements: ");
for(i=0; i<n; i++)
scanf("%d", ptr + i);
printf("Enter element to search: ");
scanf("%d", &key);
for(i=0; i<n; i++) {
if(*(ptr + i) == key) {
printf("Element found at position %d\n", i+1);
found = 1;
break;
}
}
if(!found)
printf("Element not found\n");
return 0;
}
MODEL PAPER 8 B
Call by Value example
#include <stdio.h>
void increment(int a) {
a = a + 1;
printf("Inside function: a = %d\n", a);
}
int main() {
int num = 5;
printf("Before function call: num = %d\n", num);
increment(num); // Call by value
printf("After function call: num = %d\n", num);
return 0;
}
Explanation: The value of num in main does not change because only a
copy is passed.
Call by Reference example
#include <stdio.h>
void increment(int *a) {
*a = *a + 1;
printf("Inside function: a = %d\n", *a);
}
int main() {
int num = 5;
printf("Before function call: num = %d\n", num);
increment(&num); // Call by reference
printf("After function call: num = %d\n", num);
return 0;
}
MODEL PAPER 8 C
Nth Fibonacci number using recursion
#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;
printf("Enter the term (n): ");
scanf("%d", &n);
printf("Fibonacci term %d = %d", n, fibonacci(n));
return 0;
}
UNIT 5
1. Store and display details of 5 students using array of
structures
#include <stdio.h>
struct Student {
char name[50];
int age;
float marks;
};
int main() {
struct Student s[5];
int i;
// Input details
for(i=0; i<5; i++) {
printf("Enter name of student %d: ", i+1);
scanf("%s", s[i].name);
printf("Enter age of student %d: ", i+1);
scanf("%d", &s[i].age);
printf("Enter marks of student %d: ", i+1);
scanf("%f", &s[i].marks);
}
// Display details
printf("\nStudent Details:\n");
for(i=0; i<5; i++) {
printf("Name: %s, Age: %d, Marks: %.2f\n", s[i].name,
s[i].age, s[i].marks);
}
return 0;
}
2. Pass a structure to function to calculate average marks
#include <stdio.h>
struct Student {
char name[50];
int marks[3];
};
float calculateAverage(struct Student s) {
return ([Link][0] + [Link][1] + [Link][2]) / 3.0;
}
int main() {
struct Student s;
int i;
printf("Enter student name: ");
scanf("%s", [Link]);
printf("Enter marks in 3 subjects: ");
for(i=0; i<3; i++)
scanf("%d", &[Link][i]);
printf("Average marks of %s = %.2f", [Link], calculateAverage(s));
return 0;
}
3. Structure Employee
#include <stdio.h>
struct Employee {
char name[50];
char branch[50];
int experience;
};
int main() {
struct Employee e[2];
int i;
for(i=0; i<2; i++) {
printf("Enter name of employee %d: ", i+1);
scanf("%s", e[i].name);
printf("Enter branch: ");
scanf("%s", e[i].branch);
printf("Enter experience (years): ");
scanf("%d", &e[i].experience);
}
printf("\nEmployee Details:\n");
for(i=0; i<2; i++) {
printf("Name: %s, Branch: %s, Experience: %d years\n",
e[i].name, e[i].branch, e[i].experience);
}
return 0;
}
4. Copy contents of one file into another
#include <stdio.h>
int main() {
FILE *source, *dest;
char ch;
source = fopen("[Link]", "r");
if(source == NULL) {
printf("Unable to open source file.\n");
return 1;
}
dest = fopen("[Link]", "w");
if(dest == NULL) {
printf("Unable to open destination file.\n");
return 1;
}
while((ch = fgetc(source)) != EOF) {
fputc(ch, dest);
}
printf("File copied successfully.\n");
fclose(source);
fclose(dest);
return 0;
}
5. Count frequency of vowels in a file
#include <stdio.h>
int main() {
FILE *file;
char ch;
int a=0, e=0, i=0, o=0, u=0;
file = fopen("[Link]", "r");
if(file == NULL) {
printf("Unable to open file.\n");
return 1;
}
while((ch = fgetc(file)) != EOF) {
switch(ch) {
case 'a': case 'A': a++; break;
case 'e': case 'E': e++; break;
case 'i': case 'I': i++; break;
case 'o': case 'O': o++; break;
case 'u': case 'U': u++; break;
}
}
printf("Vowel counts:\nA: %d\nE: %d\nI: %d\nO: %d\nU: %d\n", a, e,
i, o, u);
fclose(file);
return 0;
}
6. Copy contents of one file to another file (same as Q4)
7. Reading and writing using fprintf() and fscanf()
#include <stdio.h>
int main() {
FILE *file;
int n, i, num;
// Writing to file
file = fopen("[Link]", "w");
if(file == NULL) {
printf("Unable to open file.\n");
return 1;
}
printf("How many numbers to write: ");
scanf("%d", &n);
for(i=0; i<n; i++) {
printf("Enter number %d: ", i+1);
scanf("%d", &num);
fprintf(file, "%d\n", num); // write using fprintf
}
fclose(file);
// Reading from file
file = fopen("[Link]", "r");
if(file == NULL) {
printf("Unable to open file.\n");
return 1;
}
printf("\nNumbers in file:\n");
while(fscanf(file, "%d", &num) != EOF) { // read using fscanf
printf("%d ", num);
}
fclose(file);
return 0;
}