0% found this document useful (0 votes)
35 views

C Programming

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views

C Programming

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 35

Q1: Greatest of three numbers using ternary operator

#include <stdio.h>

int main() {
int num1, num2, num3, greatest;

printf("Enter three numbers: ");


scanf("%d %d %d", &num1, &num2, &num3);

greatest = (num1 > num2) ? ((num1 > num3) ? num1 :


num3) : ((num2 > num3) ? num2 : num3);

printf("The greatest number is: %d\n", greatest);


return 0;
}
Output Example

Enter three numbers: 10 20 15


The greatest number is: 20

Q2: Convert Fahrenheit to Celsius

#include <stdio.h>

int main() {
float fahrenheit, celsius;

printf("Enter temperature in Fahrenheit: ");


scanf("%f", &fahrenheit);

celsius = (fahrenheit - 32) * 5 / 9;

printf("Temperature in Celsius: %.2f\n", celsius);


return 0;
}
Output Example

Enter temperature in Fahrenheit: 98.6


Temperature in Celsius: 37.00
Q3: Meter reading and billed amount#include <stdio.h>

#include <stdio.h>

int main() {
int units;
float bill = 0;

printf("Enter meter reading (units): ");


scanf("%d", &units);

if (units <= 100) {


bill = units * 1.75;
} else if (units <= 200) {
bill = 100 * 1.75 + (units - 100) * 3.50;
} else {
bill = 100 * 1.75 + 100 * 3.50 + (units - 200) *
5.20;
}

printf("Total bill amount: Rs %.2f\n", bill);


return 0;
}
Output Example

Enter meter reading (units): 250


Total bill amount: Rs 790.00

Q4: Character case conversion

#include <stdio.h>
#include <ctype.h>

int main() {
char ch;

printf("Enter a character: ");


scanf(" %c", &ch);

if (islower(ch))
printf("Uppercase: %c\n", toupper(ch));
else if (isupper(ch))
printf("Lowercase: %c\n", tolower(ch));
else
printf("Not an alphabet character.\n");
return 0;
}
Output Example

Enter a character: a
Uppercase: A

Enter a character: Z
Lowercase: z

Q5: Check even or odd

#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;
}
Output Example

Enter a number: 4
4 is Even.

Q6: Menu-driven program for calculating area

#include <stdio.h>
#include <math.h>

int main() {
int choice;
float base, height, radius, length, breadth, side, area;

printf("Choose the shape to calculate area:\n");


printf("1. Triangle\n2. Circle\n3. Rectangle\n4.
Square\n");
printf("Enter your choice (1-4): ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter base and height of the triangle:
");
scanf("%f %f", &base, &height);
area = 0.5 * base * height;
printf("Area of Triangle: %.2f\n", area);
break;
case 2:
printf("Enter radius of the circle: ");
scanf("%f", &radius);
area = M_PI * radius * radius;
printf("Area of Circle: %.2f\n", area);
break;
case 3:
printf("Enter length and breadth of the
rectangle: ");
scanf("%f %f", &length, &breadth);
area = length * breadth;
printf("Area of Rectangle: %.2f\n", area);
break;
case 4:
printf("Enter side of the square: ");
scanf("%f", &side);
area = side * side;
printf("Area of Square: %.2f\n", area);
break;
default:
printf("Invalid choice!\n");
}

return 0;
}
Output Example

Choose the shape to calculate area:


1. Triangle
2. Circle
3. Rectangle
4. Square
Enter your choice (1-4): 2
Enter radius of the circle: 5
Area of Circle: 78.54
Q7: Leap year check

#include <stdio.h>

int main() {
int year;

printf("Enter a year: ");


scanf("%d", &year);

if ((year % 4 == 0 && year % 100 != 0) || (year % 400 ==


0))
printf("%d is a Leap Year.\n", year);
else
printf("%d is not a Leap Year.\n", year);

return 0;
}
Output Example

Enter a year: 2024


2024 is a Leap Year.

Q8: Swap two variables without a third variable

#include <stdio.h>

int main() {
int a, b;

printf("Enter two numbers (a and b): ");


scanf("%d %d", &a, &b);

printf("Before swapping: a = %d, b = %d\n", a, b);

a = a + b;
b = a - b;
a = a - b;

printf("After swapping: a = %d, b = %d\n", a, b);


return 0;
}
Output Example

Enter two numbers (a and b): 5 10


Before swapping: a = 5, b = 10
After swapping: a = 10, b = 5

Q9: Swap two variables using a third variable

#include <stdio.h>

int main() {
int a, b, temp;

printf("Enter two numbers (a and b): ");


scanf("%d %d", &a, &b);

printf("Before swapping: a = %d, b = %d\n", a, b);

temp = a;
a = b;
b = temp;

printf("After swapping: a = %d, b = %d\n", a, b);


return 0;
}
Output Example

Enter two numbers (a and b): 5 10


Before swapping: a = 5, b = 10
After swapping: a = 10, b = 5

Q10: Calculate simple interest

#include <stdio.h>

int main() {
float principal, rate, time, interest;

printf("Enter principal amount: ");


scanf("%f", &principal);
printf("Enter annual interest rate (%%): ");
scanf("%f", &rate);
printf("Enter time in years: ");
scanf("%f", &time);

interest = (principal * rate * time) / 100;

printf("Simple Interest: %.2f\n", interest);


return 0;
}
Output Example

Enter principal amount: 1000


Enter annual interest rate (%): 5
Enter time in years: 2
Simple Interest: 100.00

Q11: Number of days in a month using switch case

#include <stdio.h>

int main() {
int month;

printf("Enter month number (1-12): ");


scanf("%d", &month);

switch (month) {
case 1: case 3: case 5: case 7: case 8: case 10: case
12:
printf("31 days\n");
break;
case 4: case 6: case 9: case 11:
printf("30 days\n");
break;
case 2:
printf("28 or 29 days (depending on leap year)
\n");
break;
default:
printf("Invalid month number!\n");
}

return 0;
}
Output Example

Enter month number (1-12): 2


28 or 29 days (depending on leap year)
Q12: Check if a person is eligible to vote

#include <stdio.h>

int main() {
int age;

printf("Enter your age: ");


scanf("%d", &age);

if (age >= 18)


printf("You are eligible to vote.\n");
else
printf("You are not eligible to vote.\n");

return 0;
}
Output Example

Enter your age: 20


You are eligible to vote.

Q13: Demonstration of constants

#include <stdio.h>

#define PI 3.14159

int main() {
float radius, area;

printf("Enter radius of the circle: ");


scanf("%f", &radius);

area = PI * radius * radius;

printf("Area of the circle: %.2f\n", area);


return 0;
}
Output Example

Enter radius of the circle: 5


Area of the circle: 78.54
Q14: Calculate factorial of a number

#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 of %d is %d\n", num, factorial);


return 0;
}
Output Example

Enter a number: 5
Factorial of 5 is 120

Q15: Check input character type

#include <stdio.h>
#include <ctype.h>

int main() {
char ch;

printf("Enter a character: ");


scanf(" %c", &ch);

if (isdigit(ch))
printf("Character is a number.\n");
else if (isalpha(ch))
printf("Character is an alphabet.\n");
else
printf("Character is a special character.\n");

return 0;
}
Output Example

Enter a character: @
Character is a special character.
Q16: Compute grade point average and academic standing

#include <stdio.h>

int main() {
int a, b, c, d, f;
float total_points, gpa;

printf("Enter the number of grades (A, B, C, D, F): ");


scanf("%d %d %d %d %d", &a, &b, &c, &d, &f);

total_points = (4.0 * a) + (3.0 * b) + (2.0 * c) + (1.0 *


d) + (0.0 * f);
gpa = total_points / (a + b + c + d + f);

printf("GPA: %.2f\n", gpa);

if (gpa >= 3.51)


printf("Academic Standing: High Honors\n");
else if (gpa >= 3.00)
printf("Academic Standing: Honors\n");
else if (gpa >= 2.00)
printf("Academic Standing: Satisfactory\n");
else
printf("Academic Standing: Probation\n");

return 0;
}
Output Example

Enter the number of grades (A, B, C, D, F): 3 2 1 0 0


GPA: 3.33
Academic Standing: Honors

Q17: Income tax calculation

#include <stdio.h>

int main() {
float income, tax = 0;

printf("Enter your annual income: ");


scanf("%f", &income);

if (income > 35000) {


tax += (income - 35000) * 0.20;
income = 35000;
}
if (income > 20000) {
tax += (income - 20000) * 0.15;
income = 20000;
}
if (income > 10000) {
tax += (income - 10000) * 0.10;
income = 10000;
}

printf("Total tax owed: %.2f\n", tax);


return 0;
}
Output Example

Enter your annual income: 38000


Total tax owed: 4600.00

Q18: Area of triangle using Heron’s formula

#include <stdio.h>
#include <math.h>

int main() {
float a, b, c, s, area;

printf("Enter the sides of the triangle (a, b, c): ");


scanf("%f %f %f", &a, &b, &c);

s = (a + b + c) / 2;
area = sqrt(s * (s - a) * (s - b) * (s - c));

printf("Area of the triangle: %.2f\n", area);


return 0;
}
Output Example

Enter the sides of the triangle (a, b, c): 3 4 5


Area of the triangle: 6.00
Q19: Calculate area and perimeter of a circle

#include <stdio.h>
#define PI 3.14159

int main() {
float radius, area, perimeter;

printf("Enter the radius of the circle: ");


scanf("%f", &radius);

area = PI * radius * radius;


perimeter = 2 * PI * radius;

printf("Area of the circle: %.2f\n", area);


printf("Perimeter of the circle: %.2f\n", perimeter);
return 0;
}
Output Example

Enter the radius of the circle: 5


Area of the circle: 78.54
Perimeter of the circle: 31.42

Q20: Count even/odd integers and calculate averages

#include <stdio.h>

int main() {
int num, even_count = 0, odd_count = 0;
float even_sum = 0, odd_sum = 0;

printf("Enter integers (0 to stop):\n");

while (1) {
scanf("%d", &num);
if (num == 0)
break;

if (num % 2 == 0) {
even_count++;
even_sum += num;
} else {
odd_count++;
odd_sum += num;
}
}

printf("Total even numbers: %d\n", even_count);


printf("Total odd numbers: %d\n", odd_count);

if (even_count > 0)
printf("Average of even numbers: %.2f\n", even_sum /
even_count);
else
printf("No even numbers entered.\n");

if (odd_count > 0)
printf("Average of odd numbers: %.2f\n", odd_sum /
odd_count);
else
printf("No odd numbers entered.\n");

return 0;
}
Output Example

Enter integers (0 to stop):


12 7 5 8 0
Total even numbers: 2
Total odd numbers: 2
Average of even numbers: 10.00
Average of odd numbers: 6.00

Q21: Check if a number is prime

#include <stdio.h>

int main() {
int num, i, is_prime = 1;

printf("Enter a number: ");


scanf("%d", &num);

if (num <= 1) {
is_prime = 0;
} else {
for (i = 2; i <= num / 2; i++) {
if (num % i == 0) {
is_prime = 0;
break;
}
}
}

if (is_prime)
printf("%d is a prime number.\n", num);
else
printf("%d is not a prime number.\n", num);

return 0;
}
Output Example

Enter a number: 7
7 is a prime number.

Q22: Check if a number is perfect

#include <stdio.h>

int main() {
int num, sum = 0;

printf("Enter a number: ");


scanf("%d", &num);

for (int i = 1; i <= num / 2; i++) {


if (num % i == 0)
sum += i;
}

if (sum == num)
printf("%d is a perfect number.\n", num);
else
printf("%d is not a perfect number.\n", num);

return 0;
}
Output Example

Enter a number: 28
28 is a perfect number.
Q23: Calculate BMI

#include <stdio.h>

int main() {
float weight, height, bmi;

printf("Enter weight in pounds: ");


scanf("%f", &weight);
printf("Enter height in inches: ");
scanf("%f", &height);

weight *= 0.45359237; // Convert pounds to kilograms


height *= 0.0254; // Convert inches to meters

bmi = weight / (height * height);

printf("Your BMI is: %.2f\n", bmi);

return 0;
}
Output Example

Enter weight in pounds: 150


Enter height in inches: 68
Your BMI is: 22.83

Q24: Reverse a number

#include <stdio.h>

int main() {
int num, reverse = 0, digit;

printf("Enter a number: ");


scanf("%d", &num);

while (num != 0) {
digit = num % 10;
reverse = reverse * 10 + digit;
num /= 10;
}

printf("Reversed number: %d\n", reverse);


return 0;
}
Output Example

Enter a number: 1234


Reversed number: 4321

Q25: Convert a number to words

#include <stdio.h>

void print_digit_word(int digit) {


switch (digit) {
case 0: printf("Zero "); break;
case 1: printf("One "); break;
case 2: printf("Two "); break;
case 3: printf("Three "); break;
case 4: printf("Four "); break;
case 5: printf("Five "); break;
case 6: printf("Six "); break;
case 7: printf("Seven "); break;
case 8: printf("Eight "); break;
case 9: printf("Nine "); break;
}
}

int main() {
int num, digit, reverse = 0;

printf("Enter a number: ");


scanf("%d", &num);

// Reverse the number first


while (num != 0) {
digit = num % 10;
reverse = reverse * 10 + digit;
num /= 10;
}

// Convert each digit to word


while (reverse != 0) {
digit = reverse % 10;
print_digit_word(digit);
reverse /= 10;
}

printf("\n");
return 0;
}
Output Example

Enter a number: 1234


One Two Three Four

Q26: Convert a large number to words

#include <stdio.h>
#include <string.h>

// Function to print the word equivalent of a number


void convert_to_words(int num) {
char *ones[] = {"", "One", "Two", "Three", "Four",
"Five", "Six", "Seven", "Eight", "Nine"};
char *tens[] = {"", "", "Twenty", "Thirty", "Forty",
"Fifty", "Sixty", "Seventy", "Eighty", "Ninety"};
char *teens[] = {"Ten", "Eleven", "Twelve", "Thirteen",
"Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen",
"Nineteen"};

if (num >= 1000) {


printf("%s Thousand ", ones[num / 1000]);
num %= 1000;
}

if (num >= 100) {


printf("%s Hundred ", ones[num / 100]);
num %= 100;
}

if (num >= 20) {


printf("%s ", tens[num / 10]);
num %= 10;
} else if (num >= 10) {
printf("%s ", teens[num - 10]);
num = 0;
}

if (num > 0) {
printf("%s ", ones[num]);
}
}

int main() {
int num;

printf("Enter a number (max 5 digits): ");


scanf("%d", &num);

if (num < 1 || num > 99999) {


printf("Invalid input! Please enter a number between
1 and 99999.\n");
return 1;
}

convert_to_words(num);
printf("Only\n");

return 0;
}
Output Example

Enter a number (max 5 digits): 78935


Seventy Eight Thousand Nine Hundred Thirty Five Only

Q27: Check if a character is lowercase without library functions

#include <stdio.h>

int main() {
char ch;

printf("Enter a character: ");


scanf(" %c", &ch);

if (ch >= 'a' && ch <= 'z')


printf("The character '%c' is a lowercase letter.\n",
ch);
else
printf("The character '%c' is not a lowercase letter.
\n", ch);

return 0;
}
Output Example

Enter a character: g
The character 'g' is a lowercase letter.
Q28: Factorial using recursion

#include <stdio.h>

long long factorial(int n) {


if (n == 0 || n == 1)
return 1;
else
return n * factorial(n - 1);
}

int main() {
int num;

printf("Enter a number: ");


scanf("%d", &num);

if (num < 0)
printf("Factorial is not defined for negative
numbers.\n");
else
printf("Factorial of %d is %lld.\n", num,
factorial(num));

return 0;
}
Output Example

Enter a number: 5
Factorial of 5 is 120.

Q29: HCF and LCM using user-de ned functions

#include <stdio.h>

// Function to calculate HCF


int find_hcf(int a, int b) {
if (b == 0)
return a;
return find_hcf(b, a % b);
}

// Function to calculate LCM


int find_lcm(int a, int b, int hcf) {
return (a * b) / hcf;
}
fi
int main() {
int num1, num2, hcf, lcm;

printf("Enter two numbers: ");


scanf("%d %d", &num1, &num2);

hcf = find_hcf(num1, num2);


lcm = find_lcm(num1, num2, hcf);

printf("HCF of %d and %d is %d.\n", num1, num2, hcf);


printf("LCM of %d and %d is %d.\n", num1, num2, lcm);

return 0;
}
Output Example

Enter two numbers: 12 15


HCF of 12 and 15 is 3.
LCM of 12 and 15 is 60.

Q30: Fibonacci series 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 limit, i;

printf("Enter the limit: ");


scanf("%d", &limit);

printf("Fibonacci series: ");


for (i = 0; i < limit; i++) {
printf("%d ", fibonacci(i));
}

printf("\n");
return 0;
}
Output Example

Enter the limit: 7


Fibonacci series: 0 1 1 2 3 5 8

Q31: Power Calculation using Recursion

#include <stdio.h>

// Recursive function to calculate power


int power(int base, int exp) {
if (exp == 0)
return 1;
return base * power(base, exp - 1);
}

int main() {
int base, exp;

printf("Enter base and exponent: ");


scanf("%d %d", &base, &exp);

printf("%d raised to the power of %d is %d.\n", base,


exp, power(base, exp));
return 0;
}
Output Example

Enter base and exponent: 2 5


2 raised to the power of 5 is 32.

Q32: Check if two numbers are amicable

#include <stdio.h>

// Function to calculate the sum of proper divisors


int sum_of_divisors(int num) {
int sum = 1; // 1 is a proper divisor of every number
for (int i = 2; i <= num / 2; i++) {
if (num % i == 0)
sum += i;
}
return sum;
}
int main() {
int num1, num2;

printf("Enter two numbers: ");


scanf("%d %d", &num1, &num2);

if (sum_of_divisors(num1) == num2 &&


sum_of_divisors(num2) == num1)
printf("%d and %d are amicable numbers.\n", num1,
num2);
else
printf("%d and %d are not amicable numbers.\n", num1,
num2);

return 0;
}
Output Example

Enter two numbers: 220 284


220 and 284 are amicable numbers.

Q33: Roots of a Quadratic Equation

#include <stdio.h>
#include <math.h>

int main() {
double a, b, c, discriminant, root1, root2;

printf("Enter coefficients a, b, and c: ");


scanf("%lf %lf %lf", &a, &b, &c);

discriminant = b * b - 4 * a * c;

if (discriminant > 0) {
root1 = (-b + sqrt(discriminant)) / (2 * a);
root2 = (-b - sqrt(discriminant)) / (2 * a);
printf("Roots are real and distinct: %.2lf and
%.2lf\n", root1, root2);
} else if (discriminant == 0) {
root1 = -b / (2 * a);
printf("Roots are real and equal: %.2lf\n", root1);
} else {
double realPart = -b / (2 * a);
double imaginaryPart = sqrt(-discriminant) / (2 * a);
printf("Roots are complex: %.2lf + %.2lfi and %.2lf -
%.2lfi\n", realPart, imaginaryPart, realPart, imaginaryPart);
}

return 0;
}
Output Example

Enter coefficients a, b, and c: 1 -3 2


Roots are real and distinct: 2.00 and 1.00

Q34: Convert a Number to Binary

#include <stdio.h>

void convert_to_binary(int num) {


if (num > 1)
convert_to_binary(num / 2);
printf("%d", num % 2);
}

int main() {
int num;

printf("Enter a number: ");


scanf("%d", &num);

printf("Binary representation: ");


convert_to_binary(num);
printf("\n");

return 0;
}
Output Example

Enter a number: 10
Binary representation: 1010

Q35: Compound Interest Calculation Function

#include <stdio.h>
#include <math.h>

// Function to calculate compound interest


float comp_int_calc(float int_amt, float rate, int years) {
return int_amt * pow(1 + rate / 100, years) - int_amt;
}

int main() {
float principal, rate, interest;
int years;

printf("Enter principal amount: ");


scanf("%f", &principal);
printf("Enter annual interest rate (%%): ");
scanf("%f", &rate);
printf("Enter number of years: ");
scanf("%d", &years);

interest = comp_int_calc(principal, rate, years);


printf("Compound interest is: %.2f\n", interest);

return 0;
}
Output Example

Enter principal amount: 1000


Enter annual interest rate (%): 5
Enter number of years: 2
Compound interest is: 102.50

Q36: Surface Area, Volume, and Space Diagonal of Cuboids

#include <stdio.h>
#include <math.h>

int main() {
float length, width, height, surfaceArea, volume,
diagonal;

printf("Enter length, width, and height of the cuboid:


");
scanf("%f %f %f", &length, &width, &height);

surfaceArea = 2 * (length * width + width * height +


height * length);
volume = length * width * height;
diagonal = sqrt(length * length + width * width + height
* height);

printf("Surface Area: %.2f\n", surface Area);


printf("Volume: %.2f\n", volume);
printf("Space Diagonal: %.2f\n", diagonal);

return 0;
}
Output Example

Enter length, width, and height of the cuboid: 5 3 4


Surface Area: 94.00
Volume: 60.00
Space Diagonal: 7.07

Q37: Macros for MIN and MAX

#include <stdio.h>

#define MIN(a, b) ((a) < (b) ? (a) : (b))


#define MAX(a, b) ((a) > (b) ? (a) : (b))

int main() {
int num1, num2;

printf("Enter two numbers: ");


scanf("%d %d", &num1, &num2);

printf("Minimum of %d and %d is %d\n", num1, num2,


MIN(num1, num2));
printf("Maximum of %d and %d is %d\n", num1, num2,
MAX(num1, num2));

return 0;
}
Output Example

Enter two numbers: 5 10


Minimum of 5 and 10 is 5
Maximum of 5 and 10 is 10

Q38: Inverting n Bits Starting at Position p

#include <stdio.h>

// Function to invert n bits starting at position p


int invert(int x, int p, int n) {
int mask = (~(~0 << n)) << (p - n + 1);
return x ^ mask;
}
int main() {
int x, p, n;

printf("Enter the number (x): ");


scanf("%d", &x);
printf("Enter the position (p): ");
scanf("%d", &p);
printf("Enter the number of bits (n): ");
scanf("%d", &n);

printf("Result after inverting: %d\n", invert(x, p, n));


return 0;
}
Output Example

Enter the number (x): 181


Enter the position (p): 4
Enter the number of bits (n): 2
Result after inverting: 173

Q39: Swapping by Call by Value

#include <stdio.h>

void swap(int a, int b) {


int temp = a;
a = b;
b = temp;
printf("After swapping: a = %d, b = %d\n", a, b);
}

int main() {
int a, b;

printf("Enter two numbers: ");


scanf("%d %d", &a, &b);

printf("Before swapping: a = %d, b = %d\n", a, b);


swap(a, b);

printf("Original values after swap function call: a = %d,


b = %d\n", a, b);
return 0;
}
Output Example
Enter two numbers: 5 10
Before swapping: a = 5, b = 10
After swapping: a = 10, b = 5
Original values after swap function call: a = 5, b = 10

Q40: Swapping by Call by Reference

#include <stdio.h>

void swap(int *a, int *b) {


int temp = *a;
*a = *b;
*b = temp;
}

int main() {
int a, b;

printf("Enter two numbers: ");


scanf("%d %d", &a, &b);

printf("Before swapping: a = %d, b = %d\n", a, b);


swap(&a, &b);
printf("After swapping: a = %d, b = %d\n", a, b);

return 0;
}
Output Example

Enter two numbers: 5 10


Before swapping: a = 5, b = 10
After swapping: a = 10, b = 5

Q41: Pointer Arithmetic Demonstration

#include <stdio.h>

int main() {
int arr[] = {10, 20, 30, 40, 50};
int *ptr = arr;

printf("Pointer arithmetic demonstration:\n");


for (int i = 0; i < 5; i++) {
printf("Value at arr[%d] = %d\n", i, *ptr);
ptr++;
}

return 0;
}
Output Example

Pointer arithmetic demonstration:


Value at arr[0] = 10
Value at arr[1] = 20
Value at arr[2] = 30
Value at arr[3] = 40
Value at arr[4] = 50

Q42: Linear Search in an Array

#include <stdio.h>

void linear_search(int arr[], int size, int key) {


for (int i = 0; i < size; i++) {
if (arr[i] == key) {
printf("Element %d found at index %d.\n", key,
i);
return;
}
}
printf("Element %d not found.\n", key);
}

int main() {
int arr[10], n, key;

printf("Enter number of elements: ");


scanf("%d", &n);

printf("Enter elements:\n");
for (int i = 0; i < n; i++)
scanf("%d", &arr[i]);

printf("Enter element to search: ");


scanf("%d", &key);

linear_search(arr, n, key);

return 0;
}
Output Example
Enter number of elements: 5
Enter elements:
1 2 3 4 5
Enter element to search: 3
Element 3 found at index 2.
Would you like me to continue with Q43 and further?

Q43: Printing Upper and Lower Triangular Matrix

#include <stdio.h>

void print_upper_triangular(int matrix[10][10], int size) {


printf("Upper Triangular Matrix:\n");
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
if (i > j)
printf("0 ");
else
printf("%d ", matrix[i][j]);
}
printf("\n");
}
}

void print_lower_triangular(int matrix[10][10], int size) {


printf("Lower Triangular Matrix:\n");
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
if (i < j)
printf("0 ");
else
printf("%d ", matrix[i][j]);
}
printf("\n");
}
}

int main() {
int matrix[10][10], size;

printf("Enter the size of the square matrix: ");


scanf("%d", &size);

printf("Enter the elements of the matrix:\n");


for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
scanf("%d", &matrix[i][j]);
}
}

print_upper_triangular(matrix, size);
print_lower_triangular(matrix, size);

return 0;
}
Output Example

Enter the size of the square matrix: 3


Enter the elements of the matrix:
1 2 3
4 5 6
7 8 9
Upper Triangular Matrix:
1 2 3
0 5 6
0 0 9
Lower Triangular Matrix:
1 0 0
4 5 0
7 8 9

Q44: Matrix Multiplication

#include <stdio.h>

void multiply_matrices(int mat1[10][10], int mat2[10][10],


int res[10][10], int r1, int c1, int c2) {
for (int i = 0; i < r1; i++) {
for (int j = 0; j < c2; j++) {
res[i][j] = 0;
for (int k = 0; k < c1; k++) {
res[i][j] += mat1[i][k] * mat2[k][j];
}
}
}
}

int main() {
int mat1[10][10], mat2[10][10], res[10][10], r1, c1, r2,
c2;
printf("Enter rows and columns for first matrix: ");
scanf("%d %d", &r1, &c1);

printf("Enter rows and columns for second matrix: ");


scanf("%d %d", &r2, &c2);

if (c1 != r2) {
printf("Matrix multiplication not possible.\n");
return 0;
}

printf("Enter elements of first matrix:\n");


for (int i = 0; i < r1; i++) {
for (int j = 0; j < c1; j++) {
scanf("%d", &mat1[i][j]);
}
}

printf("Enter elements of second matrix:\n");


for (int i = 0; i < r2; i++) {
for (int j = 0; j < c2; j++) {
scanf("%d", &mat2[i][j]);
}
}

multiply_matrices(mat1, mat2, res, r1, c1, c2);

printf("Resultant Matrix:\n");
for (int i = 0; i < r1; i++) {
for (int j = 0; j < c2; j++) {
printf("%d ", res[i][j]);
}
printf("\n");
}

return 0;
}
Output Example

Enter rows and columns for first matrix: 2 3


Enter rows and columns for second matrix: 3 2
Enter elements of first matrix:
1 2 3
4 5 6
Enter elements of second matrix:
7 8
9 10
11 12
Resultant Matrix:
58 64
139 154

Q45: Second Smallest Value in a Single Dimensional Array

#include <stdio.h>

void find_second_smallest(int arr[], int size) {


int smallest = __INT_MAX__, second_smallest =
__INT_MAX__;

for (int i = 0; i < size; i++) {


if (arr[i] < smallest) {
second_smallest = smallest;
smallest = arr[i];
} else if (arr[i] < second_smallest && arr[i] !=
smallest) {
second_smallest = arr[i];
}
}

if (second_smallest == __INT_MAX__) {
printf("Second smallest element does not exist.\n");
} else {
printf("Second smallest element: %d\n",
second_smallest);
}
}

int main() {
int arr[10], n;

printf("Enter number of elements: ");


scanf("%d", &n);

printf("Enter elements:\n");
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}

find_second_smallest(arr, n);
return 0;
}
Output Example

Enter number of elements: 5


Enter elements:
12 3 5 7 2
Second smallest element: 3

Q46: Transpose of a Matrix

#include <stdio.h>

void print_transpose(int matrix[10][10], int rows, int cols)


{
printf("Transpose of the Matrix:\n");
for (int i = 0; i < cols; i++) {
for (int j = 0; j < rows; j++) {
printf("%d ", matrix[j][i]);
}
printf("\n");
}
}

int main() {
int matrix[10][10], rows, cols;

printf("Enter rows and columns of the matrix: ");


scanf("%d %d", &rows, &cols);

printf("Enter elements of the matrix:\n");


for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
scanf("%d", &matrix[i][j]);
}
}

print_transpose(matrix, rows, cols);

return 0;
}
Output Example

Enter rows and columns of the matrix: 2 3


Enter elements of the matrix:
1 2 3
4 5 6
Transpose of the Matrix:
1 4
2 5
3 6
Q47: Structure for Student Details and Grade

#include <stdio.h>
#include <string.h>

struct Student {
char name[50];
int roll;
float percentage;
char grade;
};

char calculate_grade(float percentage) {


if (percentage >= 75) return 'A';
else if (percentage >= 60) return 'B';
else if (percentage >= 50) return 'C';
else return 'F';
}

void input_student_details(struct Student *student) {


printf("Enter student name: ");
scanf(" %[^\n]s", student->name);
printf("Enter roll number: ");
scanf("%d", &student->roll);
printf("Enter percentage: ");
scanf("%f", &student->percentage);
student->grade = calculate_grade(student->percentage);
}

void display_student_details(struct Student student) {


printf("\nStudent Details:\n");
printf("Name: %s\n", student.name);
printf("Roll Number: %d\n", student.roll);
printf("Percentage: %.2f%%\n", student.percentage);
printf("Grade: %c\n", student.grade);
}

int main() {
struct Student student;
input_student_details(&student);
display_student_details(student);

return 0;
}
Output Example

Enter student name: John Doe


Enter roll number: 101
Enter percentage: 78.5

Student Details:
Name: John Doe
Roll Number: 101
Percentage: 78.50% Grade: A

You might also like