0% found this document useful (0 votes)
58 views44 pages

C Lab Manual

The document describes a C program to find the largest element in an array. It includes the following steps: 1. Define an integer variable n to store the size of the array and declare a double array with size 100 to store elements. 2. Read elements into the array. 3. Iterate through the array and keep track of the maximum element seen so far, updating it if a larger element is found. 4. After iterating, the maximum element variable will be the largest element in the array. Print this value.

Uploaded by

lakshmi.s
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
58 views44 pages

C Lab Manual

The document describes a C program to find the largest element in an array. It includes the following steps: 1. Define an integer variable n to store the size of the array and declare a double array with size 100 to store elements. 2. Read elements into the array. 3. Iterate through the array and keep track of the maximum element seen so far, updating it if a larger element is found. 4. After iterating, the maximum element variable will be the largest element in the array. Print this value.

Uploaded by

lakshmi.s
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 44

EX.

NO: 01
TO FIND SUM AND AVERAGE OF TWO NUMBERS

AIM
To create a C Program for addition of two numbers

ALGORITHM

STEP 1: Start the program.

STEP 2: Read the values of ‘a’&’b’.

STEP 3: Compute the sum of the entered numbers ‘a’,’b’,c=a+b.

STEP 4: Print the value of ‘c’.

STEP 5: Find the average of c.

STEP 6: Stop the program.


SOURCE CODE:

#include<stdio.h>

#include<conio.h>

void main()

inta,b,sum;

floatavg;

clrscr();

printf("\n\t\t TO FIND SUM AND AVERAGE OF TWO NUMBERS");

printf("\n\t\t ---------------------------------------\n");

printf("\n\t Enter first number:");

scanf("%d",&a);

printf("\n\t Enter second number:");

scanf("%d",&b);

sum=a+b;

avg=(float)(a+b)/2;

printf("\n\t\t OUTPUT");

printf("\n\t\t -------");

printf("\n\t Sum of %d and %d is =%d",a,b,sum);

printf("\n\t Average of %d and %d is=%f",a,b,avg);

getch();

2|P a ge
OUTPUT

RESULT

3|P a ge
EX.NO: 02
LIST THE SUM OF FIRST 10 EVEN NUMBER

AIM

To create a C program using find the sum of first 10 even numbers

ALGORITHM

Step 1: Start the program

Step 2: write the sum of first 10 even numbers

Step 3: Add integer of 1st 10 even numbers

Step 4: s+n

Step 5: n+2

Step 6: stop

4|P a ge
SOURCE CODE

#include<stdio.h>

#include<conio.h>

void main()

inti,n=2,s=0;

clrscr();

printf("\n\t\t LIST THE SUM OF FIRST 10 EVEN NUMBER");

printf("\n\t\t-------------------------------------\n");

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

printf("%d\n",n);

s=s+n;

n=n+2;

printf("\n\t\t\t\t OUTPUT ");

printf("\n\t\t\t\t--------\n");

printf("\n\t sum of1st 10 even numbers=%d",s);

getch();

5|P a ge
OUTPUT:

RESULT

6|P a ge
EX.NO: 03
PERFECT NUMBER OR NOT

AIM

To create a C program using check whether the given number is perfect number or
not.

ALGORITHM

Step 1: declare int variables and initialized result=0.

Step 2: read number at runtime.

Step 3: for loop i=1;i<=number;i++

Condition satisfies

i. if(number%i==0)

ii. result=result+i;

Step 4: checking the sum of factors.

i. if(result==2*number)

ii. print perfect number

iii. else print not perfect number

Step 5:Stop

7|P a ge
SOURCE CODE

#include<stdio.h>

#include<conio.h>

void main()

intn,i=1,sum=0;

clrscr();

printf("\n\t\t PERFACT NUMBER OR NOT");

printf("\n\t\t ---------------------\n");

printf("\n\t Enter a number:");

scanf("%d",&n);

while(i<n)

if(n%i==0)

sum=sum+i;

i++;

if(sum==n)

printf("\n\t %d is a perfect number",i);

else

printf("\n\t %d is not perfect number",i);

getch();

8|P a ge
}

OUTPUT

RESULT

9|P a ge
EX.NO: 04 TO FIND ARMSTRONG OR NOT

AIM

To create a C program using check whether the given number is Armstrong


Number or not.

ALGORITHM

Step 1: Start

Step 2: Declare Variable sum, temp, num

Step 3: Read num from User

Step 4: Initialize Variable sum=0 and temp=num

Step 5: Repeat Until num>=0

5.1 sum=sum + cube of last digit i.e [(num%10)*(num%10)*(num%10)]

5.2 num=num/10

Step 6: IF sum==temp

Print "Armstrong Number"

ELSE

Print "Not Armstrong Number"

Step 7: Stop

10 | P a g e
SOURCE CODE

#include<stdio.h>

#include<conio.h>

void main()

intnumber,remainder,total=0,temp;

clrscr();

printf("\n\t\t TO FIND ARMSTRONG OR NOT");

printf("\n\t\t -------------------------\n");

printf("\n\t Enter the number=");

scanf("%d",&number);

temp=number;

while(number>0)

remainder=number%10;

total=total+(remainder*remainder*remainder);

number=number/10;

if(temp==total)

printf("\n\t This number is Armstrong number");

else

printf("\n\t This number is not Armstrong number");


getch();

11 | P a g e
}

OUTPUT

Enter an integer: 1634


1634 is an Armstrong number.

RESULT

12 | P a g e
EX.NO: 05
FIND OUT NUMBER IS PRIME OR NOT

AIM

To create a C program using check whether a number is prime number or not.

ALGORITHM

Step 1: Start

Step 2: Read number n

Step 3: Set f=0

Step 4: For i=2 to n-1

Step 5: If n mod 1=0 then

Step 6: Set f=1 and break

Step 7: Loop

Step 8: If f=0 then

print 'The given number is prime'

else

print 'The given number is not prime'

Step 9: stop

13 | P a g e
SOURCE CODE

#include<stdio.h>

#include<conio.h>

void main()

intn,i,c=0;

clrscr();

printf("\n\t\t FIND OUT NUMBER IS PRIME OR NOT");

printf("\n\t\t --------------------------------\n");

printf("\n\t Enter any number:");

scanf("%d",&n);

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

if(n%i==0)

c++;

printf("\n\t\t\t OUTPUT");

printf("\n\t\t\t -------\n");

if(c==2)

printf("\n\t The Given Number is prime Number");


}

14 | P a g e
Else
printf("\n\t The Given Number is not a prime Number");
getch();

OUTPUT

RESULT

15 | P a g e
EX.NO: 06
THE FACTORIAL OF A GIVEN NUMBER USING LOOP

AIM

To create a C program using Factorial of a given number using for loop.

ALGORITHM

Step 1: Start

Step 2: Read a number n

Step 2: Initialize variables:

i = 1, fact = 1

Step 3: if i <= n go to step 4 otherwise go to step 7

Step 4: Calculate

fact = fact * i

Step 5: Increment the i by 1 (i=i+1) and go to step 3

Step 6: Print fact

Step 7: Stop

16 | P a g e
SOURCE CODE

#include<stdio.h>

#include<conio.h>

void main()

inti,fact=1,number;

clrscr();

printf("\n\t\t THE FACTORIALOF A GIVEN NUMBER USING LOOP");

printf("\n\t\t ------------------------------------------\n");

printf("\n\t Enter a Number:");

scanf("%d",& number);

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

fact=fact*i;

printf("\n\t\t OUTPUT");

printf("\n\t\t ------");

printf("\n\t Factorial of %d is:%d",number, fact);

getch();

17 | P a g e
OUTPUT

RESULT

18 | P a g e
EX.NO: 07 CALCULATE SIMPLE INTEREST

AIM

To create a C program using calculate simple Interest.

ALGORITHM

Step 1 : START

Step 2 : Declare variables p , n , r and si .

Step 3 : Read the values of variable p ( principal ) , n ( Number of years ) , r ( Rate


of Interest ) .

Step 4 : calculate the values of “ si = ( p * n * r )/100 “ .

Step 5 : Display si ( simple interest ) .

Step 6 : STOP

19 | P a g e
SOURCE CODE

#include<stdio.h>

#include<conio.h>

void main()

intprinciple,rate,time,interest;

clrscr();

printf("\n\t\t CALCULATE SIMPLE INTEREST");

printf("\n\t\t -------------------------\n");

printf("\n\t Enter the principle:");

scanf("%d",&principle);

printf("\n\t Enter the rate:");

scanf("%d",&rate);

printf("\n\t Enter the time:");

scanf("%d",&time);

interest=principle*rate*time/100;

printf("\n\t\t OUTPUT");

printf("\n\t\t --------\n");

printf("\n\t The simple interest is%d",interest);

getch();

20 | P a g e
OUTPUT

RESULT

21 | P a g e
EX.NO: 08 STRING MANIPULATION USING SWICH CASE

AIM

To create a C program for string manipulation using switch case.

ALGORITHM

Step 1: Start the program

Step 2: Enter the values for the particular variables.

Step 3: To use the operators for to get the various outputs like +,-,/,*.

Step 4: Perform the operations using operators.

Step 5: Run the program and get the output.

Step 6: Try to get various outputs using various operators.

Step 7: Stop the Program.

22 | P a g e
SOURCE CODE

#include<stdio.h>

#include<conio.h>

void main()

char operation;

double n1,n2;

clrscr();

printf("\n\t\t STRING MANIPULATION USING SWICH CASE");

printf("\n\t\t ------------------------------------\n");

printf("\n\t Enter an operator(+,-,*,/):");

scanf("%c",& operation);

printf("\n\tEnter two operands:");

scanf("%lf %lf",&n1,&n2);

switch(operation)

case'+':

printf("\n\t %.1lf+%.1lf=%.1lf",n1,n2,n1+n2);

break;

case'-':

printf("\n\t %.1lf-%.1lf=%.1lf",n1,n2,n1-n2);

break;

case'*':

23 | P a g e
printf("\n\t %.1lf*%.1lf=%.1lf",n1,n2,n1*n2);

break;

case'/':

printf("\n\t %.1lf/%.1lf=%.1lf",n1,n2,n1/n2);

break;

defalut:

printf("erorr! opertor is not corrct");

getch();

24 | P a g e
OUTPUT

RESULT

25 | P a g e
EX.NO: 09 FIND THE LARGEST ELEMENT IN A GIVEN ARRAY OF
ELEMENT

AIM

Find The Largest Element In A Given Array Of Element.

ALGORITHM

Step 1: Start the program.

Step 2: Set a ← 10 and b ← 20

Step 3: Call the function swap(&a,&b)

Step 3a: Start fuction

Step 3b: Assign t ← *x

Step 3c: Assign *x ← *y

Step 3d: Assign *y ← t

Step 3e: End function

Step 4: Print x and y.

Step 5: Stop the program

26 | P a g e
SOURCE CODE

#include<conio.h>

#include<stdio.h>

void main()

int n;

doublearr[100];

clrscr();

printf("\n\t\t FIND THE LARGEST ELEMENT IN A GIVEN ARRAY OF


ELEMENT");

printf("\n\t\t -----------------------------------------------------\n");

printf("\n\t Enter the Number (1 to 100):");

scanf("%d",&n);

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

printf("\n\t Enter number %d:",i+1);

scanf("%lf",&arr[i]);

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

if(arr[0]<arr[i])

arr[0]=arr[i];

27 | P a g e
}

printf("\n\t Largest Element =%.2lf",arr[0]);


getch();
}

OUTPUT

Enter number1: 34.5


Enter number2: 2.4
Enter number3: -35.5
Enter number4: 38.7
Enter number5: 24.5
Largest element = 38.70

RESULT

28 | P a g e
EX.NO: 10 TO FIND DEMONSTRATE ANY 5 MATHEMATICAL
FUNCTION

AIM

To create the C program with use of mathematical functions to find the


various answers.

ALGORITHM

Step 1: Start the program.

Step 2: Enter the values for the variables.

Step 3: Print the mathematical functions to demonstrate with the values.

Step 4: Get the various outputs for corresponding mathematical functions.

Step 5: Run the program and get the output.

Step 5: Close the Program.

29 | P a g e
SOURCE CODE

#include<stdio.h>

#include<conio.h>

int main()

floatn,x;

clrscr();

printf("\n\t\t TO FIND DEMONSTRATE ANY 5 MATHEMATICAL


FUNCTION");

printf("\n\t\t ------------------------------------------------\n");

printf("\n\t Enter a floating point(decimal) number");

scanf("%f",&n);

x=ceil(n);

printf("ceil value of% f is%f",n,x);

return 0;

30 | P a g e
OUTPUT

Enter a floating point (decimal) number 12.4


Ceil value of 12.400000 is 13.00

RESULT

31 | P a g e
TO SWAP TWO NUMBERS USING CELL BY
EX.NO: 11
REFERENCE METHOD.

AIM

To swap two numbers using cell by reference method.

ALGORITHM

Step 1: Start the program.

Step 2: Set a ← 10 and b ← 20

Step 3: Call the function swap(&a,&b)

Step 3a: Start fuction

Step 3b: Assign t ← *x

Step 3c: Assign *x ← *y

Step 3d: Assign *y ← t

Step 3e: End function

Step 4: Print x and y.

Step 5: Stop the program

32 | P a g e
SOURCE CODE

#include <stdio.h>
swap (int *, int *);
int main()
{
int a, b;
printf("\nEnter value of a & b: ");
scanf("%d %d", &a, &b);
printf("\nBefore Swapping:\n");
printf("\na = %d\n\nb = %d\n", a, b);
swap(&a, &b);
printf("\nAfter Swapping:\n");
printf("\na = %d\n\nb = %d", a, b);
return 0;
}
swap (int *x, int *y)
{
int temp;
temp = *x;
*x = *y;
*y = temp;
}

33 | P a g e
OUTPUT

Enter value of a & b: 10 20

Before Swapping:

a = 10

b = 20

After Swapping:

a = 20

b = 10

RESULT

34 | P a g e
EX.NO: 12 DISPLAY THE DETAILS OF AN EMPLOYEE USING
STRUCTURES.

AIM

To accept and display the details of an employee using structures.

ALGORITHM

Step 1: Start the program.

Step 2: Read the employee’s details like name, employee id. Designation and
salary using structure

Step 3: print the entered values for mentioned field.

Step 4: Run the program and get the output.

Step 5: Close the Program.

35 | P a g e
SOURCE CODE

# include < stdio.h >


# include < conio.h >
struct employee
{
int e ;
char name[20] ;
char designation[20] ;
char dept[20] ;
int sal ;

};

int main( )
{
struct employee a ;
printf("\n Enter Employee Details:\n") ;
printf("-------------------------------\n") ;
printf(" Enter Employee-Id : ") ;
scanf("%d",&a.e ) ;
printf(" Enter Name : ") ;
scanf("%s",a.name ) ;
printf(" Enter Designation : " ) ;
scanf("%s",a.designation ) ;
printf(" Enter Department : " ) ;
scanf("%s",a.dept ) ;
printf(" Enter Salary : ") ;
scanf("%d",&a.sal ) ;
printf("-------------------------------") ;
printf("\n Employee Details: \n---------------------------------\n") ;
printf(" Employee-Id : %d\n",a.e ) ;
printf(" Name : %s\n",a.name ) ;
printf(" Designation : %s\n",a.designation ) ;
printf(" Department : %s\n",a.dept ) ;
printf(" Salary : %d\n",a.sal ) ;
return 0 ;
}

36 | P a g e
OUTPUT

RESULT

37 | P a g e
EX.NO: 13 DISPLAY LENGTH OF THE PROGRAM STRING USING
POINTERS.

AIM

To display length of a string using pointers.

ALGORITHM

Step 1:Get the string as a character array.

Step 2:Compare the current character at current pointer with \0.

Step 3:Increment pointer to get to next character.


Step 4:If current character is not null, increment counter else current counter is the
length of string.
step 5: stop

38 | P a g e
SOURCE CODE

#include<stdio.h>
#include<conio.h>

int string_ln(char*);

void main() {
char str[20];
int length;
clrscr();

printf("\nEnter any string : ");


gets(str);

length = string_ln(str);
printf("The length of the given string %s is : %d", str, length);
getch();
}

int string_ln(char*p) /* p=&str[0] */


{
int count = 0;
while (*p != '\0') {
count++;
p++;
}
return count;
}

39 | P a g e
OUTPUT

Enter the String : pritesh


Length of the given string pritesh is : 7

RESULT

40 | P a g e
EX.NO: 14 FIND THE AREA AND CIRCUMFERENCE OF A CIRCLE

AIM

To find the area and circumference of the circle

ALGORITHM

Step-1 Start the program.

Step-2Input the radius of the circle.

Step-3Find the area and circumference of the circle using


theformulaArea=3.14*r*rCircumference=2*3.14*r

Step-4Print the area and the circumference of the circle

Step-5Stop

41 | P a g e
SOURCE CODE

#include<stdio.h>

int main() {

int rad;
float PI = 3.14, area, ci;

printf("\nEnter radius of circle: ");


scanf("%d", &rad);

area = PI * rad * rad;


printf("\nArea of circle : %f ", area);

ci = 2 * PI * rad;
printf("\nCircumference : %f ", ci);

return (0);
}

42 | P a g e
OUTPUT

Enter radius of a circle: 1


Area of circle: 3.14
Circumference: 6.28

RESULT

43 | P a g e

You might also like