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

answer key xi

The document provides a series of C programming exercises along with their solutions. Each exercise focuses on basic programming concepts such as arithmetic operations, control statements, and user input/output. The solutions include complete code snippets demonstrating how to implement the required functionality in C.

Uploaded by

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

answer key xi

The document provides a series of C programming exercises along with their solutions. Each exercise focuses on basic programming concepts such as arithmetic operations, control statements, and user input/output. The solutions include complete code snippets demonstrating how to implement the required functionality in C.

Uploaded by

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

C Programming

Sequential program
1. Write a program to find sum of two numbers where first number is 45 and second number
is to be input through keyboard.

Solution:
#include <stdio.h>
int main()
{
int num1 = 45;
int num2;
printf("Enter the second number: ");
scanf("%d", &num2);
int sum = num1 + num2;
printf("The sum of %d and %d is %d.\n", num1, num2, sum);
return 0;
}

2. Write a program to swap 2 integer numbers by using a third variable.


Solution:
#include <stdio.h>
int main()
{
int num1, num2, temp;

printf("Enter first number: ");


scanf("%d", &num1);

printf("Enter second number: ");


scanf("%d", &num2);

printf("\nBefore swapping:\n");
printf("First number: %d\n", num1);
printf("Second number: %d\n", num2);

// Swapping using a third variable


temp = num1;
num1 = num2;
num2 = temp;

printf("\nAfter swapping:\n");
printf("First number: %d\n", num1);
printf("Second number: %d\n", num2);
return 0;
}

3. Write a simple program to calculate addition, subtraction and multiplication of 3 different


integer and floating point variables and display results.
Solution:
#include <stdio.h>
int main()
{
// Integer variables
int a = 10, b = 5, c = 3;
// Floating-point variables
float x = 7.5, y = 3.2, z = 1.8;
// Addition
int int_sum = a + b + c;
float float_sum = x + y + z;
// Subtraction
int int_diff = a - b - c;
float float_diff = x - y - z;
// Multiplication
int int_prod = a * b * c;
float float_prod = x * y * z;
// Displaying results
printf("Addition:\n");
printf("Integer sum: %d\n", int_sum);
printf("Floating-point sum: %.2f\n", float_sum);
printf("\nSubtraction:\n");
printf("Integer difference: %d\n", int_diff);
printf("Floating-point difference: %.2f\n", float_diff);
printf("\nMultiplication:\n");
printf("Integer product: %d\n", int_prod);
printf("Floating-point product: %.2f\n", float_prod);
return 0;
}
4. Write a program to ask user to input 4 different integer value and find the average.
Solution:
#include <stdio.h>
int main()
{
int num1, num2, num3, num4;
float average;
printf("Enter the first number: ");
scanf("%d", &num1);

printf("Enter the second number: ");


scanf("%d", &num2);
printf("Enter the third number: ");
scanf("%d", &num3);
printf("Enter the fourth number: ");
scanf("%d", &num4);
// Calculate average
average = (num1 + num2 + num3 + num4) / 4.0;
printf("The average of four number is %.2f\n", average);
return 0;
}
5. Write a C program to read marks of 3 subjects and print obtained percentage marks.
Solution:
#include <stdio.h>
int main()
{
int eng, maths, comp;
float total_marks, per;

// Input marks for three subjects


printf("Enter marks for English: ");
scanf("%d", &eng);

printf("Enter marks for Maths: ");


scanf("%d", &maths);

printf("Enter marks for Computer: ");


scanf("%d", &comp);

// Calculate total marks


total_marks = eng + maths + comp;

// Calculate percentage
per = (total_marks / 300.0) * 100;

// Print the obtained percentage marks


printf("Obtained percentage marks: %.2f%%\n", per);
return 0;
}
6. Write a program that takes two integer numbers as input from the user, calculates the
quotient and remainder using the / and % operators respectively, and then displays the
results.
Solution:
#include <stdio.h>
int main()
{
int num1, num2, quotient, remainder;

// Input two integer numbers


printf("Enter the first integer number: ");
scanf("%d", &num1);

printf("Enter the second integer number: ");


scanf("%d", &num2);

// Calculate quotient and remainder


quotient = num1 / num2;
remainder = num1 % num2;

// Display the results


printf("Quotient of %d divided by %d is %d\n", num1, num2, quotient);
printf("Remainder of %d divided by %d is %d\n", num1, num2, remainder);
return 0;
}
7. A company gives Rs. 20,000 to as basic salary per month to its employee, 10% of basic
salary as provident fund and 10% of basic salary as CIT, deducts 1% as tax of the basic
Salary. Now, write a program to find the net salary of the employee.
Solution:
#include <stdio.h>
int main()
{
float basic_salary = 20000;
float provident_fund, cit, tax, net_salary;

// Calculate provident fund, CIT, and tax


provident_fund = 0.1 * basic_salary;
cit = 0.1 * basic_salary;
tax = 0.01 * basic_salary;

// Calculate net salary


net_salary = basic_salary - provident_fund - cit - tax;
// Display the results
printf("Basic Salary: Rs. %.2f\n", basic_salary);
printf("Provident Fund: Rs. %.2f\n", provident_fund);
printf("CIT: Rs. %.2f\n", cit);
printf("Tax: Rs. %.2f\n", tax);
printf("Net Salary: Rs. %.2f\n", net_salary);
return 0;
}
8. Write a C program to compute the perimeter and area of a circle with a given radius. Use
pi=3.14.
Solution:
#include <stdio.h>
#define PI 3.14
int main()
{
float radius, perimeter, area;

// Input radius from the user


printf("Enter the radius of the circle: ");
scanf("%f", &radius);

// Calculate perimeter (circumference)


perimeter = 2 * PI * radius;

// Calculate area
area = PI * radius * radius;

// Display the results


printf("Perimeter (Circumference) of the circle: %.2f\n", perimeter);
printf("Area of the circle: %.2f\n", area);
return 0;
}
9. Write a program to find simple interest where principle is Rs.1000, rate 10% and time is
5 years.
Solution:
#include <stdio.h>
int main()
{
float p = 1000;
float r = 10;
float t = 5;
float simple_interest;
simple_interest = (p * r * t) / 100;
printf("Simple Interest: Rs. %.2f\n", simple_interest);
return 0;
}
10. Write a C program to calculate a bike’s average fuel consumption from the given total
distance (integer value) travelled in km and spent fuel in litters, you can use float number
upto 2 decimal points.
Solution:

#include <stdio.h>
int main()
{
int total_distance;
float fuel_spent, average_consumption;

// Input total distance traveled in km


printf("Enter the total distance traveled (in km): ");
scanf("%d", &total_distance);

// Input fuel spent in liters


printf("Enter the fuel spent (in liters): ");
scanf("%f", &fuel_spent);

// Calculate average fuel consumption


average_consumption = fuel_spent / total_distance;

// Display the result


printf("Average fuel consumption: %.2f liters/km\n", average_consumption);

return 0;
}
11. Write a C program to convert specified days into years, weeks and days.
Solution:
#include <stdio.h>

int main()
{
int days, years, weeks;

days = 1329; // Total number of days

// Converts days to years, weeks and days


years = days/365; // Calculate years
weeks = (days % 365)/7; // Calculate weeks
days = days - ((years*365) + (weeks*7)); // Calculate remaining days

// Print the results


printf("Years: %d\n", years);
printf("Weeks: %d\n", weeks);
printf("Days: %d \n", days);
return 0;
}

Selection control statement Or Decision Control Statement:


(if …statement)
12. Write a C program to accept two integers and check whether they are equal or not.
Solution:
#include <stdio.h>
int main()
{
// Declare two integer variables 'int1' and 'int2'.
int num1, num2;

printf("Input the values for Number1 and Number2 : ");


scanf("%d %d", &num1, &num2);
if (num1 == num2)
printf("Number1 and Number2 are equal\n");
else
printf("Number1 and Number2 are not equal\n");
return 0;
}

13. Write a C program to accept two integer numbers and check whether the second number
is greater or not?
Solution:
#include <stdio.h>
int main() {
int num1, num2;

printf("Enter the first integer number: ");


scanf("%d", &num1);

printf("Enter the second integer number: ");


scanf("%d", &num2);
if (num2 > num1)
{
printf("The 2nd number %d is greater than the 1st number %d.\n", num2, num1);
}
else
{
printf("The 2nd number %d is not greater than the 1st number %d.\n", num2, num1);
}
return 0;
}
14. Write a C program to check whether a given number is even or odd.

Solution:
#include <stdio.h>
int main()
{
int num, rem;
printf("Input an integer : ");
scanf("%d", &num);
rem = num % 2; // finds remainder of 'num' when divided by 2.
if (rem == 0) // Check if the remainder is equal to 0.
printf("%d is an even integer\n", num);
else
printf("%d is an odd integer\n", num);
return 0;
}

15. Write a C program to check whether a person is eligible for voting or not.[eligible age is
18 years and more]
Solution:
#include<stdio.h>
int main()
{
int a ;
printf("Enter the age of the person: ");
scanf("%d",&a);
if (a>=18)
{
printf("You are Eligible for voting");
}
else
{
printf("You are Not Eligible for voting\n");
}

return 0;
}

16. Write a program to enter any integer number and check whether it is exactly divisible by
3 and 5 or not?
Solution:
#include <stdio.h>
int main()
{
int num;
printf("Enter an integer number: ");
scanf("%d", &num);
if (num % 3 == 0 && num % 5 == 0)
{
printf("%d is divisible by both 3 and 5.\n", num);
}
else {
printf("%d is not divisible by both 3 and 5.\n", num);
}
return 0;
}
17. Write a program to enter any three numbers and find the smallest one.
Solution:
#include <stdio.h>
int main()
{
int num1, num2, num3;
printf("Enter three numbers: ");
scanf("%d %d %d", &num1, &num2, &num3);

if (num1 < num2 && num1 < num3)


{
printf("First number %d is smallest\n", num1);
}
else if (num2 < num1 && num2 < num3)
{
printf("Second number %d is smallest\n", num2);
}
else if (num3 < num1 && num3 < num2)
{
printf("Third number %d is smallest\n", num3);
}
else
{
printf("Invalid input\n");
}
return 0;
}

18. Write a program to input ‘Y’ or ‘N’ for whether a person has citizenship or not and then
on the basis of age factor decide whether he/she is eligible for driving license for 4
wheeler vehicle or not. [Note: Government of Nepal only allows Nepalese citizens who
are above 20 years of age.]
Solution:
#include <stdio.h>
int main()
{
char citizenship;
int age;
/ Prompting user to input citizenship status ('Y' or 'N')
printf("Do you have citizenship? (Y/N): ");
scanf(" %c", &citizenship); // the space before %c to consume any leading whitespace

// Checking if the person has citizenship


if (citizenship == 'Y' || citizenship == 'y')
{
// Prompting user to input age
printf("Enter your age: ");
scanf("%d", &age);

// Checking age eligibility for driving license


if (age >= 20) {
printf("Congratulations! You are eligible for a driving license for a four-wheeler
vehicle.\n");
} else {
printf("Sorry, you are not eligible for a driving license for a four-wheeler vehicle.\
n");
}
} else if (citizenship == 'N' || citizenship == 'n') {
printf("Sorry, only Nepalese citizens are eligible for a driving license.\n");
} else {
printf("Invalid input. Please enter either 'Y' or 'N'.\n");
}
return 0;
}
19. Write a program to check a given character is a lowercase character or uppercase without
using library function.

Solution:
#include <stdio.h>
int main() {
char ch;

// Prompting user to enter a character


printf("Enter a character: ");
scanf("%c", &ch);

// Checking if the character is lowercase


if (ch >= 'a' && ch <= 'z') {
printf("%c is a lowercase character.\n", ch);
}
// Checking if the character is uppercase
else if (ch >= 'A' && ch <= 'Z') {
printf("%c is an uppercase character.\n", ch);
}
// If the character is not a letter
else {
printf("%c is not a letter.\n", ch);
}

return 0;
}
20. Write a program to check a given character is a vowel or not without using library
function.
Solution:
#include <stdio.h>
int main() {
char ch;

// Prompting user to enter a character


printf("Enter a character: ");
scanf(" %c", &ch); // Note the space before %c to consume any leading whitespace
// Checking if the character is a vowel
if ((ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') ||
(ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U')) {
printf("%c is a vowel.\n", ch);
} else {
printf("%c is not a vowel.\n", ch);
}
return 0;
}
21. Write a program to ask user to input lowercase character and convert into uppercase
without using library function.
Solution:
#include <stdio.h>

int main() {
char lowercase, uppercase;

// Prompting user to enter a lowercase character


printf("Enter a lowercase character: ");
scanf(" %c", &lowercase); // Note the space before %c to consume any leading
whitespace

// Checking if the input is a lowercase character


if (lowercase >= 'a' && lowercase <= 'z') {
// Converting to uppercase by subtracting the ASCII value difference
uppercase = lowercase - ('a' - 'A');
printf("Uppercase equivalent: %c\n", uppercase);
} else {
printf("Invalid input. Please enter a lowercase character.\n");
}

return 0;
}
22. Write a program to enter marks of 5 subjects of a student, calculate their percentage
marks and find the grading A, B, C or D on the basis of following:
a. If percentage marks <50 then assign grade D
b. If percentage marks between 50 to 60 then assign grade C
c. If percentage marks between 61 to 79.99 then assign grade B
d. If percentage marks greater than or equal to 80 then assign grade A

Solution:
#include <stdio.h>
int main() {
int marks[5];
float total_marks = 0, percentage;
char grade;

// Prompting user to enter marks for 5 subjects


printf("Enter marks for 5 subjects:\n");
for (int i = 0; i < 5; i++) {
printf("Enter marks for subject %d: ", i + 1);
scanf("%d", &marks[i]);
total_marks += marks[i];
}

// Calculating percentage
percentage = (total_marks / 5.0);

// Assigning grade based on percentage


if (percentage >= 80) {
grade = 'A';
} else if (percentage >= 61 && percentage < 80) {
grade = 'B';
} else if (percentage >= 50 && percentage < 61) {
grade = 'C';
} else {
grade = 'D';
}

// Printing percentage and grade


printf("Percentage Marks: %.2f%%\n", percentage);
printf("Grade: %c\n", grade);

return 0;
}
23. Write a program to check whether an entered year is a leap year or not.
Note: A leap year is such which is divisible by 4, if the year is divisible by 100 then it
should also be divisible by 400.

Solution:
#include <stdio.h>
int main() {
int year;
// Prompting user to enter a year
printf("Enter a year: ");
scanf("%d", &year);

// Checking if the year is a leap 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;
}

Switch case statement


24. Write a program to check entered character is VOWEL or CONSONANT using if and
switch.. case statement.
Solution:
#include <stdio.h>
int main() {
char ch;

// Prompting user to enter a character


printf("Enter a character: ");
scanf(" %c", &ch); // Note the space before %c to consume any leading whitespace

// Using if statement to check if the character is a vowel or consonant


if (ch >= 'A' && ch <= 'Z') {
ch = ch + 32; // Convert uppercase to lowercase for simplicity
}
if (ch >= 'a' && ch <= 'z') {
if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') {
printf("%c is a vowel.\n", ch);
} else {
printf("%c is a consonant.\n", ch);
}
} else {
printf("Invalid input. Please enter an alphabet.\n");
}

// Using switch...case statement to check if the character is a vowel or consonant


switch(ch) {
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
printf("%c is a vowel.\n", ch);
break;
default:
if (ch >= 'a' && ch <= 'z') {
printf("%c is a consonant.\n", ch);
}
break;
}

return 0;
}
25. Write a program using switch case statement that will ask user two integers and Display a
sample menu like
1. Addition
2. Subtraction
3. Multiplication
4. Division
Once user selects option from the list do the arithmetic operation as per and display the
result.
Solution:
#include <stdio.h>
int main() {
int num1, num2, choice;
float result;

// Prompting user to enter two integers


printf("Enter two integers: ");
scanf("%d %d", &num1, &num2);

// Displaying the menu


printf("\nSample Menu:\n");
printf("1. Addition\n");
printf("2. Subtraction\n");
printf("3. Multiplication\n");
printf("4. Division\n");
printf("Enter your choice (1-4): ");
scanf("%d", &choice);

// Performing the arithmetic operation based on user's choice


switch (choice) {
case 1:
result = num1 + num2;
printf("Result of addition: %.2f\n", result);
break;
case 2:
result = num1 - num2;
printf("Result of subtraction: %.2f\n", result);
break;
case 3:
result = num1 * num2;
printf("Result of multiplication: %.2f\n", result);
break;
case 4:
// Checking if num2 is not zero to avoid division by zero
if (num2 != 0) {
result = (float)num1 / num2;
printf("Result of division: %.2f\n", result);
} else {
printf("Error! Division by zero is not allowed.\n");
}
break;
default:
printf("Invalid choice! Please enter a number between 1 and 4.\n");
}

return 0;
}
26. Write a program using switch case statement that will ask user any number from 1 to 12
and display name of month.[1- baishakh, 2- Jestha……]
Solution:
#include <stdio.h>
int main() {
int month_number;

// Prompting user to enter a number from 1 to 12


printf("Enter a number from 1 to 12: ");
scanf("%d", &month_number);

// Displaying the name of the month based on the entered number


switch (month_number) {
case 1:
printf("Month %d is Baishakh.\n", month_number);
break;
case 2:
printf("Month %d is Jestha.\n", month_number);
break;
case 3:
printf("Month %d is Ashar.\n", month_number);
break;
case 4:
printf("Month %d is Shrawan.\n", month_number);
break;
case 5:
printf("Month %d is Bhadra.\n", month_number);
break;
case 6:
printf("Month %d is Ashoj.\n", month_number);
break;
case 7:
printf("Month %d is Kartik.\n", month_number);
break;
case 8:
printf("Month %d is Mangsir.\n", month_number);
break;
case 9:
printf("Month %d is Poush.\n", month_number);
break;
case 10:
printf("Month %d is Magh.\n", month_number);
break;
case 11:
printf("Month %d is Falgun.\n", month_number);
break;
case 12:
printf("Month %d is Chaitra.\n", month_number);
break;
default:
printf("Invalid input! Please enter a number between 1 and 12.\n");
}

return 0;
}

Iteration Control Statement: Looping


27. Write a program using while loop to display the n terms of odd natural numbers. (eg: 1 3
5 ………. N)
Solution:
#include <stdio.h>
int main() {
int n, count = 1;
// Prompting user to enter the value of n
printf("Enter the value of n: ");
scanf("%d", &n);

// Displaying the n terms of odd natural numbers using a while loop


printf("The first %d odd natural numbers are:\n", n);
while (n > 0) {
printf("%d ", count);
count += 2; // Incrementing count by 2 to get the next odd number
n--;
}
printf("\n");

return 0;
}
28. Write a program to ask any number from the user and print their table using for loop.
Solution:
#include <stdio.h>
int main() {
int number;

// Prompting user to enter a number


printf("Enter a number: ");
scanf("%d", &number);

// Printing the multiplication table of the entered number using a for loop
printf("Multiplication table of %d:\n", number);
for (int i = 1; i <= 10; i++) {
printf("%d x %d = %d\n", number, i, number * i);
}

return 0;
}
29. Write a program to find the sum of n natural numbers.
Solution:
#include <stdio.h>
int main() {
int n, sum = 0;

// Prompting user to enter the value of n


printf("Enter the value of n: ");
scanf("%d", &n);

// Calculating the sum of the first n natural numbers using a loop


for (int i = 1; i <= n; i++) {
sum += i;
}

// Printing the sum


printf("The sum of the first %d natural numbers is: %d\n", n, sum);

return 0;
}
30. Write a program to find the sum of n even natural numbers.
Solution:
#include <stdio.h>
int main() {
int n, sum = 0;

// Prompting user to enter the value of n


printf("Enter the value of n: ");
scanf("%d", &n);

// Calculating the sum of the first n even natural numbers


for (int i = 1; i <= n; i++) {
sum += 2 * i; // Even natural numbers follow the pattern 2, 4, 6, ...
}

// Printing the sum


printf("The sum of the first %d even natural numbers is: %d\n", n, sum);

return 0;
}
31. Write a program to find the LCM of Two numbers.
Solution:
#include <stdio.h>
// Function to find the GCD (greatest common divisor) of two numbers
int gcd(int a, int b) {
while (b != 0) {
int temp = b;
b = a % b;
a = temp;
}
return a;
}

// Function to find the LCM (least common multiple) of two numbers


int lcm(int a, int b) {
return (a * b) / gcd(a, b);
}

int main() {
int num1, num2;

// Prompting user to enter two numbers


printf("Enter two numbers: ");
scanf("%d %d", &num1, &num2);

// Calculating the LCM and printing the result


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

return 0;
}
32. Write a program to find the HCF (Highest Common Factor) of two numbers.
Solution:
#include <stdio.h>
// Function to find the GCD (greatest common divisor) of two numbers
int gcd(int a, int b) {
while (b != 0) {
int temp = b;
b = a % b;
a = temp;
}
return a;
}

int main() {
int num1, num2;

// Prompting user to enter two numbers


printf("Enter two numbers: ");
scanf("%d %d", &num1, &num2);

// Calculating the GCD and printing the result


printf("The HCF of %d and %d is: %d\n", num1, num2, gcd(num1, num2));

return 0;
}
33. Write a program that checks if a given integer number is a binary number or not without
using a separate function: (Hints use number/10 and number % 10 and While loop)
Solution:
#include <stdio.h>
int main() {
int number, digit;

// Prompting user to enter a number


printf("Enter an integer number: ");
scanf("%d", &number);
// Copying the original number to a temporary variable
int temp = number;

// Flag to indicate if the number is binary or not


int isBinary = 1; // Assume number is binary initially

// Checking each digit of the number


while (temp != 0) {
digit = temp % 10;
if (digit != 0 && digit != 1) {
isBinary = 0; // Update flag if any digit is not 0 or 1
break;
}
temp /= 10;
}

// Checking the flag to determine the result


if (isBinary) {
printf("%d is a binary number.\n", number);
} else {
printf("%d is not a binary number.\n", number);
}

return 0;
}
34. Write a program to find factorial of a number using looping statement.
Solution:
#include <stdio.h>
int main() {
int number;
unsigned long long factorial = 1;

// Prompting user to enter a number


printf("Enter a positive integer: ");
scanf("%d", &number);

// Checking if the number is negative


if (number < 0) {
printf("Error! Factorial of a negative number doesn't exist.\n");
} else {
// Calculating factorial using a loop
for (int i = 1; i <= number; i++) {
factorial *= i;
}
printf("Factorial of %d = %llu\n", number, factorial);
}

return 0;
}
35. Write a program to generate Fibonacci Series using loop.
Solution:
#include <stdio.h>

int main() {
int n, first = 0, second = 1, next;

// Prompting user to enter the number of terms


printf("Enter the number of terms: ");
scanf("%d", &n);

printf("Fibonacci Series: ");

// Generating Fibonacci series using a loop


for (int i = 0; i < n; i++) {
if (i <= 1) {
next = i;
} else {
next = first + second;
first = second;
second = next;
}
printf("%d ", next);
}

printf("\n");

return 0;
}

Array
36. Write a program to enter any 10 numbers using array and find sum of first, fifth and tenth
element of the array.
Solution:
#include <stdio.h>

int main() {
int numbers[10];
int sum = 0;

// Prompting user to enter 10 numbers and storing them in an array


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

// Calculating the sum of the first, fifth, and tenth elements


sum = numbers[0] + numbers[4] + numbers[9];

// Printing the sum


printf("Sum of the first, fifth, and tenth elements = %d\n", sum);

return 0;
}
37. Write a program to declare a single dimensional array of 5 elements, take input for all
elements and then display all 5 elements in a row without using loop.
Solution:
#include <stdio.h>
int main() {
int arr[5];

// Taking input for all elements


printf("Enter the elements of the array:\n");
printf("Enter element 1: ");
scanf("%d", &arr[0]);
printf("Enter element 2: ");
scanf("%d", &arr[1]);
printf("Enter element 3: ");
scanf("%d", &arr[2]);
printf("Enter element 4: ");
scanf("%d", &arr[3]);
printf("Enter element 5: ");
scanf("%d", &arr[4]);

// Displaying all 5 elements in a row without using loop


printf("Elements of the array: %d %d %d %d %d\n", arr[0], arr[1], arr[2], arr[3],
arr[4]);

return 0;
}
38. Write a program to find out the average of 10 integers.
Solution:
#include <stdio.h>

int main() {
int numbers[10];
int sum = 0;
float average;

// Prompting user to enter 10 integers and calculating their sum


printf("Enter 10 integers:\n");
for (int i = 0; i < 10; i++) {
printf("Enter number %d: ", i + 1);
scanf("%d", &numbers[i]);
sum += numbers[i];
}

// Calculating the average


average = (float)sum / 10;

// Printing the average


printf("Average of the 10 integers = %.2f\n", average);

return 0;
}
39. Program to print the smallest number from ‘n’ number of element of the array.
Solution:
#include <stdio.h>

int main() {
int n;

// Prompting user to enter the size of the array


printf("Enter the number of elements in the array: ");
scanf("%d", &n);

int arr[n];

// Prompting user to enter the elements of the array


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

// Finding the smallest number in the array


int smallest = arr[0]; // Assume the first element as the smallest
for (int i = 1; i < n; i++) {
if (arr[i] < smallest) {
smallest = arr[i];
}
}
// Printing the smallest number
printf("The smallest number in the array is: %d\n", smallest);

return 0;
}
40. Write a program to find second largest number in an array.

Solution:
#include <stdio.h>

int main() {

int n;

// Prompting user to enter the size of the array

printf("Enter the number of elements in the array: ");

scanf("%d", &n);

// Declaring an array of size n

int arr[n];

// Prompting user to enter the elements of the array

printf("Enter %d elements:\n", n);

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

printf("Enter element %d: ", i + 1);

scanf("%d", &arr[i]);

// Finding the second largest number in the array

int largest = arr[0];

int secondLargest = arr[1];

if (secondLargest > largest) {


int temp = largest;

largest = secondLargest;

secondLargest = temp;

for (int i = 2; i < n; i++) {

if (arr[i] > largest) {

secondLargest = largest;

largest = arr[i];

} else if (arr[i] > secondLargest && arr[i] != largest) {

secondLargest = arr[i];

// Printing the second largest number

printf("The second largest number in the array is: %d\n", secondLargest);

return 0;

You might also like