Question:

Need Help in C LANGUAGE?

by  |  earlier

0 LIKES UnLike

printf("Enter a Number: ");

how will it be able to recognize if it is a prime number or a composite number? our topic is looping. please help me. heres the output i need...

"ENTER A NUMBER:"

then if i enter a number.. the words "YOU ENTERED A PRIME NUMBER" or "YOU ENTERED A COMPOSITE NUMBER" must appear under "ENTER A NUMBER:"

sample output:

Enter a Number: 7

You Entered a Prime Number

please help me...

 Tags:

   Report

2 ANSWERS


  1. #include <iostream>

    using namespace std;

    int main()

    {

        int number=0;

        cout << "Enter a number: ";

        cin >> number;

    cout << endl;



        if ( number == 2 || number == 3 )

        {

            cout << "You entered a Prime Number";

            return 0;

        }



    int i = 2;

        while ( (i <= number/2) && (number%i != 0) )

    {

    i++;

    }



    if ( i == number/2 )

    cout << "You entered a Prime Number";

    else

    cout << "You entered a Composite Number";



    return 0;

    }


  2. Get your number from user and store in testValue (int)

    (int) testLimit = testValue / 2

    (bool) isPrime=allDone = false

    (int) loopDivisor = 2

    while (!allDone){

    (int) remainder = testValue % loopDivisor

    if (remainder==0) isPrime = allDone = true;

    if (++loopDivisor>testLimit) allDone=true;

    }

    print appropriate message based on isPrime

Question Stats

Latest activity: earlier.
This question has 2 answers.

BECOME A GUIDE

Share your knowledge and help people by answering questions.