C C++ program to generate series of prime numbers

C C++ program to generate series of prime numbers

Looping और iterative statements के बारे में और अधिक जानने के लिए देखें— Looping statements in C

C Language में character set क्या होता है जानने के लिए देखें—Character set of C

C Language में Token क्या होता है जानने के लिए देखें—Tokens in C

Flowchart to generate series of prime numbers

Generating prime numbers upto n flowchart

C program to generate series of prime numbers

#include<stdio.h>
int main()
{
    int num=1, c=2, n;
    char prime='t';
    printf("Enter n: ");
    scanf("%d",&n);
    while(num<=n)
    {
        c=2;
        prime='t';
        while(c<num)
        {
            if(num%c==0)
            {
                prime='f';
                break;
            }
            c++;
        }
        if(prime=='t')
        {
            printf("%d\n",num);
        }
        num++;
    }
    return 0;
}

C++ program to generate series of prime numbers

#include<iostream>
using namespace std;
int main()
{
    int num=1, c=2, n;
    char prime='t';
    cout<<"Enter n: ";
    cin>>n;
    while(num<=n)
    {
        c=2;
        prime='t';
        while(c<num)
        {
            if(num%c==0)
            {
                prime='f';
                break;
            }
            c++;
        }
        if(prime=='t')
        {
            cout<<num<<"\n";
        }
        num++;
    }
    return 0;
}

Output of C C++ program to generate series of prime numbers

Enter n: 10
1
2
3
5
7
Enter n: 20
1
2
3
5
7 
11
13
17
19

Some most common looping programs in C C++

  1. Generate series of natural numbers (while, for, do while)
  2. Generate series of natural numbers and their sum
  3. Generate series of odd numbers and find their sum
  4. Generate series of even numbers and find their sum
  5. Generate table of given number
  6. Find factorial of given number
  7. Find power of given number
  8. Generate multiplication table of given number
  9. HCF and LCM of two numbers
  10. Generating Fibonacci series
  11. Checking prime number
  12. Sum of digits of given number
  13. Reverse of digits of given number
  14. Checking palindrome number
  15. Checking Armstrong number
  16. Generating series of Armstrong numbers
  17. Convert decimal to binary
  18. Convert binary to decimal
Share it to:

Published by

admin

I am a computer teacher, programmer and web developer