Here are some of the most repeated C Program in NEB Grade 12
Board Exam.
Function
1.Write a C
program to generate Fibonacci series. [ 1,1, 2, ………..10th term].
#include <stdio.h>
void abc();
int main()
{
abc();
}
void abc()
{
int a=0,b=1,c=1,i;
for (i=0;i<10;i++)
{
printf("%5d",c);
c=a+b;
a=b;
b=c;
}
}
2.Write a C
program to input a number and check whether it is odd or even.
#include <stdio.h>
void abc(int a);
int main()
{
int a;
printf("Enter Any Number:");
scanf("%d",&a);
abc(a);
}
void abc(int a)
{
int b;
b=a%2;
if (b==0)
printf("Even Number.");
else
printf("Odd Number.");
}
3.Write a C
program to input a number and check whether it is exactly divisible by 5 but
not by 7.
#include <stdio.h>
void abc(int a);
int main()
{
int a;
printf("Enter Any Number:");
scanf("%d",&a);
abc(a);
}
void abc(int a)
{
int b,c;
b=a%5;
c=a%7;
if (b==0&&c!=0)
printf("It is exactly divisible by 5 but not by 7.");
else
printf("Invalid as Per Need of Question.");
}
4.Write a C
program to input a number and print multiplication table of given number.
#include <stdio.h>
void abc(int a);
int main()
{
int a;
printf("Enter Any Number:");
scanf("%d",&a);
abc(a);
}
void abc(int a)
{
int i;
for (i=1;i<=10;i++)
{
printf("%d * %d =%d\n",a,i,a*i);
}
}
5.Write a C
program to find greatest number among 3 number.
#include <stdio.h>
void abc(int a, int b, int c);
int main()
{
int a,b,c;
printf("Enter First Number:");
scanf("%d",&a);
printf("Enter Second Number:");
scanf("%d",&b);
printf("Enter Third Number:");
scanf("%d",&c);
abc(a,b,c);
}
void abc(int a, int b, int c)
{
if (a>b && a>c)
printf("%d is Greatest Among All Three Program.",a);
else if(b>a && b>c)
printf("%d is Greatest Among All Three Program.",b);
else
printf("%d is Greatest Among All Three Program.",c);
}
6.Write a C
program to calculate sum of n-natural number.
#include <stdio.h>
int sum (int);
int main()
{
int n,s;
printf("Enter any number");
scanf("%d",&n);
s = sum(n);
printf("Sum is %d\n",s);
return 0;
}
int sum (int n)
{
if (n<=0)
return 0;
else
return (n+sum(n-1));
}
7. Write
a C program to reverse a given number.
#include<stdio.h>
void rev(int num);
void main()
{
int num;
printf(“Enter a number”);
scanf(“\n%d”,&num);
rev(n);
}
void rev(int num)
{
int tmp,reverse;
tmp=num%10;
num=num/10;
reverse=10*tmp+num;
printf(“\nReverses number is %d”,reverse);
}
8.Write a C
program to check whether given string is palindrome or not.
#include <stdio.h>
#include <string.h>
int checkpalindrome(char *s)
{
int i,c=0,n;
n=strlen(s);
for(i=0;i<n/2;i++)
{
if(s[i]==s[n-i-1])
c++;
}
if(c==i)
return 1;
else
return 0;
}
int main()
{
char s[1000];
printf("Enter the string: ");
gets(s);
if(checkpalindrome(s))
printf("string is palindrome");
else
printf("string is not palindrome");
}
9.Write a C
program to count total vowels from given string.
#include <stdio.h>
#include <string.h>
void stringcount(char *s)
{
int i,vowels=0,consonants=0;
for(i=0;s[i];i++)
{
if((s[i]>=65 && s[i]<=90)|| (s[i]>=97&& s[i]<=122))
{
if(s[i]=='a'||s[i]=='e'||s[i]=='i'||s[i]=='o'||s[i]=='u'||s[i]=='A'||s[i]=='E'||s[i]=='I'||s[i]=='O'||s[i]=='U')
vowels++;
else
consonants++;
}
}
printf("vowels = %d\n",vowels);
printf("consonants = %d\n",consonants);
}
int main()
{
char s[1000];
printf("Enter the string: ");
gets(s);
stringcount(s);
}
10. Write a C program to calculate
factorial of a given number using recursive function.
#include<stdio.h>
long int multiplyNumbers(int n);
int main()
{
int n;
printf("Enter a positive integer: ");
scanf("%d",&n);
printf("Factorial of %d = %ld", n, multiplyNumbers(n));
return 0;
}
long int multiplyNumbers(int n)
{
if (n>=1)
return n*multiplyNumbers(n-1);
else
return 1;
}
11. Write a C program to find
greatest number among 10/n numbers.
#include <stdio.h>
int main()
{
int a[10];
int i;
int greatest;
printf("Enter ten values:");
for (i = 0; i < 10; i++)
{
scanf("%d", &a[i]);
}
greatest = a[0];
for (i = 0; i < 10; i++)
{
if (a[i] > greatest)
{
greatest = a[i];
}
}
printf("Greatest of ten numbers is %d", greatest);
return 0;
}
12. Write a C program to arrange 10/n
numbers in ascending order.
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,temp,a[10];
printf(“Enter 10 integer numbers: \n”);
for(i=0;i<10;i++)
scanf(“%d”,&a[i]);
for (i=0;i<10;i++)
{
for(j=i+1;j<10;j++)
{
if(a[i]>a[j])
{
temp=a[j];
a[j]=a[i];
a[i]=temp;
}
}
}
printf(“\n\nThe 10 numbers sorted in ascending order are: \n”);
for(i=0;i<10;i++)
printf(“%d\t”,a[i]);
}
13. Write a C program to read two 3x3
matrices and perform matrix addition/multiplication.
#include<stdio.h>
int main()
{
int a[3][3],b[3][3],c[3][3],i,j,k,sum;
printf("\nEnter the matrix elements of A\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",&a[i][j]);
}
printf("\n");
}
printf("\nEnter the matrix elements of B\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",&b[i][j]);
}
printf("\n");
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
sum=0;
for(k=0;k<3;k++)
{
sum=sum+a[i][k]*b[k][j];
}
c[i][j]=sum;
}
}
printf("\nProduct of two matrix is:\n\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("%d",c[i][j]);
printf("\t");
}
printf("\n\n");
}
return 0;
}
14. Write a C program to arrange
10/n numbers in descending order.
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,temp,a[10];
printf(“Enter 10 integer numbers: \n”);
for(i=0;i<10;i++)
scanf(“%d”,&a[i]);
for (i=0;i<10;i++)
{
for(j=i+1;j<10;j++)
{
if(a[i]<a[j])
{
temp=a[j];
a[j]=a[i];
a[i]=temp;
}
}
}
printf(“\n\nThe 10 numbers sorted in decending order are:\n”);
for(i=0;i<10;i++)
printf(“%d\t”,a[i]);
}
File Handling
15. A datafile “patient.txt” contain
name, disease, age and bed number of few patients. Write a C program to read
and display only records of patients suffering from “COVID”.
#include <stdio.h>
int main()
{
char n[10], d[10];
int a, b;
FILE *fptr;
fptr = fopen(“patient.txt”,”w”);
printf("Enter name, disease, age and bed number");
scanf(“%s %s %d %d”, n, d, &a, &b);
fprintf(fptr,"%s %s %d %d\n”, n, d, a, b);
fclose(fptr);
return 0;
}
16. Create a datafile “student.txt” and
store name, class and marks obtained in 3 different subject until user press
“y” / as per user requirement.
#include <stdio.h>
int main()
{
char n[10],ch[3];
int c, e, ne, m;
FILE *fptr;
fptr = fopen("student.txt","w");
do
{
printf("Enter name class and 3 marks");
scanf("%s %d %d %d %d",n, &c, &e, &ne,&m);
fprintf(fptr,"%s %d %d %d %d\n",n, c, e, ne, m);
printf("Press Y to continue");
scanf("%s",ch);
} while (strcmp(ch,"Y") == 0 ||strcmp(ch,"y")==0);
fclose(fptr);
return 0;
}
17. A datafile “student.txt” contain
name, class and marks obtained in 3 different subject of few students. Write a
C program to add 200 more records.
#include <stdio.h>
int main()
{
char n[10];
int c, e, ne, m, i;
FILE *fptr;
fptr = fopen("student.txt","a");
for(i=1;i<=200;i++)
{
printf("Enter name class and 3 marks");
scanf("%s %d %d %d %d", n, &c, &e,&ne, &m);
fprintf(fptr,"%s %d %d %d %d \n",n, c, e, ne, m);
}
fclose(fptr);
return 0;
}
18. A datafile “student.txt” contain
name, class and marks obtained in 3 different subjects of few students. Write a
C program to add more records until user press “y” / as per user requirement.
#include <stdio.h>
int main()
{
char n[10], ch[3];
int c, e, ne, m;
FILE *fptr;
fptr = fopen("student.txt”,”a”);
do
{
printf("Enter name class and 3 marks");
scanf("%s %d %d %d %d", n, &c, &e,&ne, &m);
fprintf(fptr,"%s %d %d %d %d\n", n, c, e, ne, m);
printf("Press Y to continue");
scanf("%s",ch);
}
while (strcmp(ch,"Y") == 0 ||strcmp(ch,"y")==0);
fclose(fptr);
return 0;
}