Computer science lab manual, Papers of Computer Science

Btech 1 St year lab manual programming lab in c language

Typology: Papers

2021/2022

Uploaded on 09/14/2022

anmol-singhal
anmol-singhal 🇮🇳

1 document

1 / 103

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Theme: General Programs
Program No: 1
Objective:
Write a program to calculate the area of triangle using formula at=√s (s- a) (s-b) (s-c)
Logic
Given a triangle with side lenths a, b and c. Then is area can be calculated as==√s (a+b+c)/2where
s=(a+b+c)/2
Example:
If the input is:
a = 3.
b = 5.
c = 7.
Triangle area = 6.49519062
Algorithm:
1. Start
2. Enter the value of a, b and c
3. Calculate the value of s as s=(a+b+c)/2
4. Calculate the value of Area as area=sqrt(s(s-1) (s-b) (s-c)
5. Print the area
6. Stop
Program Code:
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
float a, b, c, s, area;
clrscr();
printf(“Enter the value of a)
scanf(%f,&a)
printf(“Enter the value of b”);
scanf(“ %f”,&b);
printf(“Enter the value of c”);
scanf(“%f ”, &d);
s = (a+b+c)/2;
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37
pf38
pf39
pf3a
pf3b
pf3c
pf3d
pf3e
pf3f
pf40
pf41
pf42
pf43
pf44
pf45
pf46
pf47
pf48
pf49
pf4a
pf4b
pf4c
pf4d
pf4e
pf4f
pf50
pf51
pf52
pf53
pf54
pf55
pf56
pf57
pf58
pf59
pf5a
pf5b
pf5c
pf5d
pf5e
pf5f
pf60
pf61
pf62
pf63
pf64

Partial preview of the text

Download Computer science lab manual and more Papers Computer Science in PDF only on Docsity!

Theme: General Programs Program No: 1 Objective: Write a program to calculate the area of triangle using formula at=√s (s- a) (s-b) (s-c) Logic Given a triangle with side lenths a, b and c. Then is area can be calculated as==√s (a+b+c)/2where s=(a+b+c)/ Example: If the input is: a = 3. b = 5. c = 7. Triangle area = 6. Algorithm:

  1. Start
  2. Enter the value of a, b and c
  3. Calculate the value of s as s=(a+b+c)/
  4. Calculate the value of Area as area=sqrt(s(s-1) (s-b) (s-c)
  5. Print the area
  6. Stop Program Code: #include<stdio.h> #include<conio.h> #include<math.h> void main() { float a, b, c, s, area; clrscr(); printf(“Enter the value of a) scanf(%f,&a) printf(“Enter the value of b”); scanf(“ %f”,&b); printf(“Enter the value of c”); scanf(“%f ”, &d); s = (a+b+c)/2;

area= sqrt(s(s-a)(s-b)*(s-c); print(“ Area of the triangle %f”, area); getch(); Output Window :

getch(); } Output Window:

THEME: Conditional Branching

Program No: Objective: Write a program to determine the roots of quadratic equation. Logic: The general form of a quadratic equation is (ax^2 + bx + c = 0). The formula to find the roots of a quadratic equation is given as follows x = [-b +/- sqrt (-b^2 - 4ac)]/2a The discriminant of the quadratic equation is d= (b^2 - 4ac). Depending upon the nature of the discriminant, the roots can be found in different ways.

  1. If the discriminant is positive, then there are two distinct real roots.
  2. If the discriminant is zero, then the two roots are equal.
  3. If the discriminant is negative, then there are two distinct complex roots. For example, consider the following equation 2x^2 8x + 3 = 0. a = 2, b = - 8, c = 3 Discriminant value, d =b^2 - 4ac = 8^2 - 4(-8) * 3 = 40 The discriminant value is positive. Hence, the roots are real and distinct. r1 = (-b +d)/ 2a= (8 +40) /22 = 2. r2 = (b +d)/ 2a =(-8 +40) /2*2 = - 0. r1 = 2.3875andr2 = - 0.3875 are the two roots.

real_part = - b/2a; imag_part = sqrt(-discriminant)/2a; printf(First root of the equation is %lf+%lf”, real_part, imag_part); printf(Second root of the equation is %lf-%lf”, real_part, imag_part); getch(); } } Output Window :

Program No: Objective: Write a program to find the largest of three numbers using nested if else Logic: Given three numbers A, B and C The task is to find the largest number among the three. Input: A = 2, B = 8, C = 1 Output: Largest number = 8 Input: A = 231, B = 4751, C = 75821 Output: Largest number = 75821 Algorithm:

  1. Start
  2. Read the three numbers to be compared, as A, B and C.
  3. Check if A is greater than B. 3.1 If true, then check if A is greater than C. 3.1.1 If true, print 'A' as the greatest number. 3.1.2 If false, print 'C' as the greatest number. 3.2 If false, then check if B is greater than C. 3.1.1 If true, print 'B' as the greatest number. 3.1.2 If false, print 'C' as the greatest number.
  4. Stop Program Code: #include<stdio.h> #include<conio.h> void main() { float a, b, c: clrscr(); printf(“Enter the value of a) scanf(%f,&a) printf(“Enter the value of b”); scanf(“ %f”,&b); printf(“Enter the value of c”); scanf(“%f ”, &d); if(a>b && a>c) { printf( a is largest of three number %/f”, a); }

Program No: 3 Objective: Write a program to receive marks of physics, chemistry & maths from user & check its eligibility for course if a) Marks of physics > 40 b) Marks of chemistry > 50 c) Marks of math’s > 60 d) Total of physics & math’s marks > 150 or Total of three subjects marks > 200 Logic: Take the marks of Physics , Chemistry and Math from user Case1: if Marks of Physics > Case2: if Marks of Chemistry > Case3: if Marks of Math > Case 4: if Addition of Physics and maths marks > Case5: if Addition of Physics , chemistry and Maths> If all five cases are satisfied then candidate is eligible. Algorithm:

  1. Start
  2. Input marks of Physics , Chemistry and Math from user
  3. If Marks of Physics is greater than 40 and
  4. If Marks of Chemistry is greater than 50 and
  5. If Marks of Maths is greater than 60 and
  6. If Total marks of Physics and Maths is greater than 150
  7. If total marks of Physics , Chemistry and Maths is greater than 200
  8. If all step 9, 10, 11 12 13 is satisfied than candidate is eligible for course admission
  9. Otherwise not eligible for admission in course
  10. end Program Code : #include<stdio.h> #include<conio.h> void main() {

int physics, chemistry, maths, total1,total2; clrscr(); printf(“ Enter the marks of Physics”); scanf(“%d”, & physics); printf(“ Enter the marks of Chemistry”); scanf(“%d”, & chemistry); printf(“ Enter the marks of Maths”); scanf(“%d”, & maths); toatl1=physics+maths; total2=physics+chemistry+maths; if(physics>40) if(chemistry>50) if(maths>60) if(total1>150|| total2>200) printf(“ Applicants is eligible for admission”); else printf(“ Applicants is not eligible for admission”); else printf(“ Applicants is not eligible for admission”); else printf(“ Applicants is not eligible for admission”); else printf(“ Applicants is not eligible for admission”); } getch() }

Program - 4 Objective: Write a program to find the value of y for a particular value of n. The a, x, b, n is input by user (switch) if n=1 y=ax%b if n=2 y=ax2+b if n=3 y=a-bx if n=4 y=a+x/b Logic: Use the switch feature to choose one condition from multiple condition. Case 1: n=1, calculate using formula y=ax%b Case2: n=2 calculate using y=ax2+b Case3: n=3 calculate using y=a-bx Case 3 n=4 calculate using y=a+x/b Example: Input: If a=5, b=1, x=1, n= Output=4.0 beacause as n=3 case 3 will be executed as y=a-bx=5-1*1= Algorithm:

  1. Start
  2. Enter the value of a, b,x and n ,
  3. Apply the switch case to select only one match case.
  4. If case label is n=1 then compute using formula y=ax%b
  5. If case label is n=2 then compute using formula y=ax2+b
  6. If case label is n=1 then compute using formula y=a-bx
  7. If case label is n=1 then compute using formula y=a+x/b
  8. Stop Program Code: #include<stdio.h> #include<conio.h> void main() { int a, b, x , n; float y; clrscr(); printf(" Eneter the value of a\n"); scanf("%d",&a); printf(" Eneter the value of b\n");

scanf("%d",&b); printf(" Eneter the value of x\n"); scanf("%d",&x); printf(" Eneter the value ofn\n"); scanf("%d",&n); switch(n) { case 1: y=(ax%b); printf(" The value of y is=%f" ); break; case 2: y=(ax2+b2); printf( " The value of y is=%f" ,y); break; case 3: y=(a-b*x); printf( " The value of y is=%f" ,y); break; case 4: y=(a+x/b); printf( " The value of y is=%f" ,y); break; } getch(); } Output Window :

#include<conio.h> void main() { int a ,b ,c , i, terms; clrscr(); printf(“ Enter the number the terms”); scanf(“%d”, &terms); a = 0; b= 1; c=0; printf(“Fibonacci series is :\n”); for(i=0;i<+terms;i++) { printf(“\t%d”,c); a=b; b=c; c=a+b; getch(); } Output Window :

Program No: Objective: Write a program to find whether the number is Armstrong number. Logic: If Sum of cubes of every digit of number is equal to itself number is known as Armstrong Number like 0, 1, 153, 370, 371 and 407 153= 1^3 +5^3 +3^3 Determine Remainder of Given number as Remainder=Number%10.Then calculate the last digit of given number as Number=Number/10. Algorithm:

  1. Start
  2. Input any integer number
  3. Set Original number = Number
  4. Calculate the Remainder= Original number %
  5. Compute the Sum=Sum+RemainderRemainderRemainder
  6. Original number=Original number /10;
  7. Repeat steps 4 ,5 and 6 while Number>
  8. If Sum= Number Print Number is Armstrong Otherwise print number is not Armstrong.
  9. Stop Program Code: #include<stdio.h> #include<conio.h> void main() { int Number, Original_number, Remainder, Sum=0; clrscr(); printf("enter the number of three digit integer\n"); scanf("%d",& Number); Original_number=Number; while(Original_number!=0) { Remainder=Original_number%10;

Program No: Objective: Write a program to generate sum of series 1!+2!+3!+ n!. Logic: Apply loop to Compute the summation of every Integer value.Initialize the value of sum=0 and loop counter by 1.At each iteration factorial of each term and then compute Sum. Example: Input: Terms= So, Sum=1! +2! +3! +4! +5!=1+2+6+24+120= Output: 153 Algorithm:

  1. Start
  2. Enter the value of terms ie n
  3. Calculate the value of P=P*I
  4. Calculate the value Sum=Sum+P
  5. Repeat steps 3 ,4 for i=0 to term
  6. Print the value of Sum
  7. Stop Program Code: #include<stdio.h> #include<conio.h> void main() { long i , term, Sum=0, P=1; printf( “Enter the terms”) scanf(“%d”, &term); for(i=0;i<term;i++) { P= P*i; Sum=Sum+P; }

printf(“ The sum of the series is: %d”,Sum); getch(); } Output Window :