C Language

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

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

Program 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 <stdio.h>
#include <stdlib.h>

int main()
{
    int i,j,n;
    printf("Enter the number until which want to print prime\n");
    scanf("%d",&n);
    printf("Prime numbers 1 to %d are\n",n);
    for(i=2; i<=n; i++){
        int count=0;
        for(j=1; j<=i; j++){
        if(i%j==0){
            count++;
        }
    }
    if(count==2){
        printf("%d ",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

Program 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 <stdio.h>
#include <stdlib.h>

int main()
{
    int i,j,n;
    printf("Enter the number until which want to print prime\n");
    scanf("%d",&n);
    printf("Prime numbers 1 to %d are\n",n);
    i=2;
    while(i<=n){
        int count=0;
        j=1;
        while(j<=i){
        if(i%j==0){
            count++;
        }
         j++;
    }
    if(count==2){
        printf("%d ",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
50
Prime numbers 1 to 50 are
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47

 

Program 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 <stdio.h>
#include <stdlib.h>

int main()
{
    int i,j,n;
    printf("Enter the number until which want to print prime\n");
    scanf("%d",&n);
    printf("Prime numbers 1 to %d are\n",n);
    i=2;
    do{
        int count=0;
        j=1;
       do{
        if(i%j==0){
            count++;
        }
         j++;
    } while(j<=i);
    if(count==2){
        printf("%d ",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
75
Prime numbers 1 to 75 are
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73


Program 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 user defined function in C language

Program 4

#include <stdio.h>
#include <stdlib.h>
int printPrime(int number);
int main()
{
    int max,i;
    printf("Enter the maximum value for range\n");
    scanf("%d",&max);
    printf("Prime numbers from 1 to %d are\n",max);
    for(i=1; i<=max; i++){
    if(printPrime(i)==0)
        printf("%d ",i);
}
  getch();
    return 0;
}
int printPrime(int num){
int count=0,i;
for(i=2; i<num; i++){
    if(num%i==0)
    {
        count=1;
        break;
    }
}
if(num==1)count=1;
return count;

}

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 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

 

Suggested post

for loop in C language

while loop in C language

do-while loop in C language

if statements in C language

function 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

 

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

 

Karmehavannan

Recent Posts

Multiply two numbers in Java using scanner| 5 different ways

Multiply two numbers in Java using scanner| 5 different ways In this article, we will…

3 months ago

5 different ways to Divide two numbers in Java using scanner

5 Different ways to Divide two numbers in Java using scanner In this article, we…

3 months ago

Learn 8 Ways to Subtract Two Numbers Using Methods in Java

Learn 8 Ways to Subtract Two Numbers Using Methods in Java In this article, we…

4 months ago

10 ways to subtract two numbers in Java

10 ways to subtract two numbers in Java In this article, we will discuss the…

4 months ago

Java Code Examples – Multiply Two Numbers in 5 Easy Ways

Java Code Examples – Multiply Two Numbers in 5 Easy Ways In this article, we…

4 months ago

How to Divide two numbers in Java| 5 different ways

How to Divide two numbers in Java| 5 different ways In this article, we will…

4 months ago

This website uses cookies.