Skip to content
Menu
Code for Java c
  • Home
  • Java
    • Java Examples
    • Java tutorials
  • C
    • C tutorials
    • C Examples
  • C++
    • C++ Tutorials
    • C++ Examples
  • Python
    • Python Tutorials
    • Python Examples
  • About
    • About me
    • contact us
    • disclaimer
    • Privacy Policy
Code for Java c
Code to display prime numbers from 1 to 100 or 1 to n in Python

C++ code to display prime numbers from 1 to 100 or 1 to n

Posted on November 13, 2020November 14, 2020

Table of Contents

  • C++ code to display prime numbers from 1 to 100 or 1 to n
    • code to display prime numbers from 1 to 100 or 1 to n using for loop
    • Code to display prime numbers from 1 to 100 or 1 to n using while loop
    • Code to display prime numbers from 1 to 100 or 1 to n using do- while loop
    • Code to display prime numbers from 1 to 100 or 1 to n using function

C++ code to display prime numbers from 1 to 100 or 1 to n

In this article, we will discuss the concept of C++ code to display prime numbers from 1 to 100 or 1 to n

In this code, we are going to learn how to print prime number from 1 to 100 or 1 to n using several ways in C++ language.

This is done using for loop , while loop , do-while loop and function in C++ language

code to display prime numbers from 1 to 100 or 1 to n using for loop

In this program, we will print prime numbers from 1 to 100 or 1 to n  using a for loop in C++ language

Program 1

#include <iostream>
#include <conio.h>
using namespace std;

int main()
{
    int i,j,n;
    cout<<"Enter the number until which want to print prime\n";
    cin>>n;
    cout<<"Prime numbers 1 to "<<n<<" are:"<<endl;
    for(i=2; i<=n; i++){
        int counter=0;
        for(j=1; j<=i; j++){
        if(i%j==0){
            counter++;
        }
    }
    if(counter==2){
        cout<<i<<" ";
    }
    }
getch();
    return 0;
}

When the above code is executed, it produces the following result

Case 1

Enter the number until which want to print prime
100
Prime numbers 1 to 100 are
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97

Case 2

Enter the number until which want to print prime
30
Prime numbers 1 to 30 are
2 3 5 7 11 13 17 19 23 29

 

Code to display prime numbers from 1 to 100 or 1 to n using while loop

In this program, we will print prime numbers from 1 to 100 or 1 to n  using a while loop in C++ language

Program 2

#include <iostream>
#include <conio.h>
using namespace std;

int main()
{
    int i,j,n;
    cout<<"Enter the number until which want to print prime\n";
    cin>>n;
    cout<<"Prime numbers 1 to "<<n<<" are:"<<endl;
    i=2;
    while(i<=n){
        int counter=0;
        j=1;
        while(j<=i){
        if(i%j==0){
            counter++;
        }
         j++;
    }
    if(counter==2){
        cout<<i<<" ";
    }
     i++;
    }
getch();
    return 0;
}

When the above code is executed, it produces the following result

Case 1

Enter the number until which want to print prime
100
Prime numbers 1 to 100 are
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97

Case 2

Enter the number until which want to print prime
60
Prime numbers 1 to 60 are
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59

 

Code to display prime numbers from 1 to 100 or 1 to n using do- while loop

In this program, we will print prime numbers from 1 to 100 or 1 to n  using a do-while loop in C++ language

Program 3

#include <iostream>
#include <conio.h>
using namespace std;

int main()
{
    int i,j,n;
    cout<<"Enter the number until which want to print prime\n";
    cin>>n;
    cout<<"Prime numbers 1 to "<<n<<" are:"<<endl;
    i=2;
    do{
        int counter=0;
        j=1;
        do{
        if(i%j==0){
            counter++;
        }
         j++;
    }while(j<=i);
    if(counter==2){
        cout<<i<<" ";
    }
     i++;
    }while(i<=n);
getch();
    return 0;
}

 

When the above code is executed, it produces the following result

Case 1

Enter the number until which want to print prime
100
Prime numbers 1 to 100 are
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97

Case 2

Enter the number until which want to print prime
80
Prime numbers 1 to 80 are
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79


Code to display prime numbers from 1 to 100 or 1 to n using function

In this program, we will print prime numbers from 1 to 100 or 1 to n  using function in C++ language

Program 4

#include <iostream>
#include <conio.h>
using namespace std;

int printPrime(int number);
int main()
{
    int max,i;
    cout<<"Enter the maximum value for range\n";
    cin>>max;
    cout<<"Prime numbers from 1 to "<<max <<" are\n";
    for(i=1; i<=max; i++){
    if(printPrime(i)==0)
        cout<<i<<" ";
}
  getch();
    return 0;
}
int printPrime(int num){
int counter=0,i;
for(i=2; i<num; i++){
    if(num%i==0)
    {
        counter=1;
        break;
    }
}
if(num==1)counter=1;
return counter;

}

When the above code is executed, it produces the following result

Case 1

Enter the maximum value for range
100
Prime numbers 1 to 100 are
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97

Case 2

Enter the maximum value for range
20
Prime numbers 1 to 20 are
2 3 5 7 11 13 17 19

 

Suggested post

Operator in C++ language

for loop in C++ language

while loop in C++ language

do-while loop in C++ language

 

Similar post

Java programming code to check prime or not

C programming code to check prime or not

C++ programming code to check prime or not

Python programming code to check prime or not

 

Code to print prime numbers from 1 to 100 or 1 to n in Java

Code to print prime numbers from 1 to 100 or 1 to n in C

Code to print prime numbers from 1 to 100 or 1 to n in C++

Code to print prime numbers from 1 to 100 or 1 to n in Python

Related

Recent Posts

  • Subtract two numbers using method overriding
  • PHP Star triangle Pattern program
  • Using function or method to Write temperature conversion : Fahrenheit into Celsius
  • Function or method:temperature conversion from Fahrenheit into Celsius – Entered by user
  • Write temperature conversion program: Fahrenheit into Celsius
  • How to write a program to convert Fahrenheit into Celsius

tag

Addition (6) Array (38) C++ language (91) C language (98) c sharp (23) Division (6) Function (29) if else (32) Java language (102) JavaScript (5) loops (137) Multiply (7) Oop (2) patterns (65) PHP (13) Python Language (38) Subtraction (7) temperature (20)

Archives

Categories

Address

Global information technology

Puloly south, PointPedro

Jaffna

Srilanka

©2025 Code for Java c | Powered by SuperbThemes