Answers for "c program print prime numbers"

C
0

Write a c program to show the prime factor of a given number.

/**
 * C program to find all prime factors of a given number
 */

#include <stdio.h>

int main()
{
    int i, j, num, isPrime;

    /* Input a number from user */
    printf("Enter any number to print Prime factors: ");
    scanf("%d", &num);

    printf("All Prime Factors of %d are: n", num);

    /* Find all Prime factors */
    for(i=2; i<=num; i++)
    {
        /* Check 'i' for factor of num */
        if(num%i==0)
        {
            /* Check 'i' for Prime */
            isPrime = 1;
            for(j=2; j<=i/2; j++)
            {
                if(i%j==0)
                {
                    isPrime = 0;
                    break;
                }
            }

            /* If 'i' is Prime number and factor of num */
            if(isPrime==1)
            {
                printf("%d, ", i);
            }
        }
    }

    return 0;
}
Posted by: Guest on September-01-2021
-1

prime numbers in c

#include<stdio.h>
main() {
    int n, i, count=0;
    printf("Enter Number");
    scanf("%d",&n);
   
    for ( i = 1; i<= n; i++) {
        if (n % i == 1) {
            count++;
        }
    }
    if( count == 2) {
        printf("Prime Number");
    }
    else {
        printf("Not A  Prime Number");
    }
}
Posted by: Guest on May-18-2021
0

c program to check prime number

//program to find prime number or not using c programming
#include<stdio.h>
#include<conio.h>
// program starts from here after main() function
    void main(){
    int n; 
    int i,count =0;
    // count is incremented when the loop is true.
    printf("Enter the numbern");
    scanf("%d",&n);
    for (i=1; i<=n; i++){
        if(n%i==0){
            count++;
        }
    }
     // for the prime number the no of count is 2
     // number is divided by 1 or itself(given number) so count ==2
        if(count ==2){
            printf("%d is prime number.n",n);
        }
        
    // for the composite number the number is divided by more than 2 number.
    else{
         printf("%d is not prime numbern",n);
    }

    getch();
    }
Posted by: Guest on October-01-2021

Code answers related to "c program print prime numbers"

Code answers related to "C"

Browse Popular Code Answers by Language