0% found this document useful (0 votes)
10 views9 pages

Algorithms and Pseudocode Examples

The document contains a series of algorithms and pseudocode for various mathematical and logical operations, including finding squares, doubling numbers, calculating areas and perimeters, determining leap years, and categorizing individuals by age. Each section outlines a step-by-step approach to solving specific problems, providing both algorithmic and pseudocode representations. The document serves as a comprehensive guide for basic programming concepts and problem-solving techniques.

Uploaded by

nishantsuhag6
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views9 pages

Algorithms and Pseudocode Examples

The document contains a series of algorithms and pseudocode for various mathematical and logical operations, including finding squares, doubling numbers, calculating areas and perimeters, determining leap years, and categorizing individuals by age. Each section outlines a step-by-step approach to solving specific problems, providing both algorithmic and pseudocode representations. The document serves as a comprehensive guide for basic programming concepts and problem-solving techniques.

Uploaded by

nishantsuhag6
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Q. Write an algorithm to find the square of a number.

Ans.

1. Start
2. Input a number
3. Square = number ** 2
4. Print Square
5. Stop

Q. Write an algorithm to double a number in two different ways: (i) n+n, (ii) 2x
Ans.
1. Start
2. Input a number
3. First = number + number
4. Second = number * 2
5. Print First
6. Print Second
7. Stop

Q. Write an algorithm and draw a flowchart to determine if a student passed the exam or
not.( Note there are 4 subject papers and passing average is 50 or more.)
Ans.
Step 1: Input M1, M2, M3, M4
Step 2: GRADE B (M1+ M2 +M3+
Step 3: if (GRADE <50) then
Print "FAIL"
else
Print "PASS"

# end of if

Q. Write pseudocode for following :-


Ans.
(a)

X = input "x"
Y = input "y"
Z = input "z"

If x is greater than y
If x is greater than z
Display "x is largest number"
Else
Display "z is greatest number"
Else
If y is greater than z
Display "y is largest number"
Else
Display "z is greatest number"

(b)

c=0
s=0
while c is less than 5
n = input a number
s equal to s + num
c equal to c + 1
avg = s / 5
display avg

Q. To find the area and perimeter of a rectangle.


Ans :-

Algorithm :-

1. Input length
2. Input width
3. Perimeter 2 * ( length + width )
4. Area length * width
5. Print perimeter
6. Print Area
7. Stop

Pseudocode :-

L = input "length of rectangle"


W = input "width of rectangle"
Perimeter = 2 * ( L + W )
Area = L * W
Display Perimeter
Display Area

Q. To calculate the area and the circumference of circles.

Ans :-

Algorithm :-

1. Input radius
2. Circumference 2 * 3.14 * radius
3. Area 3.14 * radius ** 2
4. Print Circumference
5. Print Area
6. 7. Stop

Pseudocode :-

R = input "Radius of Circle"


Circumference = 2 * 3.14 * radius
Area = 3.14 * radius ** 2
Display Circumference
Display Area

Q. To calculate the simple interest.


Ans :-
Algorithm :-

1. Start
2. Input principal
3. Input rate
4. Input time
5. Simple interest principal * rate * time
6. Print simple interest
7. Stop

Pseudocode :-

P = input "principal value "


R = input " rate "
T = input "time "
Si = p * r * t
Display si

Q. To check whether a year is a leap year or not


Ans :-
Algorithm :-
1. Start
2. Input Year
3. If year % 4 == 0
Print "leap year "
Else
Print " not leap year "

Pseudocode :-
Y = input " a year"
If y is full divisible by 4
Display y is leap year
Else
Display y is not leap year

Q. To check if a number is a positive or negative number.


Answ :-
Algorithm :-
1. Start
2. Input number
3. If number < 0
Print "number is negative"
Else
Print "number is positive"
4. Stop

Pseudocode :-

N = input "A number "


If n is smaller than 0
Display " n is negative number "
Else
Display " n is positive number "

Q. To check if a number is an odd or even number


Ans :-
Algorithm :-

1. Start
2. Input number
3. If number % 2 == 0
4. Print "number is even"
5. Else
6. Print "number odd"
7. Stop

Pseudocode :-

N = input "A number "


If n is fully divisible by 2
Display " n is even number "
Else
Display " n is positive number "
Q. To categorise a person as either child (<13), teenager (> 13 but <20) or adult (20), based
on age specified.
Ans:-
Algorithm :-

1. Start
2. Input age
3. If age < 13
Print "Child"
Elif age >= 20
Print "adult"
Else
Print "teenager"
4. Stop

Pseudocode :-

A = input "Age "


If a is smaller than 13
Display " Child "
Elif a is greater or equal 20
Display "adult"
Else
Display " teenager "

Q. To print all natural numbers up to n.


Ans :-
Algorithm :-

1. Start
2. Input n
3. For I in range ( 1, n + 1 )
Print I
4. Stop

Pseudocode:-

N = input "N "


For I in range ( 1 to n + 1 )
Display I

Q. To print n odd numbers.


Ans :-
Algorithm :-
1. Start
2. Input n
3. For I in range ( 1 , 2 * n + 1 )
If I % 2 != 0 :
Print I
5. Stop

Pseudocode :-

N = input n
For I in range ( 1 to 2 * n +1)
If I is not divisible by 2
Display I

Q. To print square of a number.


Ans :-
Algorithm :-
1. Start
2. Input a number
3. Square = number ** 2
4. Print Square
5. Stop

Pseudocode :-

N = input "number "


Square = n * n
Display square

Q. To accept 5 numbers and find their average.


Ans:-
Algorithm :-
1. Start
2. Input a , b, c , d, e
3. Sum a + b + c + d + e
4. Average Sum / 5
5. Print Average
6. Stop

Pseudocode :-

A = input first number


B = input second number
C = input third number
D = input fourth number
E = input fifth number
Sum = a + b + c + d + e
Average = Sum / 5
Display Average

Q. To accept numbers till the user enters and then find their average.
Ans.
Algorithm :-
1. Start
2. Sum = 0
3. Count = 0
4. While True
Input number
Sum += number
Count += 1
Input choice to quit enter yes
If choice == yes
Break
5. Average = Sum / count
6. Print average
7. Stop

Pseudocode :-

Sum = 0
Count = 0
While True
N = input "Number "
Sum is equal to sum + number
Count is equal to count + number
Choice = input "to quit enter quit"
If choice is equal to quit
Break
Average = sum / count
Display Average

Q. To print squares of first n numbers.


Ans :-
Algorithm:-

1. Start
2. Input a number
3. For I in range ( number +1 )
Square = I ** 2
Print Square
4. Stop
Pseudocode :-

N = input "a number"


For I in range 0 to n + 1
Square = I ** 2
Display square
Q. To print the cube of a number.
Ans :-
Algorithm:-

1. Start
2. Input a number
3. Cube = number ** 3
4. Print cube
5. Stop

Pseudocode:-

N = input a number
Cube = n ** 3
Display cube

Q. To print cubes of first n numbers.


Ans :-
Algorithm:-
1. Start
2. Input a number
3. For I in range ( number +1 )
Cube = I ** 3
Print cube
4. Stop

Pseudocode :-

N = input "a number"


For I in range 0 to n + 1
Cube = I ** 3
Display cube
Q. To find sum of n given numbers.
Ans:-
Algorithm :-

1. Start
2. Sum = 0
3. While True
Input number
Sum += number
Choice = input to quit enter quit
If choice == quit
Break
4. Print Sum
5. Stop

Pseudocode :-

Sum = 0
While True
N = input a number
Sum equal to sum + n
Choice = input to quit enter quit
If choice is quit
Break
Display sum
Q. To find factorial of a given number.
Ans :-
Algorithm :-

1. Start
2. Input number
3. Fact = 1
4. For I in range number + 1
Fact *= I
5. Print Fact

Pseudocode :-

N = input a number
Fact is 1
For I in rang n +1
Fact equal to fact * I
Display Fact

You might also like