+ 4

A program that prints all the prime numbers from 1 to n ? Where n is taken as an input from the user .

lets say , the user asks to print all the prime numbers from 1 to 10 . 10 will be an input from the user .

24th Mar 2017, 2:21 PM
Shru
Shru - avatar
10 Réponses
+ 7
ok so here is the right one import java.io.*; import java.util.*; public class Primes{ public static boolean is_prime(int x){ int y=1,numOfFactors=0; for(y=1;y<=x;y++){ if(x%y==0){ numOfFactors+=1; } } if(numOfFactors==2){ return true; } else{ return false; } } public static void main (String [] args) throws IOException{ Scanner s=new Scanner(System.in); System.out.println("Enter the range(1 to ??): "); int x=1, l=s.nextInt(); for(x=1;x<=l;x++){ if(is_prime(x)){ System.out.println(x); } } } }
24th Mar 2017, 3:35 PM
Prabhakar Dev
Prabhakar Dev - avatar
+ 6
ok
24th Mar 2017, 3:35 PM
Prabhakar Dev
Prabhakar Dev - avatar
+ 6
#include<iostream> using namespace std; bool is_prime(int x){ int y=1,NOF=0; for(y=1;y<=x;y++){ if(x%y==0){ NOF+=1; } } if(NOF==2){ return true; } else{ return false; } } int main(){ cout<<"Enter the range(1 to ??): "; int n; cin>>n; for(int x=1;x<=n;x++){ if(is_prime(x)){ cout<<x<<endl; } } }
24th Mar 2017, 3:45 PM
Prabhakar Dev
Prabhakar Dev - avatar
+ 4
Well if I see my code nicely then it is a little wrong too, wait 5 minutes
24th Mar 2017, 3:33 PM
Prabhakar Dev
Prabhakar Dev - avatar
+ 4
sir?? I am 13
24th Mar 2017, 4:25 PM
Prabhakar Dev
Prabhakar Dev - avatar
+ 2
Python: def manual(n): for i in range(1,n+1): if i % 2 != 0: print(i)
24th Mar 2017, 2:55 PM
Ateeb Ahmed
Ateeb Ahmed - avatar
+ 2
thanks muCh
24th Mar 2017, 3:25 PM
Shru
Shru - avatar
+ 2
agreed with prabhakar
24th Mar 2017, 3:30 PM
Shru
Shru - avatar
+ 2
umm prabhakar , can you write me a c++ code of this ??
24th Mar 2017, 3:34 PM
Shru
Shru - avatar
+ 2
thank you so much sir .
24th Mar 2017, 3:58 PM
Shru
Shru - avatar