Unit 4: Programming in C
Class 12 Computer Science: C PROGRAMMING Program Question and Solution For NEB Exam 2079 and 2080 [2023]
NEB Class 12 Computer Science C Programming Program and Theory's Questions for 2080-2081, Includes loops, strings, arrays, functions, FILE.
Table of Contents
Here are some important questions for Class 12 Computer Science in NEB board related to C programming:
Theory Questions:
- What is C? Write its Features.
- What are the advantages and disadvantages of C programming Languages?
- What is a Data type? Explain the type of data with examples.
- What is a variable? Explain its types.
- What is an operator? Write its types and explain any four of them.
- What is a control structure? Write a difference between break and continue statements with examples.
- What is looping? Write the difference between while and Do while loop with examples.
- Define the term array. What is the string? Explain any four-string handling function for example.
- What is the function? Write its features and describe its types.
- What is the recursion technique? Explain with one example.
- What is the concept of storage? Differentiate between automatic storage and external storage.
- What is a Structure? Explain with one example.
- Differentiate between array and structure.
- Differentiate between structure and union.
- Differentiate between array and pointer.
- Define the terms call by value and call by reference with examples.
- What is a pointer? Explain with examples.
- Differentiate between Structure and Pointer.
- Differentiate between sequential access and random access techniques of the data file.
- Differentiate between the fprintf and fscanf functions.
Program Questions:
- Write a C program to generate Fibonacci series. [ 2, 3, 5, 8 ……10th term]
- Write a C program to input a number and check whether it is odd or even.
- Write a C program to input a number and check whether it is exactly divisible by 5 but not by 7.
- Write a C program to input a number and print multiplication table of given number.
- Write a C program to find greatest number among 3 number.
- Write a C program to calculate sum of n-natural number.
- Write a C program to reverse a given number.
- Write a C program to check whether given string is palindrome or not.
- Write a C program to count total vowels from given string.
- Write a C program to calculate factorial of a given number using recursive function.
- Write a C program to find greatest number among n numbers.
- Write a C program to arrange n numbers in ascending order.
- Write a C program to read salary of 300 employees and count the number of employee getting salary between 30000 to 50000.
- Write a C program to read two 3x3 matrices and perform matrix addition/multiplication.
- Write a C program to enter name, roll number and percentage of 10 student and store them in a data file [use any name]
- A datafile “patient.txt” contain name, disease , age and bed number of few patient. Write a C program to read and display records of all patients in appropriate format.
- A datafile “patient.txt” contain name, disease , age and bed number of few patient. Write a C program to read and display only records of patients suffering from “FEVER”.
- Write a C program to enter book name, book id and book author of few book and store them in a data file named “library.txt”. Display each records in screen in appropriate format.
Solution of this Program Question
Important C Programming Questions for Class 12 Computer Science in NEB Board
Write a C program to generate Fibonacci series. [ 2, 3, 5, 8 ……10th term]
Here is a C program to generate the Fibonacci series up to the 10th term, which is {2, 3, 5, 8, 13, 21, 34, 55, 89, 144}:
#include<stdio.h>
int main() {
int a = 2, b = 3, c, i;
printf("%d, %d, ", a, b); // print the first two terms of the series
for (i = 3; i <= 10; i++) {
c = a + b; // calculate the next term of the series
printf("%d, ", c);
a = b;
b = c;
}
return 0;
}
Write a C program to input a number and check whether it is odd or even.
Here is a C program to input a number from the user and check whether it is odd or even:
#include<stdio.h>
int main() {
int num;
printf("Enter an integer: ");
scanf("%d", &num);
if(num % 2 == 0) {
printf("%d is even.", num);
} else {
printf("%d is odd.", num);
}
return 0;
}
Write a C program to input a number and check whether it is exactly divisible by 5 but not by 7.
Here is a C program to input a number from the user and check whether it is odd or even:
#include<stdio.h>
int main() {
int num;
printf("Enter an integer: ");
scanf("%d", &num);
if(num % 5 == 0 && num % 7 != 0) {
printf("%d is divisible by 5 but not by 7.", num);
} else {
printf("%d is not divisible by 5 or is divisible by 7.", num);
}
return 0;
}
Write a C program to input a number and print multiplication table of given number.
Here is a C program to input a number from the user and print the multiplication table of that number:
#include<stdio.h>
int main() {
int num, i;
printf("Enter an integer: ");
scanf("%d", &num);
printf("Multiplication table of %d:\n", num);
for (i = 1; i <= 10; i++) {
printf("%d x %d = %d\n", num, i, num*i);
}
return 0;
}
Write a C program to find greatest number among 3 number.
Here is a C program to input 3 numbers from the user and find the greatest number among them:
#include<stdio.h>
int main() {
int num1, num2, num3, max;
printf("Enter three integers: ");
scanf("%d %d %d", &num1, &num2, &num3);
max = num1;
if (num2 > max) {
max = num2;
}
if (num3 > max) {
max = num3;
}
printf("The greatest number is %d", max);
return 0;
}
Write a C program to calculate sum of n-natural number.
Here is a C program to calculate the sum of the first n natural numbers:
#include<stdio.h>
int main() {
int n, sum = 0, i;
printf("Enter a positive integer: ");
scanf("%d", &n);
for (i = 1; i <= n; i++) {
sum += i;
}
printf("Sum of first %d natural numbers = %d", n, sum);
return 0;
}
Write a C program to reverse a given number.
Here is a C program to reverse a given number:
#include<stdio.h>
int main() {
int num, rev = 0, rem;
printf("Enter an integer: ");
scanf("%d", &num);
while (num != 0) {
rem = num % 10;
rev = rev * 10 + rem;
num /= 10;
}
printf("Reverse of the number = %d", rev);
return 0;
}
Write a C program to check whether given string is palindrome or not.
Here is a C program to check whether a given string is a palindrome or not:
#include<stdio.h>
#include
int main() {
char str[100];
int i, j, len, flag = 0;
printf("Enter a string: ");
gets(str);
len = strlen(str);
for (i = 0, j = len - 1; i < len / 2; i++, j--) {
if (str[i] != str[j]) {
flag = 1;
break;
}
}
if (flag) {
printf("%s is not a palindrome", str);
} else {
printf("%s is a palindrome", str);
}
return 0;
}
Write a C program to count total vowels from given string.
Here is a C program to count the total number of vowels in a given string:
#include<stdio.h>
#include<string.h>
int main() {
char str[100];
int i, count = 0, len;
printf("Enter a string: ");
gets(str);
len = strlen(str);
for (i = 0; i < len; 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("Total vowels in the string: %d", count);
return 0;
}
Write a C program to calculate factorial of a given number using recursive function.
Here is a C program to calculate the factorial of a given number using a recursive function:
#include<stdio.h>
int factorial(int n) {
if (n == 0) {
return 1;
} else {
return n * factorial(n - 1);
}
}
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
if (num < 0) {
printf("Factorial of negative numbers is not defined");
} else {
printf("Factorial of %d is %d", num, factorial(num));
}
return 0;
}
Write a C program to find greatest number among n numbers.
Here is a C program to find the greatest number among n numbers:
#include<stdio.h>
int main() {
int n, i, num;
int max = 0;
printf("Enter the value of n: ");
scanf("%d", &n);
for (i = 1; i <= n; i++) {
printf("Enter number %d: ", i);
scanf("%d", &num);
if (num > max) {
max = num;
}
}
printf("The greatest number is %d", max);
return 0;
}
Write a C program to arrange n numbers in ascending order.
Here is a C program to arrange n numbers in ascending order using the bubble sort algorithm:
#include<stdio.h>
int main() {
int n, i, j, temp;
int nums[100];
printf("Enter the number of elements: ");
scanf("%d", &n);
printf("Enter the elements: ");
for (i = 0; i < n; i++) {
scanf("%d", &nums[i]);
}
// Bubble sort
for (i = 0; i < n - 1; i++) {
for (j = 0; j < n - i - 1; j++) {
if (nums[j] > nums[j + 1]) {
temp = nums[j];
nums[j] = nums[j + 1];
nums[j + 1] = temp;
}
}
}
printf("The elements arranged in ascending order are: ");
for (i = 0; i < n; i++) {
printf("%d ", nums[i]);
}
return 0;
}
Write a C program to read salary of 300 employees and count the number of employee getting salary between 30000 to 50000.
Here is a C program to read the salary of 300 employees and count the number of employees getting a salary between 30000 to 50000:
#include<stdio.h>
int main() {
int i, count = 0;
float salary;
printf("Enter the salary of 300 employees:\n");
for (i = 1; i <= 300; i++) {
printf("Employee %d: ", i);
scanf("%f", &salary);
if (salary >= 30000 && salary <= 50000) {
count++;
}
}
printf("Number of employees with salary between 30000 to 50000: %d", count);
return 0;
}
Write a C program to read two 3x3 matrices and perform matrix addition/multiplication.
Here is a C program to read two 3x3 matrices and perform matrix addition and multiplication:
#include<stdio.h>
int main() {
int matrix1[3][3], matrix2[3][3], result[3][3], i, j, k, sum;
// Read matrix1
printf("Enter the elements of matrix1:\n");
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
printf("matrix1[%d][%d]: ", i, j);
scanf("%d", &matrix1[i][j]);
}
}
// Read matrix2
printf("Enter the elements of matrix2:\n");
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
printf("matrix2[%d][%d]: ", i, j);
scanf("%d", &matrix2[i][j]);
}
}
// Matrix addition
printf("\nMatrix Addition:\n");
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
result[i][j] = matrix1[i][j] + matrix2[i][j];
printf("%d\t", result[i][j]);
}
printf("\n");
}
// Matrix multiplication
printf("\nMatrix Multiplication:\n");
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
sum = 0;
for (k = 0; k < 3; k++) {
sum = sum + matrix1[i][k] * matrix2[k][j];
}
result[i][j] = sum;
printf("%d\t", result[i][j]);
}
printf("\n");
}
return 0;
}
Write a C program to enter name, roll number and percentage of 10 student and store them in a data file [use any name]
Here's a C program that prompts the user to enter the name, roll number, and percentage of 10 students and stores them in a data file named "student.dat":
#include<stdio.h>
#include<stdlib.h>
typedef struct {
char name[30];
int roll;
float percentage;
} Student;
int main() {
FILE *fp;
Student student;
int i;
fp = fopen("student.dat", "w");
if (fp == NULL) {
printf("Error: Unable to open file!\n");
exit(1);
}
for (i = 1; i <= 10; i++) {
printf("Enter name, roll number, and percentage of student %d: ", i);
scanf("%s %d %f", student.name, &student.roll, &student.percentage);
// Write student record to file
fwrite(&student, sizeof(student), 1, fp);
}
fclose(fp);
return 0;
}
A datafile “patient.txt” contain name, disease , age and bed number of few patient. Write a C program to read and display records of all patients in appropriate format.
Here's a C program that reads and displays the records of all patients from the "patient.txt" file:
#include<stdio.h>
typedef struct {
char name[30];
char disease[30];
int age;
int bed_num;
} Patient;
int main() {
FILE *fp;
Patient patient;
fp = fopen("patient.txt", "r");
if (fp == NULL) {
printf("Error: Unable to open file!\n");
return 1;
}
printf("%-20s %-20s %-10s %-10s\n", "Name", "Disease", "Age", "Bed No.");
printf("------------------------------------------------------------\n");
while (fread(&patient, sizeof(Patient), 1, fp) == 1) {
printf("%-20s %-20s %-10d %-10d\n", patient.name, patient.disease, patient.age, patient.bed_num);
}
fclose(fp);
return 0;
}
A datafile “patient.txt” contain name, disease , age and bed number of few patient. Write a C program to read and display only records of patients suffering from “FEVER”.
Here's a C program that reads and displays only the records of patients suffering from "FEVER" from the "patient.txt" file:
#include<stdio.h>
#include<string.h>
typedef struct {
char name[30];
char disease[30];
int age;
int bed_num;
} Patient;
int main() {
FILE *fp;
Patient patient;
fp = fopen("patient.txt", "r");
if (fp == NULL) {
printf("Error: Unable to open file!\n");
return 1;
}
printf("%-20s %-20s %-10s %-10s\n", "Name", "Disease", "Age", "Bed No.");
printf("------------------------------------------------------------\n");
while (fread(&patient, sizeof(Patient), 1, fp) == 1) {
if (strcmp(patient.disease, "FEVER") == 0) {
printf("%-20s %-20s %-10d %-10d\n", patient.name, patient.disease, patient.age, patient.bed_num);
}
}
fclose(fp);
return 0;
}
Write a C program to enter book name, book id and book author of few book and store them in a data file named “library.txt”. Display each records in screen in appropriate format.
Here's a C program that reads book name, book id, and book author of few books from the user and stores them in a data file named "library.txt". The program then displays each record in an appropriate format:
#include<stdio.h>
typedef struct {
char name[50];
int id;
char author[50];
} Book;
int main() {
FILE *fp;
Book book;
int num_books, i;
printf("Enter the number of books: ");
scanf("%d", &num_books);
fp = fopen("library.txt", "w");
if (fp == NULL) {
printf("Error: Unable to open file!\n");
return 1;
}
printf("\nEnter the details of each book:\n");
for (i = 0; i < num_books; i++) {
printf("\nBook %d:\n", i+1);
printf("Name: ");
scanf("%s", book.name);
printf("ID: ");
scanf("%d", &book.id);
printf("Author: ");
scanf("%s", book.author);
fwrite(&book, sizeof(Book), 1, fp);
}
fclose(fp);
fp = fopen("library.txt", "r");
if (fp == NULL) {
printf("Error: Unable to open file!\n");
return 1;
}
printf("\n%-30s %-10s %-20s\n", "Book Name", "Book ID", "Author");
printf("--------------------------------------------------------------\n");
while (fread(&book, sizeof(Book), 1, fp) == 1) {
printf("%-30s %-10d %-20s\n", book.name, book.id, book.author);
}
fclose(fp);
return 0;
}
These are some important questions for Class 12 Computer Science students who are studying C programming in the NEB board.