Thread: Urgent Help plz....

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    54

    Urgent Help plz....

    Here is the problem in output of my program that it prints the statement many times but I want that it print only one line.
    (The program's objective is to check the given number is prime or not)

    Code:
    #include <stdio.h>
    void main() 
    { 
    
    int x,y; 
    printf("Enter the number "); 
    scanf("%d",&x); 
    for(y=2; y<=x; y++)
    { 
    if(x%y==0) 
    printf("Number is not prime"); 
    } 
    
    if(x==y) 
    printf("Number is prime"); 
    
    
    }
    OUTPUT
    ______

    Enter the number: 18
    Number is not prime
    Number is not prime
    Number is not prime
    ...

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    For starters, you should:
    • Indent your code properly.
    • Change void main to int main.

    As for your problem: set a boolean flag to 1 if the number is not prime. This way, you check the flag variable after the loop.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Nov 2012
    Posts
    54
    please type this code for me as u r saying. Its urgent sometime left only in posting my assignment to teacher.

  4. #4
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    733
    Quote Originally Posted by shansajid View Post
    please type this code for me as u r saying. Its urgent sometime left only in posting my assignment to teacher.
    Noone is gonna do the homework for you. laserlight has already told you what to do. Don't print anything in the loop.
    Before the loop, declare a variable:

    Code:
    bool isPrime = true;
    and then in the loop set it to false if you know the number is not prime (and break).

    After the loop finishes, check the value of `isPrime`:

    Code:
    if (isPrime) 
        printf("Number is prime"); 
    else
        printf("Number is not prime");
    You will also have to fix the loop's condition.
    Last edited by kmdv; 01-05-2013 at 05:48 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. HELP!! Urgent !!
    By huwan in forum C++ Programming
    Replies: 21
    Last Post: 06-15-2007, 10:04 AM
  2. Urgent Help!!!
    By Chri5ty in forum C++ Programming
    Replies: 5
    Last Post: 01-17-2007, 06:39 AM
  3. please please help me its urgent
    By youngashish in forum C++ Programming
    Replies: 8
    Last Post: 12-06-2004, 09:20 AM
  4. need help..URGENT!
    By webmasts in forum C++ Programming
    Replies: 5
    Last Post: 04-18-2003, 11:08 PM
  5. Please Help! Urgent!
    By Hunterhunter in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 03-25-2003, 10:09 PM