View Code of Problem 91

#include <iostream>
#include <math.h>
using namespace std;

bool isprime(int n){
  if(n<=1)
    return false;
  int bound = (int)sqrt(n)+1;
  for(int j=2;j<bound;j++){
    if(n%j==0)
      return false;
  }
  return true;
}

int main(){
  int n,count=0;
  cin>>n;
  if(isprime(n))
    printf("%d=1*%d",n,n);
  else{
    printf("%d=",n);
    for(int j=2;j<n;j++){
      if(isprime(j)){
        while(n%j==0){
          if(count==0)
            printf("%d", j);
	  else
	    printf("*%d", j);
	  n=n/j;
	  count++;
        }
      }
    }
  }
  return 0;
}

Double click to view unformatted code.


Back to problem 91