Class 12 C Programming Lab Report Solutions
Lab Report Question Class 12
1. Write a function void greet() that prints “Hello, welcome to C programming!”
2. WAP to find the sum of two numbers using a function (without argument, without return type).
3. WAP to check whether a number is even or odd using a function (without argument, with return type).
4. WAP to take a student’s marks from the user and display whether the student has passed or failed. A student passes if the marks are greater than or equal to 35 (with argument, without return type).
5. WAP to find the greatest among three numbers using a function (with argument, with return type).
6. WAP to check if a number is exactly divisible by 5 or 7 without using a function.
7. WAP to check whether a number is prime or not using a function.
8. WAP to find the sum of digits of a number using a function.
9. WAP to reverse a number and also check whether it is a palindrome or not using a function.
10. WAP to check whether a number is an Armstrong number or not.
11. WAP to print all prime numbers up to n using a function.
12. WAP to print the sum of n terms using a recursive function.
13. WAP to print the factorial of n using a recursive function.
14. WAP to generate the Fibonacci series up to the nth term using a recursive function.
15. WAP to take the marks of 5 subjects as input and calculate the percentage using a function. If the percentage is between 80 and 100, print grade A; if it is between 60 and 79, print grade B; if it is between 40 and 59, print grade C; and if it is below 40, print NG.
Question 1: Write a function void greet() that prints "Hello, welcome to C programming!"
Solution:
#include <stdio.h>
// Function declaration
void greet();
int main() {
// Calling the greet function
greet();
return 0;
}
// Function definition
void greet() {
printf("Hello, welcome to C programming!\n");
}
Output:
Hello, welcome to C programming!
Explanation:
void greet()is a function that takes no arguments and returns nothing (void)- The function simply prints the welcome message
- We call the function from
main()usinggreet();
Question 2: WAP to find the sum of two numbers using a function (without argument, without return type)
Solution:
#include <stdio.h>
void sum();
int main() {
printf("Program to find sum of two numbers\n");
sum();
return 0;
}
void sum() {
int num1, num2, result;
printf("Enter first number: ");
scanf("%d", &num1);
printf("Enter second number: ");
scanf("%d", &num2);
result = num1 + num2;
printf("\nSum of %d and %d = %d\n", num1, num2, result);
}
Output:
Program to find sum of two numbers
====================================
Enter first number: 15
Enter second number: 25
Sum of 15 and 25 = 40
Explanation:
- The function
sum()has no arguments (empty parentheses) - It has no return type (void)
- All input is taken inside the function itself
- The result is also displayed inside the function
- Nothing is passed to or returned from the function
Question 3: WAP to check whether a number is even or odd using a function (without argument, with return type)
Solution:
#include <stdio.h>
int checkEvenOdd();
int main() {
int result;
printf("Program to check Even or Odd\n");
result = checkEvenOdd();
if (result == 0) {
printf("\nThe number is EVEN\n");
} else {
printf("\nThe number is ODD\n");
}
return 0;
}
int checkEvenOdd() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
if (num % 2 == 0) {
return 0; // Even
} else {
return 1; // Odd
}
}
Output (Example 1):
Program to check Even or Odd
============================
Enter a number: 24
The number is EVEN
Output (Example 2):
Program to check Even or Odd
============================
Enter a number: 17
The number is ODD
Explanation:
- The function
checkEvenOdd()has no arguments (empty parentheses) - It has a return type of int
- Input is taken inside the function
- The function returns:
0if the number is even1if the number is odd
- The returned value is used in
main()to display the result
Alternative Solution (Returning the number itself):
#include <stdio.h>
// Function declaration
int checkEvenOdd();
int main() {
int num;
printf("Program to check Even or Odd\n");
printf("============================\n\n");
// Calling the function and storing returned number
num = checkEvenOdd();
// Check and display result
if (num % 2 == 0) {
printf("\n%d is EVEN\n", num);
} else {
printf("\n%d is ODD\n", num);
}
return 0;
}
// Function definition - returns the number itself
int checkEvenOdd() {
int number;
printf("Enter a number: ");
scanf("%d", &number);
// Return the number for checking in main
return number;
}
4. Pass or Fail Check (With Argument, Without Return)
A function that checks if a student passed (marks >= 35) or failed.
#include <stdio.h>
void check_result(int marks)
{
if (marks >= 35)
printf("Student Passed!\n");
else
printf("Student Failed!\n");
}
int main()
{
int marks;
printf("Enter student marks: ");
scanf("%d", &marks);
check_result(marks);
return 0;
}
How it works: The function receives marks as an argument, checks if it's >= 35, and prints the result directly using void return type.
5. Greatest Among Three Numbers (With Argument, With Return)
A function that finds the largest number among three values.
#include <stdio.h>
int find_greatest(int a, int b, int c)
{
if (a >= b && a >= c)
return a;
else if (b >= a && b >= c)
return b;
else
return c;
}
int main()
{
int num1, num2, num3, result;
printf("Enter first number: ");
scanf("%d", &num1);
printf("Enter second number: ");
scanf("%d", &num2);
printf("Enter third number: ");
scanf("%d", &num3);
result = find_greatest(num1, num2, num3);
printf("Greatest number is: %d\n", result);
return 0;
}
How it works: The function compares three numbers and returns the greatest one using return statement.
6. Check if Number is Divisible by 5 or 7 (Without Function)
Check divisibility without using a function.
#include <stdio.h>
int main()
{
int num;
printf("Enter a number: ");
scanf("%d", &num);
if (num % 5 == 0 || num % 7 == 0)
printf("%d is divisible by 5 or 7\n", num);
else
printf("%d is not divisible by 5 or 7\n", num);
return 0;
}
How it works: We use the modulus operator (%) to check if the remainder is 0 when divided by 5 or 7.
7. Check if Number is Prime (Using Function)
A function to determine if a number is prime or not.
#include <stdio.h>
int is_prime(int n)
{
int i;
if (n < 2)
return 0;
for (i = 2; i < n; i++)
{
if (n % i == 0)
return 0;
}
return 1;
}
int main()
{
int num;
printf("Enter a number: ");
scanf("%d", &num);
if (is_prime(num))
printf("%d is a Prime Number\n", num);
else
printf("%d is not a Prime Number\n", num);
return 0;
}
How it works: A prime number is only divisible by 1 and itself. We check if any number from 2 to n-1 divides n evenly.
8. Sum of Digits of a Number (Using Function)
A function that calculates the sum of all digits in a number.
#include <stdio.h>
int sum_of_digits(int n)
{
int total = 0;
while (n > 0)
{
int digit = n % 10;
total = total + digit;
n = n / 10;
}
return total;
}
int main()
{
int number, result;
printf("Enter a number: ");
scanf("%d", &number);
result = sum_of_digits(number);
printf("Sum of digits: %d\n", result);
return 0;
}
How it works: We extract each digit using modulus, add it to the total, and remove it using integer division.
9. Reverse Number and Check if Palindrome (Using Function)
A function to reverse a number and check if it's a palindrome.
#include <stdio.h>
int reverse_number(int n)
{
int reversed = 0;
while (n > 0)
{
int digit = n % 10;
reversed = reversed * 10 + digit;
n = n / 10;
}
return reversed;
}
int main()
{
int number, reversed;
printf("Enter a number: ");
scanf("%d", &number);
reversed = reverse_number(number);
printf("Original number: %d\n", number);
printf("Reversed number: %d\n", reversed);
if (number == reversed)
printf("It is a Palindrome!\n");
else
printf("It is not a Palindrome!\n");
return 0;
}
How it works: We reverse by extracting digits and building a new number. Then we compare original with reversed to check for palindrome.
10. Armstrong Number Check
A function to check if a number is an Armstrong number.
#include <stdio.h>
#include <math.h>
int is_armstrong(int n)
{
int original = n;
int sum = 0;
int count = 0;
int temp = n;
// Count number of digits
while (temp > 0)
{
count++;
temp = temp / 10;
}
// Calculate sum of digits raised to power count
temp = n;
while (temp > 0)
{
int digit = temp % 10;
sum = sum + pow(digit, count);
temp = temp / 10;
}
if (sum == original)
return 1;
else
return 0;
}
int main()
{
int number;
printf("Enter a number: ");
scanf("%d", &number);
if (is_armstrong(number))
printf("%d is an Armstrong Number\n", number);
else
printf("%d is not an Armstrong Number\n", number);
return 0;
}
How it works: An Armstrong number equals the sum of its digits raised to the power of number of digits. Example: 153 = 1³ + 5³ + 3³
11. Print All Prime Numbers Up to n (Using Function)
A function that finds and prints all prime numbers up to a given number.
#include <stdio.h>
int is_prime(int num)
{
int i;
if (num < 2)
return 0;
for (i = 2; i < num; i++)
{
if (num % i == 0)
return 0;
}
return 1;
}
void print_primes(int n)
{
int num;
printf("Prime numbers up to %d:\n", n);
for (num = 2; num <= n; num++)
{
if (is_prime(num))
printf("%d ", num);
}
printf("\n");
}
int main()
{
int limit;
printf("Enter the limit: ");
scanf("%d", &limit);
print_primes(limit);
return 0;
}
How it works: We check each number up to n using the is_prime function and print only the prime numbers.
12. Sum of n Terms (Using Recursive Function)
A recursive function to calculate the sum of first n natural numbers.
#include <stdio.h>
int sum_of_n(int n)
{
if (n == 0)
return 0;
else
return n + sum_of_n(n - 1);
}
int main()
{
int n, result;
printf("Enter a number: ");
scanf("%d", &n);
result = sum_of_n(n);
printf("Sum of first %d numbers: %d\n", n, result);
return 0;
}
How it works: Recursion means the function calls itself. Base case: when n=0, return 0. Recursive case: return n + sum of remaining numbers.
13. Factorial Using Recursion
A recursive function to calculate the factorial of a number.
#include <stdio.h>
int factorial(int n)
{
if (n == 0 || n == 1)
return 1;
else
return n * factorial(n - 1);
}
int main()
{
int n, result;
printf("Enter a number: ");
scanf("%d", &n);
result = factorial(n);
printf("Factorial of %d is: %d\n", n, result);
return 0;
}
How it works: Factorial of n = n × (n-1) × (n-2) × ... × 1. The function multiplies n by the factorial of (n-1).
14. Fibonacci Series Using Recursion
A recursive function to generate the Fibonacci series up to the nth term.
#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 the number of terms: ");
scanf("%d", &n);
printf("Fibonacci series up to %d terms:\n", n);
for (i = 1; i <= n; i++)
{
printf("%d ", fibonacci(i));
}
printf("\n");
return 0;
}
How it works: Each Fibonacci number is the sum of the two preceding numbers (0, 1, 1, 2, 3, 5, 8, ...). The function calls itself twice.
15. Student Grade Calculator (Using Function)
A function to calculate percentage and assign grades based on 5 subjects.
#include <stdio.h>
void calculate_grade(int marks[], float *percentage, char *grade)
{
int total = 0;
int i;
// Calculate total marks
for (i = 0; i < 5; i++)
{
total = total + marks[i];
}
// Calculate percentage (assuming each subject is out of 100)
*percentage = (total / 500.0) * 100;
// Assign grade
if (*percentage >= 80 && *percentage <= 100)
*grade = 'A';
else if (*percentage >= 60 && *percentage < 80)
*grade = 'B';
else if (*percentage >= 40 && *percentage < 60)
*grade = 'C';
else
*grade = 'N'; // N = Not Good / Fail
}
int main()
{
int marks[5];
float percentage;
char grade;
int i;
printf("Enter marks for 5 subjects (out of 100):\n");
for (i = 0; i < 5; i++)
{
printf("Subject %d: ", i + 1);
scanf("%d", &marks[i]);
}
calculate_grade(marks, &percentage, &grade);
printf("\nPercentage: %.2f%%\n", percentage);
printf("Grade: %c\n", grade);
return 0;
}
How it works: The function takes an array of marks and calculates the total percentage. It then assigns grades based on the percentage ranges. We use pointers to return multiple values from the function.
Netra Koirala
Computer Science Educator
Passionate computer science educator and author. Provides free study notes, practical guides, and tutorials for Class 9, 10, 11, 12, and B.Sc CSIT students in Nepal. Years of teaching experience in computer science fundamentals.
LinkedIn ProfileRelated Posts
Loading related posts…
Computer Science notes, tutorials, MCQs, and educational resources for Nepal students. Covering Class 9, SEE preparation, Class 11, Class 12, SLC, programming, DBMS, networking, HTML, JavaScript, PHP, OOP and more.
Featured Post
Grade 10 Computer Science: Specification Grid & Model Questions
Specification Grid & Model Questions of Computer Science | Grade 10 📚 Examination Resource Specification Grid & M...