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

readers and writers problem

1 Answers   Cognizant,


write a c program, using for loop, that accepts and odds two numbers. The output must be the sum and the addens. This should be repeated 5 times while the first number is decremented by one and the second number is incremented by 1.

2 Answers   IBM, Infosys,


write a program that reads a series of strings and prints only those strings begging with letter "b"

0 Answers  


what is the use of using for loop as "for(;;)"?

5 Answers   Satyam,


Given 1 to n random number, find top 10 maximum numbers and explain the time complexity of the algorithm.

1 Answers   IMO, NetApp,






Code for Two Classes for Doing Gzip in Memory?

0 Answers  


Code for Small C++ Class to Transform Any Static Control into a Hyperlink Control?

0 Answers   Wipro,


how to find out the maximum number out of the three inputs.

6 Answers   ABC, Apple, C3I, HP, TCS,


Algorithm in O(2n) Presently we can solve in our hypothetical machine problem instances of size 100 in 1 minute using algorithm A, which is a O(2n). We would like to solve instances of size 200 in 1 minute using algorithm A on a new machine. What is the speed of the new machine should be?

2 Answers   ABC, Qatar University,


what mean void creat_object?in public class in this code class A{ public: int x; A(){ cout << endl<< "Constructor A";} ~A(){ cout << endl<< "Destructor A, x is\t"<< x;} }; void create_object(); void main() { A a; a.x=10; { A c; c.x=20; } create_object(); } void create_object() { A b; b.x=30; }

0 Answers  


Code for Easily Using Hash Table?

0 Answers  


Show by induction that 2n > n2, for all n > 4.

2 Answers   Karvy, Qatar University,


Categories