What will be the output of the following pseudocode if a=10 and b=6?
1) Integer func (Integer a, Integer b)
2) Integer temp
3) While(b)
4) temp = a MOD b
5) a = b
6) b = temp
7) End-while
8) return a
9) End-function func()
Answers
Answered by
3
Answer:
a = 10
b = 6
while(b) temp = a%b a = b b = temp
true temp = 10%6 a = 6 b = 4
= 4
true temp = 6%4 a = 4 b = 2
= 2
true temp = 4%2 a = 2 b = 0
= 0
false
The function will return the value 2.
So, the output is 2.
Program in C++:
#include<iostream>
using namespace std;
int func(int a, int b)
{
int temp;
while(b)
{
temp = a%b;
a = b;
b = temp;
}
return a;
}
int main()
{
cout<<func(10,6);
return 0;
}
Similar questions