+ 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 .
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);
}
}
}
}
+ 6
ok
+ 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;
}
}
}
+ 4
Well if I see my code nicely then it is a little wrong too, wait 5 minutes
+ 4
sir??
I am 13
+ 2
Python:
def manual(n):
for i in range(1,n+1):
if i % 2 != 0:
print(i)
+ 2
thanks muCh
+ 2
agreed with prabhakar
+ 2
umm prabhakar , can you write me a c++ code of this ??
+ 2
thank you so much sir .