Top 15 Practical Exam C Language Program in Computer Science

  • Write A C Program to find the sum of series S=1+2+3+….+n

    “Program:”

    
    

    #include<stdio.h>

    int main() {

    int n,i;

    int sum=0;

    printf(“Enter the n i.e. max values of series: “);

    scanf(“%d”,&n);

    sum = (n * (n + 1)) / 2;

    printf(“Sum of the series: “);

    for (i =1;i <= n;i++) {

    if (i!=n)

    {

    printf(“%d + “,i);

    else

    printf(“%d = %d “,i,sum);

    }

    return 0;

    }

     

“Output:”


Enter the n i.e. max values of series: 5

Sum of the series: 1 + 2 + 3 + 4 + 5 = 15

 

Write a C program to find the Reverse Number:

“Program:”


#include <stdio.h>

int main()

{

int n, reverse = 0, remainder;

printf(“Enter an integer: “);

scanf(“%d”, &n);

while (n != 0)

{

remainder = n % 10;

reverse = reverse * 10 + remainder;

n /= 10;

}

printf(“Reversed number = %d”, reverse);

return 0;

}

“Output:”


Enter an integer: 2345

Reversed number = 5432

 

Write A CProgram to find Sum of Digit .

“Program:”


#include<stdio.h>

int main()

{

int n,sum=0,m;

printf(“Enter a number:”);

scanf(“%d”,&n);

while(n>0)

{

m=n%10;

sum=sum+m;

n=n/10;

}

printf(“Sum is=%d”,sum);

return 0;

}

 

“Output:”


Enter a number:654

Sum is=15

Enter a number:123

Sum is=6

Write A Program to find Digit on not:

“Program:”


#include<stdio.h>

#include<conio.h>

int main()

{

char ch;

clrscr();

printf(“Enter chracter: “);

scanf(“%c”, &ch);

if(ch>=’0′ && ch<=’9′)

{

printf(“%c is DIGIT.”, ch);

}

else

{

printf(“%c is NOT DIGIT.”, ch);

}

getch();

return(0);

}

“Output:”


Enter any character: 9 ↲

9 is DIGIT.

———-

Enter any character: G ↲

G is NOT DIGIT.

Write A  C Program to find  a Number prime on not:

“Program:”


#include <stdio.h>

int main()

{

int n, i, flag = 0;

printf(“Enter a positive integer: “);

scanf(“%d”, &n);

if (n == 0 || n == 1)

flag = 1;

for (i = 2; i <= n / 2; ++i)

{

if (n % i == 0) {

flag = 1;

break;

}

}

if (flag == 0)

printf(“%d is a prime number.”, n);

else

printf(“%d is not a prime number.”, n);

return 0;

}

“Output:”


Enter a positive integer: 56

56 is not a prime number.

Enter a positive integer: 29

29 is a prime number.

 

Write a C program to find S= 1-2 + 3-4 + 5-6 +7-8+. . . .+n

“Program:”


#include <stdio.h>

int series_sum(int n)

{

if (n % 2 == 0)

return (-(n/ 2));

else

return ((n + 1) / 2);

}

int main()

{

int n;

printf(“Series: 1-2+3-4+5-6+7-8…..N\n”);

printf(“Enter the N term value in Integer:”);

scanf(“%d”, &n);

printf(“Sum is: %d”, series_sum(n));

return 0;

}

“Output:”


Series: 1-2+3-4+5-6+7-8…..N

Want some up to N terms?

Enter the N term:20

Sum is: -1

 

Write A  C Program Swap with macro:

“Program:”


#include <stdio.h>

int a,b,t;

int main()

{

printf(“Enter a The Number of a “);

scanf(“%d”, &a);

printf(“Enter The Number of b “);

scanf(“%d”, &b);

t=a;

a=b;

b=t;

printf(“After  swapping   value:\n a= %d \t b=%d “,a,b);

return 0;

}

“Output:”


Enter a The Number of a 15

Enter The Number of b 20

After  swapping   value:

a= 20    b=15

 

Write a C Program to print a triangle of stars as follows (take a number of lines from users).

“Program:”


#include <stdio.h>

int main() {

int i, space, rows, k = 0;

printf(“Enter the number of rows: “);

scanf(“%d”, &rows);

for (i = 1; i <= rows; ++i, k = 0) {

for (space = 1; space <= rows – i; ++space) {

printf(”  “);

}

while (k != 2 * i – 1) {

printf(“* “);

++k;

}

printf(“\n”);

}

return 0;

}

 

Write a C Program to check the factor of a number.

“Program:”


#include <stdio.h>

int main()

{

int num, i;

printf(“Enter a positive integer: “);

scanf(“%d”, &num);

printf(“Factors of %d are: “, num);

for (i = 1; i <= num; ++i)

{

if (num % i == 0)

{

printf(“%d “, i);

}

}

return 0;

}

“Output:”


Enter a positive integer: 50

Factors of 50 are: 1 2 5 10 25 50

 

Write a c Program to calculate the factorial of a number using a recursive function.

“Program:”


#include<stdio.h>

long factorial(int n)

{

if (n == 0)

return 1;

else

return(n * factorial(n-1));

}

 

void main()

{

int number;

long fact;

printf(“Enter a number: “);

scanf(“%d”, &number);

fact = factorial(number);

printf(“Factorial of %d is %ld\n”, number, fact);

return 0;

}

 

“Output:”


Enter a number: 8

Factorial of 8 is 40320

 

Write a C Program to find the Maximum and minimum element of array.

“Program:”


#include <stdio.h>

void main()

{

int arr1[100];

int i, mx, mn, n;

printf(“Enter How Many Number In The Array :”);

scanf(“%d”,&n);

printf(“Input %d elements in the array :\n”,n);

for(i=1;i<n;i++)

{

printf(“Enter The Number %d: “,i);

scanf(“%d”,&arr1[i]);

}

mx = arr1[0];

mn = arr1[0];

for(i=1; i<n; i++)

{

if(arr1[i]>mx)

{

mx = arr1[i];

}

if(arr1[i]<mn)

{

mn = arr1[i];

}

}

printf(“Maximum element is : %d\n”, mx);

printf(“Minimum element is : %d\n\n”, mn);

}

“Output:”


Enter How Many Number In The Array :5

Input 5 elements in the array :

element – 0 : 20

element – 1 : 22

element – 2 : 23

element – 3 : 25

element – 4 : 56

Maximum element is : 56

Minimum element is : 20

 

Write a c Program to find odd and even numbers of an array.

“Program:”


#include <stdio.h>

void main()

{

int n;

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

scanf(“%d”, &n);

int arr[n];

printf(“Enter %d elements in the array: “,n);

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

{

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

}

printf(“Even numbers in the array are: “);

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

{

if(arr[i]%2==0)

printf(“%d “, arr[i]);

}

printf(“\nOdd numbers in the array are: “);

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

{

if(arr[i]%2==1)

printf(“%d “, arr[i]);

}

}

“Output:”


Enter the number of elements in the array: 4

Enter 4 elements in the array: 8 9 6 5

Even numbers in the array are: 8 6

Odd numbers in the array are: 9 5

Write a Program to Display the Fibonacci series in C Language.

“Program:”


#include <stdio.h>

int main()

{

int i, n;

int t1 = 0, t2 = 1

int nextTerm = t1 + t2;

printf(“Enter the number of terms: “);

scanf(“%d”, &n);

printf(“Fibonacci Series: %d, %d, “, t1, t2);

for (i = 3; i <= n; ++i)

{

printf(“%d, “, nextTerm);

t1 = t2;

t2 = nextTerm;

nextTerm = t1 + t2;

}

return 0;

}

“Output:”


Enter the number of terms: 9

Fibonacci Series: 0, 1, 1, 2, 3, 5, 8, 13, 21,

Add a Comment

Your email address will not be published. Required fields are marked *