In this post we will solve Tech Mahindra off campus coding question. We will see step by step algorithm for coding question and finally we will implement the code in java.

**Tech Mahindra off Campus Coding Question**

**Question:**

Write an algorithm which accepts an Integer from user say n . After that return the difference beween lowest prime factor and input number as output.

** Prime factors:** The prime numbers that divides an integer exactly are called as prime factors.

Assumptions: n>1

Note: 1 is not a prime number.

__Example:__

Input:

n: 350

__output:__

348

__Explanation:__

350=2x5x5x7 , therefore prime factors of 350 are 2,5,7. Since 2 is the smallest prime factor of 350 , difference will be 350-2=348.

**Algorithm**

- First step will be to take the input from the user which is n
- Find the prime factors for number
- sort the prime factors in ascending order (we will get in this order itself )
- Take the n value and find the difference between n and lowest prime factor of the number.
- congrats you just solved the off campus coding problem, we have lot of problems like this to know more Click Here.

**Java Code for Prime Factors Difference Problem **

import java.util.*; public class primefactors { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int m = n; int array1[] = new int[10]; int k = 0; int res = 0; while (n > 1) { for (int j = 2; j > 1; j++) { if (n % j == 0) { array1[k] = j; k += 1; n = n / j; j = j - 1; } else { continue; } } } res = m - array1[0]; System.out.println(res); } }

**To get More Explanation for Above Code Visit our channel **