Urgent Help plz....

This is a discussion on Urgent Help plz.... within the C Programming forums, part of the General Programming Boards category; Here is the problem in output of my program that it prints the statement many times but I want that ...

  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
    21,593
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    681
    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 04:48 AM.
    I never put signature, but I decided to make an exception.

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, 05:39 AM
  3. please please help me its urgent
    By youngashish in forum C++ Programming
    Replies: 8
    Last Post: 12-06-2004, 08: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, 09:09 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21