0% found this document useful (0 votes)
70 views15 pages

Introduction To Algorithm: Prepared by

The document introduces algorithms by defining them as step-by-step processes or sets of rules to solve problems, provides examples of designing algorithms like adding three numbers, and outlines the steps of specifying the problem, inputs, outputs, and developing the step-by-step process to arrive at the solution. It also provides examples of writing algorithms to print "Hello World", add two numbers, and find the largest of three numbers.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
0% found this document useful (0 votes)
70 views15 pages

Introduction To Algorithm: Prepared by

The document introduces algorithms by defining them as step-by-step processes or sets of rules to solve problems, provides examples of designing algorithms like adding three numbers, and outlines the steps of specifying the problem, inputs, outputs, and developing the step-by-step process to arrive at the solution. It also provides examples of writing algorithms to print "Hello World", add two numbers, and find the largest of three numbers.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1/ 15

Introduction to Algorithm

Prepared By:
Sejal Patel
Assistant Professor
Parul Unievrsity
What is Algorithm?
• The word Algorithm means “a process or set
of rules to be followed in calculations or other
problem-solving operations”.
• An algorithm is a procedure or step-by-step
instruction for solving a problem.
• It can be understood by taking an example of
cooking a new recipe. To cook a new recipe,
one reads the instructions and steps and
execute them one by one, in the given
sequence. The result thus obtained is the new
dish cooked perfectly. Similarly, algorithms
help to do a task in programming to get the
expected output.
How to Design an Algorithm?

• Inorder to write an algorithm, following things are


needed as a pre-requisite:
• The problem that is to be solved by this algorithm.
• The contraints of the problem that must be considered
while solving the problem.
• The input to be taken to solve the problem.
• The output to be expected when the problem the is
solved.
• The solution to this problem, in the given contraints.
• Example: Consider the example to add three
numbers and print the sum.
• Step 1: Fulfilling the pre-requisites
• Step 2: Designing the algorithm
• Step 3: Testing the algorithm by
implementing it.
Step 1: Fulfilling the pre-requisite
• As discussed above, inorder to write an algorithm, its pre-requisites
must be fulfilled.
• The problem that is to be solved by this algorithm: Add 3 numbers
and print their sum.
• The contraints of the problem that must be considered while
solving the problem: The numbers must contain only digits and no
other characters.
• The input to be taken to solve the problem: The three numbers to
be added.
• The output to be expected when the problem the is solved: The
sum of the three numbers taken as the input.
• The solution to this problem, in the given contraints: The solution
consists of adding the 3 numbers. It can be done with the help of ‘+’
operator, or bitwise, or any other method.
Step 2: Designing the algorithm
• Now let’s design the algorithm with the help of above pre-
requisites:
• Algorithm to add 3 numbers and print their sum:
• START
• Declare 3 integer variables num1, num2 and num3.
• Take the three numbers, to be added, as inputs in variables
num1, num2, and num3 respectively.
• Declare an integer variable sum to store the resultant sum
of the 3 numbers.
• Add the 3 numbers and store the result in the variable sum.
• Print the value of variable sum
• END
• Testing the algorithm by implementing it.
Inorder to test the algorithm, let’s implement
it in C language.
• #include <stdio.h>

• int main()
• {

• // Variables to take the input of the 3 numbers
• int num1, num2, num3;

• // Variable to store the resultant sum
• int sum;

• // Take the 3 numbers as input
• printf("Enter the 1st number: ");
• scanf("%d", &num1);
• printf("%d\n", num1);

• printf("Enter the 2nd number: ");
• scanf("%d", &num2);
• printf("%d\n", num2);

• printf("Enter the 3rd number: ");
• scanf("%d", &num3);
• printf("%d\n", num3);

• // Calculate the sum using + operator
• // and store it in variable sum
• sum = num1 + num2 + num3;

• // Print the sum
• printf("\nSum of the 3 numbers is: %d", sum);

• return 0;
• }
• Output:
• Enter the 1st number: 2
• Enter the 2nd number: 3
• Enter the 3rd number: 5
• Sum of the 3 numbers is: 10
• Write an Algorithm to print Hello World.
• Write an algorithm to add two numbers
entered by user.
• Step 1: Start
• Step 2: Declare variables num1, num2 and sum.
• Step 3: Read values num1 and num2.
• Step 4: Add num1 and num2 and assign the result
to sum.
• sum←num1+num2
• Step 5: Display sum
• Step 6: Stop
• Write an algorithm to find the largest among
three different numbers entered by user.
Step 1: Start
Step 2: Declare variables a,b and c.
Step 3: Read variables a,b and c.
Step 4: If a>b
If a>c
Display a is the
largest number.
Else Display c is
the largest number.
Else If b>c
Display b is the largest number.
Else Display c is the greatest
number.
Step 5: Stop

You might also like