Write an algorithm and draw a corresponding flowchart to print the sum of all integers starting from 2 to the given positive integer x where x >= 2. 10m Dec2006
An algorithm is a finite set of steps defining the solution of a particular problem. An algorithm is expressed in pseudo code – something resembling C language or Pascal, but with some statements in English rather than within the programming language
- A sequential solution of any program that written in human language, called algorithm.
- Algorithm is first step of the solution process, after the analysis of problem, programmers write the algorithm of that problem.
Pseudo code:
- Initialize I to zero , Sum to zero
- Input a Number
- While I is Less than equal to Number
- itoa(I,SNUM,10)
- If SNUM[0] equal to ‘2’
- SUM=SUM+I
- Increment I
- Print SUM
- Stop
Detailed Algorithm:
Step 1: Input NUM
Step 2: I = 0 , SUM =0
Step 3: While ( I<= NUM)
itoa(I,SNUM,10)
If ( SSNUM[0] == ‘2’)[I] == S[J] )
SUM=SUM+I
I++
Step 6: Print SUM
Step 7: End
Program Code:
[codesyntax lang=”c”]
c#include<stdio.h> #include<stdlib.h> void main() { int SUM=0,NUM,i; char SNUM[5]; clrscr(); printf("\nENTER ANY INTERGER >= 2 : "); scanf("%d",&NUM); for(i=0;i<=NUM;i++) { itoa(i,SNUM,10); if(SNUM[0]=='2') SUM=SUM+i; } printf("\n%d",SUM); getch(); }
[/codesyntax]
Flowchart:-