Write a program that takes a 3 digit number n and finds out
whether
the number 2^n + 1 is prime, or if it is not prime find out its
factors.

Answers were Sorted based on User's Feedback



Write a program that takes a 3 digit number n and finds out whether the number 2^n + 1 is prime, o..

Answer / sreejesh1987

//Sorry, this code is wrong
//but'll be useful for some other logic
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,n,x,flag=0,d;
clrscr();
printf("\tPRIME CHECK\n");
printf("Enter a 3digit no:");
scanf("%d",&n);

if((n/100>0)&&(n/100<10))
printf("\nNumber is of 3 Digits");
else
printf("\nNot a 3 digit number");

for(j=2;j<n,n%j!=0;j++)
if(n-1==j)
{
printf("\n\t%d is a prime",n);

flag=1;

d=n-1;
while(d%2==0)
d=d/2;

if(d==1)
printf("\nNumber%d is in 2^n+1 format",n);
}

if(!flag)
{
printf("\nNumber %d is not a prime",n);
printf("\nIts factors are:\n\t");

x=2;
while(n!=1)
{
while(n%x==0)
{
n=n/x;
printf("%d ",x);
}
x++;
}
}
getch();
}

Is This Answer Correct ?    16 Yes 6 No

Write a program that takes a 3 digit number n and finds out whether the number 2^n + 1 is prime, o..

Answer / beena

#include <iostream>
using namespace std;
void prime_num(int);
int main()
{
cout << " Enter a number ";
int num = 0;
cin >> num;

if((num/100>0)&&(num/100<10))
printf("\nNumber is of 3 Digits");
else
printf("\nNot a 3 digit number");

bool flag = prime_num(num);

if(!flag)
{
printf("\nNumber %d is not a prime",n);
printf("\nIts factors are:\n\t");

x=2;
while(num!=1)
{
while(num%x==0)
{
num=num/x;
printf("%d ",x);
}
x++;
}
}

}

bool prime_num( int num)
{
bool isPrime=true;

int checkNum = 1;
for(int i = 1; i <=num; i++)
checkNum *= 2;
checkNum +=1;

for ( int i = 0; i <= checkNum; i++)
{
for ( int j = 2; j <= checkNum; j++)
{
if ( i!=j && i % j == 0 )
{
isPrime=false;
break;
}
}
if (isPrime)
{
cout <<"Prime:"<< i << endl;
}
isPrime=true;
}
return isPrime;
}

Is This Answer Correct ?    5 Yes 5 No

Write a program that takes a 3 digit number n and finds out whether the number 2^n + 1 is prime, o..

Answer / srikanthgowda

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

int main()
{

int n,a[100];
cout<<"Enter size of the array"<<endl;
cin>>n;

cout<<"Enter the any value in 3digit format"<<endl;
for (int i=0;i<n;i++)
cin>>a[i];

for (int i=0;i<n;++i)
{

if((a[i]/100)>10)
{
cout<<"invaild"<<endl;
exit(0);
}
}

cout<<"Vaild within 3idigit number"<<endl;


int temp=pow(2,a[n-1]);
temp=temp+1;
cout<<temp;
int count=0;
for(int i=1;i<=1000;i++)
if( (temp)%i == 0 )

{
count++;
}


if (count==2)
{
cout<<endl<<temp<<"-->%2^n+1%"<<"is prime number"<<endl;
}
else
{
cout<<endl<<temp<<" is not primre number
";
}


return 0;
}

Is This Answer Correct ?    0 Yes 0 No

Post New Answer

More C++ Code Interview Questions

write a program to calculate the amount of investment after a period n years if the principal investors was p and interest is calculated using compound interest,formular=a=p(1+r)^n

0 Answers   Jomo Kenyatta University,


write a program using 2 D that searches a number and display the number of items 12 inputs values input 15,20, 13, 30, 38, 40,16, 18, 20 ,18 ,20 enter no. to search : 20

0 Answers  


1. Write a program using one dimensional array that calculates the sum and average of the five input values from the keyboard and prints the calculated sum and average.

2 Answers  


Code for Two Classes for Doing Gzip in Memory?

0 Answers  


1+1/2!+1/3!+...+1/n!

0 Answers  






write a program that can locate elements in array.

1 Answers  


how to write a program that opens a file and display in reverse order?

0 Answers   SMC,


what is the best algorithm to sort out unique words from a list of more than 10 million words(1 crore+)? we need the best technique in the terms of execution time.

9 Answers   TCS,


write a program that calculate the volume of cylinder after user enters radius and height and show the algorithm used

1 Answers   Jomo Kenyatta University,


Write a program using two-dimensional arrays that determines the highest and lowest of the 12 input values. Example: Enter 12 numbers: 13 15 20 13 35 40 16 18 20 18 20 14 highest: 40 lowest: 13

1 Answers  


Ask the user to input three positive integers M, N and q. Make the 2 dimensional array of integers with size MxN, where all the elements of I (I = 1,…,M) line will be members of geometrical progression with first element equal to the number of line (I) and denominator q.

0 Answers  


What output does this program generate as shown? Why? class A { A() { cout << "A::A()" << endl; } ~A() { cout << "A::~A()" << endl; throw "A::exception"; } }; class B { B() { cout << "B::B()" << endl; throw "B::exception"; } ~B() { cout << "B::~B()"; } }; int main(int, char**) { try { cout << "Entering try...catch block" << endl; A objectA; B objectB; cout << "Exiting try...catch block" << endl; } catch (char* ex) { cout << ex << endl; } return 0; }

0 Answers  


Categories