Thread: C program to check the number

  1. #1
    Registered User
    Join Date
    May 2017
    Posts
    129

    C program to check the number

    Hi
    what's wrong in program. I am getting four value but I want to print only true or false value so if number is equal than print true and not equal than print false
    Code:
    #include <stdio.h>
    int main(void)
    {
      int number = 2; 
      int i;
      for (i=1; i<5; i++)
     
      if (number == i )
      { 
        printf ("True" );
      }
    
      else 
      { 
         printf ("False");
       }
    
    return 0;
    }
    Result :FalseTrueFalseFalse

    I got it if I remove for loop than I get true or false value.
    actually I am looking for program where I have for conditions each condition has true and false value. first i want to check four condition and than I want to check either the condition is true or false. I know I have to use for loop and if else condition but I don't know how to write it
    Last edited by abhi143; 09-23-2017 at 01:27 AM. Reason: I got answer

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Number is equal to what? Here you compare it with numbers 1 to 4 inclusive, therefore getting four answers. Did you want to do something else?
    Devoted my life to programming...

  3. #3
    Banned
    Join Date
    Aug 2017
    Posts
    861
    using it for testing purposes only not user input, that is all it is going to do when it hits number = 2; it will spit out true whereas the rest of the time it will say false.

    Code:
    #include <stdio.h>
    int main(void)
    {
      int number = 2;
      int i;
    
      for (i=1; i<5; i++)
      {
         if (number == i )
         {
             printf ("True\n" );
         }
        else
        {
            printf ("False\n");
         }
      }
    
    return 0;
    }
    Code:
    userx@~/bin <> gcc -Wall checknum.c
    userx@~/bin <> ./a.out
    False
    True
    False
    False
    you can also put that into a function to rid yourself of a loop.
    Code:
    #include <stdio.h>
    
    // one way
    int Is_match(int a, int b)
    {
    
        return (a == b ? 1 : 0);
    }
    
    // the other way
    int is_it_match(int a, int b)
    {
     if ( a == b)
     return 1;
     else
     return 0;
    
    }
    
    int main(void)
    {
    
    // one way
      if ( Is_match(4,4) )
       printf("match\n");
       else
       printf("not Match\n");
    
    //the other way
     if (is_it_match(4,5) )
     printf("is a match\n");
     else
     printf("is not a match\n");
    
    
    return 0;
    }
    results
    Code:
    userx@~/bin <> gcc -Wall checknum.c
    userx@~/bin <> ./a.out
    match
    is not a match
    so actually what you are saying here is wrong,
    I want to check either the condition is true or false. I know I have to use for loop and if else condition but I don't know how to write it
    because you did already write it, using a loop and if else statement to check for true or false.

    Mod: Ok I think I see what you are saying, you want 4 test ran using 2 different conditions. 1 true 2 false

    use variables so the value change, not hard coded values. just made it up code, not tested.
    Code:
    #include <stdio.h>
    
    
    int main()
    {
    
    int a = 0, b = 0, c = 1;
    
    for ( a = 0;  a < 4 ;  a++)
    {
    
        if ( b == c )
        {
            printf("match\n");
           c++;
        }
        else
        {
            printf("not match\n");
            b++;
         }
    
     }
    return 0;
    }
    if you want/need to know the logic behind it post back and ask, so I can explain it to you so you will know.
    Last edited by userxbw; 09-23-2017 at 07:04 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 12-02-2016, 01:00 AM
  2. Program to check whether given number is prime or not?
    By avidrutham in forum C Programming
    Replies: 1
    Last Post: 05-26-2015, 04:13 AM
  3. Prime number check
    By password in forum C++ Programming
    Replies: 3
    Last Post: 06-25-2007, 10:30 AM
  4. how to check if the string is a number
    By lingz82 in forum C++ Programming
    Replies: 5
    Last Post: 09-19-2006, 11:50 AM
  5. How is to check prime number?
    By Unregistered in forum C Programming
    Replies: 7
    Last Post: 10-04-2001, 11:36 PM

Tags for this Thread