Reduce the number to zero | Mitsogo

Mitsogo Question:
This mitsogo question is asked in the mitsogo 2nd round interview. To reduce the number to zero by using certain conditions.
Conditions:
- Check whether the given number is prime or not.
- If the number is prime then decrement the number by 1.
- Otherwise, find the greatest prime number(GPN) which replaces the number.
- Then replace the number by GPN.
- Repeat the steps until the number became zero and count the number of steps taken.
- At last, we have the output to reduce the number to zero
Explanation:
- Let us consider input is 21
- The given number is not prime, then check the greatest prime number(GPN) which divides 21.
- And the GPN is 7, then replace 21 by 7 count is 1
- Now 7 is a prime number then decrement the number to 6 count is 2. Then 6 is not prime.
- The GPN which divides 6 is 3 then replace it count is 3.
- Now 3 is a prime number then decrement to 2 and 2 is also a prime then decrement 1 count 5
- At last, we get zero then print the count is 6
Program:
[code lang=”c”]
#include <stdio.h>
int isprime(int n) //Check the number is prime or not
{
int i;
for(i=n/2;i>1;i–)
{
if(n%i==0)
{
return 0; //return when the number not prime
}
}
return 1;
}
int greatest_prime(int n) //Find the greatest prime number divide n
{
int i,j;
for(i=n/2;i>1;i–)
{
if(n%i==0)
{
j=isprime(i); //check the divided number is prime or not
}
if(j==1)
{
return i; //return greatest prime number
}
}
}
void main()
{
int n,i,j=0; //j can count the number of steps taken to convert the number to zero
printf("Enter the number:");
scanf("%d",&n);
while(n>0)
{
i=isprime(n);
if(i==1) //If the number is prime then decrement the number by 1
{
n–;
j++;
}
else //not prime number
{
i=greatest_prime(n);
n=i; //simplify number by replacing greatest prime number
j++;
}
i=0;
}
printf("%d",j);
}
[/code]
You might also like:
C Program For String Reversal | Samsung
visit: Mitsogo
Interview Process:
1.aptutude test,2.logical reasoning,3.coding+essay writing
4.technical interview, all these rounds are elimination rounds, followed by 3 hr rounds.
Interview overview:
- The aptitude round contains 60 questions with multiple choice.
- Each wrong question carries a negative mark as well.
- The logical round contains 2 problems which medium-difficult
- HR round contains questions like tell me about your project? what is your favorite subject etc,