You are on page 1of 11

Experiment 2

Aim: Problem solving using Flowchart and Algorithm

Theory: Algorithm

The sequence of rules that define how a particular problem can be solved in a finite
sequence of steps is known as an algorithm.

How to develop an Algorithm?

1. Understand the problem


2. Identify the output of the problem
3. Identify the inputs required by the problem to reach desired output
4. Design a logic to produce the desired result
5. Test the algorithm
6. Repeat step 1 to 5 till desired results are produced

Steps in Problem Solving:

● First produce a general algorithm (one can use pseudocode)

● Refine the algorithm successively to get step by step detailed algorithm that is
very close to a computer language.

● Pseudocode is an artificial and informal language that helps programmers develop


algorithms. Pseudocode is very similar to everyday English.

Pseudocode & Algorithm

Example 1: Write an algorithm to determine a student’s final grade and indicate whether
it is passing or failing. The final grade is calculated as the average of four marks

Amanat kumar | Diploma ECE


Pseudocode:
● Input a set of 4 marks

● Calculate their average by summing and dividing by 4

● if average is below 50
Print “FAIL”
else
Print “PASS”

Detailed Algorithm:
Step 1: Input M1,M2,M3,M4

Step 2: GRADE ← (M1+M2+M3+M4)/4

Step 3: if (GRADE < 50) then

Print “FAIL”
else
Print “PASS”

Endif

The Flowchart
A schematic representation of a sequence of operations, as in a manufacturing process or
computer program.
The Information system flowcharts show how data flows from source documents through
the computer to final distribution to users.
Program flowcharts show the sequence of instructions in a single program or subroutine.
Different symbols are used to draw each type of flowchart.

Amanat kumar | Diploma ECE


A Flowchart
● shows logic of an algorithm

● emphasizes individual steps and their interconnections

● e.g. control flow from one action to the next

Flowchart Symbols:

Amanat kumar | Diploma ECE


Example

Problems:
1. To check if the number entered by the user is odd or even.

2. To calculate the greatest of 3 numbers.

3. To convert Temperature from Fahrenheit (℉) to Celsius (℃)

Amanat kumar | Diploma ECE


Q1.
Algorithm:
Step1: Input number A
Step2: A mod 2 = B
Step3: if B = 0 then
Print “Even”
else
Print “Odd”
Endif

Flowchart:

Amanat kumar | Diploma ECE


Q2.
Algorithm:
Step1: Input
A,B,C Step2: max
=0
Step3: If max<=A then
max=A Step4: if max<=B then
max=B Step5: if max<=C then
max=C Step6: print max
End

Flowchart:

Amanat kumar | Diploma ECE


Q3.
Algorithm:
Step1: Input temperature Fahrenheit (F)
Step2: Convert F to C = (F-32)*5/9
Step3: Print C
End

Amanat Kumar / Diploma ECE


Experiment 4

Aim: Print your name and address using c program.

Theory:

#include <stdio.h>
int main()
{
print(“Amanat Kumar, Gurugram”);

return0;
}

Output:

AMANAT KUMAR, GURUGRAM

Amanat Kumar / Diploma ECE


Experiment 4

Aim: Write a C program to find the sum of two integers.

Theory:

#include <stdio.h>
int main()
{
int a,b,sum;
printf("Enter two numbers\n");
scanf("%d,%d",&a, &b);
Sum = a + b;
printf("%d + %d = %d", a, b, sum);
return 0;
}

Output:

Amanat Kumar / Diploma ECE


Experiment 5

Aim: Write a C program to swap the value of two variables Theory:


#include <stdio.h>

void main()
{
int a,b, temp;
printf("enter two intergers\n");
scanf("%d%d", &a, &b);
printf("before swapping \nfirst integer = %d,\nsecond integer = %d\n", a,b); temp=a;
a=b;
b=temp;
printf("after swapping \nfirst integer = %d,\nsecond integer = %d”,a,b);
}

Output:

Amanat Kumar / Diploma ECE


Experiment 6

Aim: write a C program to find the greatest of three using nested if else statement

Theory:
#include <stdio.h>
#include <conio.h>
void main()
{
int n1,n2,n3;
printf(“Enter integers n1,n2,n3\n”);
scanf(“%d%d%d”,&n1,&n2,&n3);
if(n1>n2){
if(n1>n3){
printf(“%d is greatest”,n1);
}
else{
printf(“%d is greatest”,n3);
}
}
else{
if(n2>n3){
printf(“%d is greatest”,n2);
}
else{
printf(“%d is greatest”,n3);
}
}
}

Output:

Amanat Kumar / Diploma ECE

You might also like