Sunday, February 9, 2020

Prime or not given number program in C++ and java program

Image result for prime or not given number concept?

Prime or not given number program in C++  Program

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a,b,c=0;
cout<<"\nEnter any number :";
//Prime number - 47
cin>>a;
for(b=2;b<=a/2;b++)
{
if (a%b==0)
{
c=1;
break;
}
}
if (c==1)
cout<<"Given number is not Prime";
else
cout<<"Given number is Prime";
getch();
}  


Prime or not given number program in Java  Program


 import java.util.Scanner;
class prime
{
public static void main(String []args)
{
int a,b,c=0;
Scanner  obj=new Scanner(System.in);
System.out.println("  Enter a number :"  );
a=obj.nextInt();
for(b=2;b<=a/2;b++)
{
if (a%b==0)
{
c=1;
}
}
if(c==1)
System.out.println("   Given number is not Prime" );
else
System.out.println("   Given number is  Prime ");
}
}

No comments:

Post a Comment