Monday, 25 March 2019

CP: Prime number print


Que: Write a program that accepts a number from the user and prints prime numbers from 0 to that number.

The prime number is a positive integer greater than 1 that is only divisible by 1 and itself

For example 2, 3, 5, 7, 11 are the first five prime numbers.

Code:


#include<stdio.h>
void main()
{
    int n,i,fact,j;

    printf("\nEnter the Number = ");
    scanf("%d",&n);

    printf("\nPrime Numbers are: \n");

    for(i=1; i<=n; i++)
    {
        fact=0;
        for(j=1; j<=n; j++)
        {
            if(i%j==0)
                fact++;
        }
        if(fact==2)
            printf("%d " ,i);
    }
    getch();

}

Output: 


No comments:

Post a Comment

LAB 7 Arduino with Seven Segment Display || Arduino Tutorial || Code and Circuit Diagram || Project

  LAB 7 Arduino with Seven Segment Display || Arduino Tutorial || Code and Circuit Diagram || Project Dear All We will learn how to Connec...