0% found this document useful (0 votes)
98 views

CP LAB Algorithm and Flowchart Upto Week 6

The document contains 5 programming exercises from Week 1 of a Computer Programming Lab. The exercises include programs to print "Hello World", print shapes using hash symbols, perform arithmetic operations on input numbers, display multiple variable types, and calculate the perimeter and area of a rectangle. For each exercise, the algorithm and flowchart are provided.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
98 views

CP LAB Algorithm and Flowchart Upto Week 6

The document contains 5 programming exercises from Week 1 of a Computer Programming Lab. The exercises include programs to print "Hello World", print shapes using hash symbols, perform arithmetic operations on input numbers, display multiple variable types, and calculate the perimeter and area of a rectangle. For each exercise, the algorithm and flowchart are provided.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 44

Computer Programming Lab Sir CRR COE

Computer Programming Lab


Unit 1
Week 1: (Exercise 1)

(pgm1a) //Write a C program to print Hello World

Algorithm:

Step 1: Start.

Step 2: Print Hello World.

Step 3: Stop.

Flowchart:

SIR C R REDDYCOLLEGE OF ENFINEERING Page 1


Computer Programming Lab Sir CRR COE

(pgm1b) //Write a C program to print a block F using hash (#), where the F has a
heightof six characters and width of five and four characters.

Algorithm:

Step 1: Start.

Step 2: Print F block.

Step 3: Stop.

Flowchart:

SIR C R REDDYCOLLEGE OF ENFINEERING Page 2


Computer Programming Lab Sir CRR COE

(pgm1c) //Write a C program to Program to perform different arithmetic operations.

Algorithm:

Step 1: Start.

Step 2: Declare variables num1, num2, sum, diff, mul, divi.

Step 3: Print "Enter two integers:"

Step 4: Read num1 and num2 from the user.

Step 5: sum = num1 + num2.

Step 6: Print the sum.

Step 7: diff = num1 - num2.

Step 8: Print the difference.

Step 9: mul = num1 * num2.

Step 10: Print the product.

Step 11: divi = num1 / num2.

Step 12: Print the division.

Step 13: Stop.

SIR C R REDDYCOLLEGE OF ENFINEERING Page 3


Computer Programming Lab Sir CRR COE

Flowchart:

(pgm1d) //C-Program to display multiple variables.

Algorithm:

Step 1: Start.

Step 2: Declare variables: i ,c ,f ,d ,l .

Step 3: Initialize i = 10, c = 'G', f = 30.9, d = 39.8975, and l = 23975.5698795.

Step 4: Print “Integer Value”.

SIR C R REDDYCOLLEGE OF ENFINEERING Page 4


Computer Programming Lab Sir CRR COE

Step 5: Print "Character Value".

Step 6: Print "Float Value”.

Step 7: Print "Double Value".

Step 8: Print "Long Double Value”.

Step 9: Stop.

Flowchart:

(pgm1e) //C-Program to compute the perimeter and area of a rectangle with a height
of 7 inches and width of 5 inches.

Algorithm:

SIR C R REDDYCOLLEGE OF ENFINEERING Page 5


Computer Programming Lab Sir CRR COE

Step 1: Start.

Step 2: Declare variables l, w.

Step 3: Initialize l = 7 and w = 5.

Step 4: Calculate perimeter = 2 * (l + w).

Step 5: Calculate area = l * w.

Step 6: Print "Perimeter”.

Step 7: Print “area”.

Step 8: Stop.

Flowchart:

SIR C R REDDYCOLLEGE OF ENFINEERING Page 6


Computer Programming Lab Sir CRR COE

Week 2: (Exercise 2)
(pgm2a) //C Program to find Sum and Average of 3 Numbers

Algorithm:

Step 1: Start.

Step 2: Declare variables: a, b, c, sum, and avg.

Step 3: Read three integers a, b, and c.

Step 4: Calculate sum = a + b + c.

Step 5: Calculate avg = sum / 3.

Step 6: Print “sum”.

Step 7: Print “avg”.

Step 8: Stop.
Flowchart:

1.

SIR C R REDDYCOLLEGE OF ENFINEERING Page 7


Computer Programming Lab Sir CRR COE

(pgm2b) //Write a C program to Conversion of Fahrenheit to Celsius & vice versa.

Algorithm:

Step 1: Start.

Step 2: Declare variables fh (Fahrenheit), cl (Celsius) and choice.

Step 3: Read choice.

Step 4: If choice equals 1:

a. Enter temperature in Fahrenheit:

b. Calculate cl (Celsius) = (fh - 32) / 1.8.

c. Display temperature in Celsius.

Step 5: If choice equals 2:

a. Enter temperature in Celsius:

b. Calculate fh (Fahrenheit)= (cl * 1.8) + 32.

c. Display temperature in Fahrenheit.

Step 6: If the choice is not 1 or 2:


a. Display "Invalid Choice !!!"

Step 7: Stop.

SIR C R REDDYCOLLEGE OF ENFINEERING Page 8


Computer Programming Lab Sir CRR COE

Flowchart:

SIR C R REDDYCOLLEGE OF ENFINEERING Page 9


Computer Programming Lab Sir CRR COE

(pgm2C) //C program to calculate simple interest.

Algorithm:

Step 1: Start.

Step 2: Declare variables principle, time, rate, and SI.


Step 3: Read the principle amount, time, and rate.

Step 4: Calculate SI = (principle * time * rate) / 100.

Step 5: Print SI.

Step 6: Stop.

Flowchart:

SIR C R REDDYCOLLEGE OF ENFINEERING Page 10


Computer Programming Lab Sir CRR COE

Week 3: (Exercise 3)
(pgm3a) //C program to find the square root of a number using sqrt function.

Algorithm:

Step 1: Start.

Step 2: Declare variable n.

Step 3: Read n value.

Step 4: Calculate the square root.

Step 5: Print Square root value.

Step 6: Stop.

Flowchart:

SIR C R REDDYCOLLEGE OF ENFINEERING Page 11


Computer Programming Lab Sir CRR COE

(pgm3b) //Write a C program to calculate Compound Interest

Algorithm:

Step 1: Start.
Step 2: Declare variables for principal (p), rate (r), time (t), and compound interest
(ci).

Step 3: Read the values principal (p), rate (r), time (t).
Step 4: Calculate the compound interest using the provided formula.
𝑟
Compound Interest=P(1+ )𝑡 −P
100

Step 5: Display compound interest value.


Step 6: Stop.
Flowchart:

SIR C R REDDYCOLLEGE OF ENFINEERING Page 12


Computer Programming Lab Sir CRR COE

(pgm3c) //C Program to Calculate Area of Any Triangle using Heron's Formula

Algorithm:

Step 1: Start.
Step 2: Declare variables, lengths of three sides of the triangle a,b,c and semi-
perimeter, area. (a, b, c, s, area)

Step 3: Read a,b,c values


Step 4: Calculate the semi-perimeter.
s = (a + b + c) / 2
Step 5: Calculate the area using Heron's formula.
area =√(s ∗ (s − a) ∗ (s − b) ∗ (s − c))
Step 6: Display area value
Step 7: Stop
Flowchart:

SIR C R REDDYCOLLEGE OF ENFINEERING Page 13


Computer Programming Lab Sir CRR COE

(pgm3d) //Write a C program to Distance travelled by an object.

Algorithm:

Step 1: Start.

Step 2: Initialize variables u, a, t, s as floats.

Step 3: Display "Enter initial velocity in meters per second:"

Read u from the user.

Step 4: Display "Enter acceleration in meters per second squared:"

Read a from the user.

Step 5: Display "Enter time in seconds:"

Read t from the user.

Step 6: Calculate s = u * t + (0.5) * a *𝑡 2

Display "The distance traveled in meters is:", s.

Step 7: Stop.

SIR C R REDDYCOLLEGE OF ENFINEERING Page 14


Computer Programming Lab Sir CRR COE

Flowchart:

SIR C R REDDYCOLLEGE OF ENFINEERING Page 15


Computer Programming Lab Sir CRR COE

Unit 2
Week 4: (Exercise 4)

pgm4a) write a C-program to evaluate the following expressions using precedence


and associativity

i)a+b*c+(d*e)+f*g

ii)a/b*c-b+a*d/3

iii)(a++)+b-(--a)

iv)j=(i++)+(++i)

Algorithm:

Step 1: Start.
Step 2: Initialize variables a, b, c, d, e, f, g, i as floats.
Step 3: Display "Enter values:"
Read a, b, c, d, e, f, g, i from the user.
Step 4: Calculate a+b*c+(d*e)+f*g and
Display "a+b*c+(d*e)+f*g".
Step 5: Calculate a/b*c-b+a*d/3 and
Display "a/b*c-b+a*d/3.
Step 6: Calculate (a++)+b-(--a) and
Display (a++)+b-(--a).
Step 7: Calculate j = (i++)+(++i).
Display j.
Step 8: Stop.

SIR C R REDDYCOLLEGE OF ENFINEERING Page 16


Computer Programming Lab Sir CRR COE

Flowchart:

(pgm4b) Find the maximum of three numbers using conditional operator

Algorithm:

Step 1: Start.

Step 2: Initialize variables n1, n2, n3, max as integers.

Step 3: Display "Enter first number:"

Read n1 from the user.

Step 4: Display "Enter second number:"


SIR C R REDDYCOLLEGE OF ENFINEERING Page 17
Computer Programming Lab Sir CRR COE

Read n2 from the user.

Step 5: Display "Enter third number:"

Read n3 from the user.

Step 6: Calculate max = (n1 > n2) ? (n1 > n3 ? n1 : n3) : (n2 > n3 ? n2 : n3).

Step 7: Display max value.

Step 8: Stop.
Flowchart:

SIR C R REDDYCOLLEGE OF ENFINEERING Page 18


Computer Programming Lab Sir CRR COE

(pgm4c) Take marks of 5 subjects in integers, and find the total, average in float

Algorithm:

Step 1: Start.

Step 2: Initialize variables num1, num2, num3, num4, num5 as integers.

Initialize variables sum, avg as floats.

Step 3: Display "Enter first number:"

Read num1 from the user.

Step 4: Display "Enter second number:"

Read num2 from the user.

Step 5: Display "Enter third number:"

Read num3 from the user.

Step 6: Display "Enter fourth number:"

Read num4 from the user.

Step 7: Display "Enter fifth number:"

Read num5 from the user.

Step 8: Calculate sum = num1 + num2 + num3 + num4 + num5.

Calculate avg = sum / 5.0.

SIR C R REDDYCOLLEGE OF ENFINEERING Page 19


Computer Programming Lab Sir CRR COE

Step 9: Display sum value.

Display avg value.

Step 10: Stop.

Flowchart:

SIR C R REDDYCOLLEGE OF ENFINEERING Page 20


Computer Programming Lab Sir CRR COE

Week 5: (Exercise 5)
(pgm5a) Write a C program to find max and min of four numbers using if else.

Algorithm:

Step 1: Start.
Step 2: Initialize variables a, b, c, d as integers.
Step 3: Display "Enter four numbers:"
Read a, b, c, d from the user.
Step 4: // Find Maximum
if (a > b and a > c and a > d)
Display "a is the maximum value."
else if (b > a and b > c and b > d)
Display "b is the maximum value."
else if (c > a and c > b and c > d)
Display "c is the maximum value."
else
Display "d is the maximum value."
Step 5: // Find Minimum
if (a < b and a < c and a < d)
Display "a is the minimum value."
else if (b < a and b < c and b < d)
Display "b is the minimum value."
else if (c < a and c < b and c < d)
Display "c is the minimum value."
else
Display "d is the minimum value."
Step 6: Stop.

Flowchart:

SIR C R REDDYCOLLEGE OF ENFINEERING Page 21


Computer Programming Lab Sir CRR COE

SIR C R REDDYCOLLEGE OF ENFINEERING Page 22


Computer Programming Lab Sir CRR COE

(pgm5b) Write a C program to generate electricity bill.

Algorithm:

Step 1: Start.

Step 2: Initialize variables units, custnum as integers, and charges as a float.

Step 3: Display "Enter Customer no. and units consumed"

Read custnum, units from the user.

Step 4: // Calculate Charges

if (units <= 200)

charges = 0.5 * units;

else if (units <= 400)

charges = 100 + 0.65 * (units - 200);

else if (units <= 600)

charges = 230 + 0.8 * (units - 400);

else

charges = 390 + (units - 600);

Step 5: Display "Customer No:", custnum, "Charges:", charges.

Step 6: Stop.
Flowchart:

SIR C R REDDYCOLLEGE OF ENFINEERING Page 23


Computer Programming Lab Sir CRR COE

SIR C R REDDYCOLLEGE OF ENFINEERING Page 24


Computer Programming Lab Sir CRR COE

pgm5C) Find the roots of the quadratic equation.

Algorithm:

Step 1: Start.

Step 2: Initialize variables a, b, c, dis, root1, root2, rp, ip as doubles.

Step 3: Display "Enter coefficients a, b, and c:"

Read a, b, c from the user.

Step 4: // Calculate Discriminant

dis = b * b - 4 * a * c;

Step 5: // Calculate and display Roots

if (dis > 0)

root1 = (-b + sqrt(dis)) / (2 * a);

root2 = (-b - sqrt(dis)) / (2 * a);

Display "Root 1 =", root1, "and Root 2 =", root2.

else if (dis == 0)

root1 = root2 = -b / (2 * a);

Display "Root 1 = Root 2 =", root1.

else

rp = -b / (2 * a);

SIR C R REDDYCOLLEGE OF ENFINEERING Page 25


Computer Programming Lab Sir CRR COE

ip = sqrt(-dis) / (2 * a);

Display "Root 1 =", rp, "+", ip, "i and Root 2 =", rp, "-", ip, "i."

Step 6: Stop.

SIR C R REDDYCOLLEGE OF ENFINEERING Page 26


Computer Programming Lab Sir CRR COE

Flowchart:

SIR C R REDDYCOLLEGE OF ENFINEERING Page 27


Computer Programming Lab Sir CRR COE

(pgm5d) //C program to simulate calculator using switch statement.

Algorithm:

Step 1: Start.

Step 2: Initialize variables a, b, res(result) as floats.


Initialize variable choice as an integer.
Step 3: Display "Enter the values:"
Read a, b from the user.

Step 4: Display "1. Addition"


Display "2. Subtraction"
Display "3. Multiplication"
Display "4. Division"
Display "Enter your choice:"
Read choice from the user.

Step 5: // Perform Calculation


switch(choice)
case 1:
Calculate res = a + b;
Display "Addition of", a, "+", b, "is:", res.
break;
case 2:
Calculate res = a - b;
Display "Subtraction of", a, "-", b, "is:", res.
break;
case 3:
Calculate res = a * b;
Display "Multiplication of", a, "*", b, "is:", res.
break;
case 4:

SIR C R REDDYCOLLEGE OF ENFINEERING Page 28


Computer Programming Lab Sir CRR COE

Calculate res = a / b;
Display "Division of", a, "/", b, "is:", res.
break;
default:
Display "Invalid Choice".
break;

Step 6: Stop.

SIR C R REDDYCOLLEGE OF ENFINEERING Page 29


Computer Programming Lab Sir CRR COE

Flowchart:

SIR C R REDDYCOLLEGE OF ENFINEERING Page 30


Computer Programming Lab Sir CRR COE

(pgm5e) Write a C program to find the given year is a leap year or not.

Algorithm:

Step 1: Start.

Step 2: Initialize variable year as an integer.

Step 3: Display "Enter any year:"

Read year from the user.

Step 4: // Check Leap Year

if (year % 4 == 0 and (year % 100 != 0 or year % 400 == 0))

Display year, "is a leap year."

else

Display year, "is not a leap year."

Step 5: Stop.

SIR C R REDDYCOLLEGE OF ENFINEERING Page 31


Computer Programming Lab Sir CRR COE

Flowchart:

SIR C R REDDYCOLLEGE OF ENFINEERING Page 32


Computer Programming Lab Sir CRR COE

Week 6: (Exercise 6)

pgm6a) C-Program to find factorial of a number.

Algorithm:
Step 1: Start

Step 2: Initialize variables: n, i, and fact to store the input number, loop variable, and
factorial respectively.

Step 3: Display a message to the user: "Enter number to find factorial."


Read the value of n from the user.

Step 4: Set fact to 1.


Set loop variable i to 1.

Step 5: Loop:
Repeat the following steps until i becomes greater than n.
Multiply fact by i.
Increment the value of i by 1.
End Loop

Step 6: Display the result: "Factorial of n is fact."

Step 7: Stop.

SIR C R REDDYCOLLEGE OF ENFINEERING Page 33


Computer Programming Lab Sir CRR COE

Flowchart:

SIR C R REDDYCOLLEGE OF ENFINEERING Page 34


Computer Programming Lab Sir CRR COE

(pgm6b) Find the given number is a prime or not.

Algorithm:
Step 1: Start

Step 2: Declare variables n, i, and flag=0 as integers.

Step 3: Print the message: "Enter a positive integer."


Read the input integer n.

Step 4: Start a loop from i = 2 to n / 2.


Check if n is divisible by i.
If true, increment the flag by 1.
End the loop.

Step 5: Check if flag is equal to 0 and n is not equal to 1.


If true, print "n is a prime number."
If false, print "n is not a prime number."

Step 6: Stop.

SIR C R REDDYCOLLEGE OF ENFINEERING Page 35


Computer Programming Lab Sir CRR COE

Flowchart:

SIR C R REDDYCOLLEGE OF ENFINEERING Page 36


Computer Programming Lab Sir CRR COE

(pgm6c) Compute Sine and Cos series


// Cos series

Algorithm:
Step 1: Start

Step 2: Declare constants PI and MAX with values 3.1416 and 180, respectively.

Step 3: Declare variables angle, x, and y as integers and floats.

Step 4: Initialize angle to 0.

Step 5: While angle is less than or equal to MAX.


Calculate x using the formula (PI / MAX) * angle.
Calculate y as the cosine of x.
Print the current angle and y values.
Increment angle by 10.
End the loop.

Step 6: Stop.

SIR C R REDDYCOLLEGE OF ENFINEERING Page 37


Computer Programming Lab Sir CRR COE

Flowchart:

SIR C R REDDYCOLLEGE OF ENFINEERING Page 38


Computer Programming Lab Sir CRR COE

// Sin series

Algorithm:
Step 1: Start

Step 2: Declare constants PI and MAX with values 3.1416 and 180, respectively.

Step 3: Declare variables angle, x, and y as integers and floats.

Step 4: Initialize angle to 0.

Step 5: While angle is less than or equal to MAX.


Calculate x using the formula (PI / MAX) * angle.
Calculate y as the sine of x.
Print the current angle and y values.
Increment angle by 10.
End the loop.

Step 6: Stop.

SIR C R REDDYCOLLEGE OF ENFINEERING Page 39


Computer Programming Lab Sir CRR COE

Flowchart:

SIR C R REDDYCOLLEGE OF ENFINEERING Page 40


Computer Programming Lab Sir CRR COE

(pgm6d) Checking a number is palindrome or not.

Algorithm:
Step 1: Start

Step 2: Declare variables num, reverse, remainder, and original as integers.

Step 3: Print the message: "Enter an integer."


Read the input integer num.

Step 4: Set original to the value of num.

Step 5: Initialize reverse to 0.

Step 6: While num is not equal to 0,


Calculate the remainder as num % 10.
Update reverse as reverse * 10 + remainder.
Update num as num / 10.
End the loop.

Step 7: Check if original is equal to reverse.


If true, print "original is a palindrome."
If false, print "original is not a palindrome."

Step 8: Stop.

SIR C R REDDYCOLLEGE OF ENFINEERING Page 41


Computer Programming Lab Sir CRR COE

Flowchart:

SIR C R REDDYCOLLEGE OF ENFINEERING Page 42


Computer Programming Lab Sir CRR COE

(pgm6e) Construct a pyramid of numbers.

Algorithm:
Step 1: Start

Step 2: Declare variables i, j, rows, and k as integers.

Step 3: Print the message: "Enter a number to define the rows."


Read the input integer rows.

Step 4: Initialize a loop with i from 1 to rows.


Initialize an inner loop with j from 1 to rows - i. print the space.
Initialize another loop with k from 1 to (2 * i - 1).
Print the value of i followed by a space, Move to the next line.
End the loops.

Step 5: Stop.

Flowchart:

SIR C R REDDYCOLLEGE OF ENFINEERING Page 43


Computer Programming Lab Sir CRR COE

SIR C R REDDYCOLLEGE OF ENFINEERING Page 44

You might also like