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,

Basic Concept of Java Languages (Intro)

~ JAVA~

What is Java?

JAVA is a programming language and a platform. JAVA is a high-level, robust, object-oriented, and secure programming language.

JAVA was developed by sun microsystems (which is now a subsidiary of Oracle) in the year 1995. James Gosling is known as the father of JAVA  Before JAVA its name was oak. Since oak was already a registered company So, James Gosling and his team changed the oak name to JAVA.

It was developed by a standard internet application (concept of the applet) that provides interpreting processing for the use of Graphics and animation on the internet. It was also developed by the truly oop concept and also supports multiple platforms.

What is Platform?

Any hardware or software environment in which a program run is known as a platform. since JAVA has a runtime environment (JRE) and API, it is called a platform.

Types of JAVA application:-

There are mainly 4 types of applications that can be created using java programming.

1. Standalone Application: –

Standalone applications are also known as desktop applications or window-based applications. These are traditional software that we need to install on every machine. Example of standalone application is media player, antivirus, etc. AWT and swing are used in JAVA for creating standalone applications.

2.web Application: –

An application that runs on the server slide and creates a dynamic page is called a web application, currently, servlet, JSP, struts, spring, hibernate JSF, etc. technologies are used in JAVA for creating a web application in JAVA.

3. Enterprise Application: –

An application that is disturbed in nature such as a banking application etc is called an enterprise application. It has the advantage of high-level security, load balancing, and clustering, in JAVA EJB is used in JAVA for creating enterprise applications.

4. Mobile Application: –

An application that is created for mobile devices is called a mobile application. Currently, Android and JAVA ME are used for creating a mobile applications.

JAVA Platforms /Editions:-

There are 4 platforms or editions of JAVA.

1.JAVA SE (JAVA Standard Edition):

It is a JAVA programming platform. It includes JAVA Programming APIS such as JAVA.io, JAVA.net, JAVA.unit, JAVA.sql, and java—math etc.

It includes core topics like oops string, Regex, Exception, Inner classes Multithreading, I/O stream, networking, AWT, Swing, Reflection collection, etc.

2.JAVA EE (JAVA Enterprise edition): –

It is an enterprise platform that is mainly used to develop web and enterprise applications. It is built on top of the JAVA SE platform. it includes topics like servlet JSP, WEB Service, EJB, JPA, etc.

3.JAVA ME (JAVA Micro Editor): –

It is a micro platform that is mainly used to develop mobile applications.

4. JAVA FX: –

It is used to develop rich internet applications it uses a lightweight user interface API.

1. Write a Java Program to add Two Numbers?

Title


import java. Lang.*;

import java. Unit. scanner;

class add

{

Public static void main (string args[]}

{

Int a,b,s;

Scanner obj= new scanner (system. In);

System. Out. print in (“Enter the value of a,b”);

a=obj.nextInt();

b=obj. nextInt();

s=a=b;

system. Out println(“sum=+s);

}

}

2. Write a Java Program to the interest of a given number?

Title


import java. Lang*;

Import java. util.Scanner;

Class Add

{

Public Static void main (string args[])

{ int i,P,t,r;

Scanner obj=New Scanner(System.in)

System.out.println(“Enter The Value Of I,p,t,r);

I=obj.nextInt();

P=obj.nextInt();

t=objnextInt();

r=obj.nextInt();

i=((p*t*r)/100);

System.out println(“Result=”+i);

}

}

3. Write a Java program to find the Sum of two numbers?

Title


//Sum of two numbers

Import java.lang.*;

Import java.util.Scanner;

Class Add

{

Void cal(int a, Int b)

{ int s;

S=a+b;

System.Out.Println(“Sum=”+s);

}

}

Class Sum

{ public stats void main (string Args[])

{

Int a,b;

Scanner obj=New Scanner(System.in);

System.out.println(“Enter The Value of a,b”);

A=obj.nextInt();

B=obj.nextInt();

Add obj1=new Add();

Obj1.cal(a,b)

}

}