answer key xi
answer key xi
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;
}
printf("\nBefore swapping:\n");
printf("First number: %d\n", num1);
printf("Second number: %d\n", num2);
printf("\nAfter swapping:\n");
printf("First number: %d\n", num1);
printf("Second number: %d\n", num2);
return 0;
}
// Calculate percentage
per = (total_marks / 300.0) * 100;
// Calculate area
area = PI * radius * radius;
#include <stdio.h>
int main()
{
int total_distance;
float fuel_spent, 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;
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;
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);
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
Solution:
#include <stdio.h>
int main() {
char 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;
int main() {
char lowercase, uppercase;
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;
// Calculating percentage
percentage = (total_marks / 5.0);
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);
return 0;
}
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;
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;
return 0;
}
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;
// 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;
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;
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;
}
int main() {
int 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;
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;
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;
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;
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;
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];
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;
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;
int arr[n];
return 0;
}
40. Write a program to find second largest number in an array.
Solution:
#include <stdio.h>
int main() {
int n;
scanf("%d", &n);
int arr[n];
scanf("%d", &arr[i]);
largest = secondLargest;
secondLargest = temp;
secondLargest = largest;
largest = arr[i];
secondLargest = arr[i];
return 0;