Thread: small issue, can't seem to fix

  1. #1
    Registered User
    Join Date
    Feb 2019
    Posts
    6

    small issue, can't seem to fix

    This program is used to check whether a number is prime or not. it works correctly but repeats a portion of checking. i will show what i mean below.
    Code:
    #include <stdio.h>
    
    
    int main()
    
    
    {
    int num, secondTest;
    int i, x;
        printf("PRIME NUMBER CHECKER\n");
            printf("Enter Value: ");
            scanf("%d",&num);
    
    
    x=prime(num);
    
    
        if(x==0){
    
    
            for(i=1; i<num; i++){
    
    
        if(num%i==0){
            printf("\n%d = %d X %d",num,i,num/i);
        }
    
    
            }
    
    
    printf("\n%d:NOT A PRIME Number",num);
    
    
    }
    
    
        else if(x==1){
            printf("\n\n%d = %d X %d",num,1,num);
            printf("\n%d: PRIME Number",num);
    
    
    }
    
    
    do{
    
    
        printf("\nWould you like to enter another value? (1=Yes , 0=No) > ");
        scanf("%d",&secondTest);
    
    
    switch(secondTest){
    
    
        case 1:
            printf("\nEnter Value: ");
            scanf("%d",&num);
    
    
    x=prime(num);
    
    
        if(x==0){
    
    
            for(i=1; i<num; i++){
    
    
        if(num%i==0){
            printf("\n%d = %d X %d",num,i,num/i);
        }
    
    
            }
    
    
    printf("\n%d:NOT A PRIME Number",num);
    
    
    }
    
    
    else if(x==1){
            printf("%d = %d X %d",num,1,num);
            printf("%d: PRIME Number",num);
        }
    break;
    }
    
    
    }
    while(secondTest!=0);
    
    
    return 0;
    
    
    }
    
    
    int prime(num){
    int i, flag=0;
    
    
        for(i=2; i<=num/2; ++i){
    
    
    if(num%i==0)
    
    
    {
    flag=1;
    break;
    }
    
    
    }
    
    
    if (flag==0)
    return 1;
    else
    return 0;
    
    
    }
    output:

    PRIME NUMBER CHECKER
    Enter Value: 333


    333 = 1 X 333
    333 = 3 X 111
    333 = 9 X 37
    **333 = 37 X 9
    **333 = 111 X 3
    333:NOT A PRIME Number
    Would you like to enter another value? (1=Yes , 0=No) >

    the starred portion of the output is repeated which i cant figure out.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You need to indent your code properly. (Sometimes I'm nice and show examples of how to do that, but this time I'm using my phone's browser so your code is practically unreadable to me and I can't even fix it.)
    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
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    The prime suggestion for you right now: indent your code properly.

    I looked at your post history, and it seems that your problems generally stem from this. I even gave you an outline on how to indent a week or so ago.

    Quote Originally Posted by GvsuStudent
    the starred portion of the output is repeated which i cant figure out.
    I cannot figure it out either because your code is so badly indented that its logical structure is non-obvious, and I've written many magnitudes more than "20 codes" in my life, so of course you have no chance. However, if your code was properly indented, I probably could figure out in a minute, or even at a glance, and maybe you would be able to figure it out yourself, even if it takes a few more minutes.
    Last edited by laserlight; 03-10-2019 at 05:54 PM.
    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

  4. #4
    Registered User
    Join Date
    Feb 2019
    Posts
    6
    Quote Originally Posted by laserlight View Post
    The prime suggestion for you right now: indent your code properly.

    I looked at your post history, and it seems that your problems generally stem from this. I even gave you an outline on how to indent a week or so ago.


    I cannot figure it out either because your code is so badly indented that its logical structure is non-obvious, and I've written many magnitudes more than "20 codes" in my life, so of course you have no chance. However, if your code was properly indented, I probably could figure out in a minute, or even at a glance, and maybe you would be able to figure it out yourself, even if it takes a few more minutes.
    i apologize for "stupid" questions. I obviously understand indentation is something i need to work on, but i don't fully understand how this correlates to the portion of the code i'm having issues with. in general the code works as it should.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by GvsuStudent
    I obviously understand indentation is something i need to work on, but i don't fully understand how this correlates to the portion of the code i'm having issues with. in general the code works as it should.
    What you're looking at is a logic error, which typically has something to do with the flow of control through the code, which is something that is visually represented by indentation. By not having proper indentation, you obscure this flow of control, making it difficult to reason about your code.

    Here's your code with proper indentation, plus a function prototype:
    Code:
    #include <stdio.h>
    
    int prime(int num);
    
    int main(void)
    {
        int num, secondTest;
        int i, x;
        printf("PRIME NUMBER CHECKER\n");
        printf("Enter Value: ");
        scanf("%d", &num);
    
        x = prime(num);
        if (x == 0) {
            for (i = 1; i < num; i++) {
                if (num % i == 0) {
                    printf("\n%d = %d X %d", num, i, num / i);
                }
            }
            printf("\n%d:NOT A PRIME Number", num);
        }
        else if (x == 1) {
            printf("\n\n%d = %d X %d", num, 1, num);
            printf("\n%d: PRIME Number", num);
        }
    
        do {
            printf("\nWould you like to enter another value? (1=Yes , 0=No) > ");
            scanf("%d", &secondTest);
    
            switch (secondTest) {
            case 1:
                printf("\nEnter Value: ");
                scanf("%d", &num);
                x = prime(num);
                if (x == 0) {
                    for (i = 1; i < num; i++) {
                        if (num % i == 0) {
                            printf("\n%d = %d X %d", num, i, num / i);
                        }
                    }
                    printf("\n%d:NOT A PRIME Number", num);
                }
                else if (x == 1) {
                    printf("%d = %d X %d", num, 1, num);
                    printf("%d: PRIME Number", num);
                }
                break;
            }
        }
        while (secondTest != 0);
    
        return 0;
    }
    
    int prime(int num) {
        int i, flag = 0;
        for (i = 2; i <= num / 2; ++i) {
            if (num % i == 0) {
                flag = 1;
                break;
            }
        }
    
        if (flag == 0)
            return 1;
        else
            return 0;
    }
    Note also that I explicitly stated the num parameter of prime to be an int, and removed unnecessary blank lines. Generally, you should use a blank line to group portions of code, and some people have a convention of two blank lines to separate top level constructs.

    Rather than the garbled mess you had here, hidden due to poor indentation among other code:
    Code:
    x=prime(num);
     
     
        if(x==0){
     
     
            for(i=1; i<num; i++){
     
     
        if(num%i==0){
            printf("\n%d = %d X %d",num,i,num/i);
        }
     
     
            }
     
     
    printf("\n%d:NOT A PRIME Number",num);
     
     
    }
    It is now easy to see that the relevant part that is being output is controlled by a for loop that runs until i reaches num. Consequently, you repeat the other half of the factors, since you loop past the square root of x, so you start finding the factors of x that you already found by computing num / i. That you don't need to loop past the square root of x suggests a simple fix:
    Code:
    for (i = 1; i * i <= num; i++) {
        if (num % i == 0) {
            printf("\n%d = %d X %d", num, i, num / i);
        }
    }
    printf("\n%d:NOT A PRIME Number", num);
    You have to apply this fix later as well, which suggests that you should restructure your code rather than repeat yourself. (DRY principle: Don't Repeat Yourself)
    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

  6. #6
    Registered User
    Join Date
    Feb 2019
    Posts
    6
    Thank you laserlight. Your explanation is thorough and clear. I truly appreciate the time and effort you put into helping me and making me understand the importance of indentation. Knowing that is my weak point i will devote more time to the structure of my codes in the future.

  7. #7
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907

    Post

    I was going to say - Did you know that you only have to check up to the square root of the number?

    Going through every number that can be evenly divided into 36 without a remainder and its result...

    1 36
    2 18
    3 12
    4 9
    6 6
    After this (square root of 36) the terms swap over
    9 4
    12 3
    18 2
    36 1


    I noticed that Laserlight touched on this and has the best way of defining this in a for loop!
    Fact - Beethoven wrote his first symphony in C

  8. #8
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    Just with pseudo code, what you want to do is something like this...
    Code:
    print title/intro
    
    do
    {
      enter number (prompt/get input)
    
      if number is not a prime
        print factors and message saying not a prime
      else
        print single factor and tell user that it is a prime
    
      Ask user if they would like to go again
    
      Scan for response (1 for yes, 0 for no)
    
    }
    while response == 1
    What you are doing is recreating the first part of the code instead of looping back to the start of your programme.
    Fact - Beethoven wrote his first symphony in C

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. small code issue - please tell me what I'm doing wrong.
    By MonkeyTennis in forum C Programming
    Replies: 3
    Last Post: 08-14-2015, 12:36 PM
  2. Need help with a small issue...
    By yoyo2004 in forum C Programming
    Replies: 5
    Last Post: 09-01-2007, 03:56 AM
  3. Small Logic Issue
    By Flakster in forum C++ Programming
    Replies: 5
    Last Post: 05-29-2006, 02:40 PM
  4. A small issue.
    By Overwhelm in forum C++ Programming
    Replies: 35
    Last Post: 01-18-2005, 05:17 PM
  5. Small issue with system()
    By Metal Man in forum C++ Programming
    Replies: 8
    Last Post: 10-21-2003, 01:33 PM

Tags for this Thread