1. Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.
    Dismiss Notice

how to print the negative number of any number

Discussion in 'Microcontrollers' started by Parth86, Aug 19, 2017.

  1. Parth86

    Parth86 Member

    Joined:
    Dec 25, 2011
    Messages:
    515
    Likes:
    5
    Hi all
    1. How to write c program that will print the negative negative of any number.
    2. how to write c program that will print the negative of any number that is greater than four.

    I wrote this c program to print number o to 10 number.
    Code (c):


    #include <stdio.h>
    int main()
    {
       int i = 0;

       while(i <=10)

       {

          printf("Value is %d\n", i);

          i++;

       }
       return 0;
    }
     
    how to write both programs ?
     
    Last edited: Aug 19, 2017
  2. alec_t

    alec_t Well-Known Member Most Helpful Member

    Joined:
    Jul 10, 2011
    Messages:
    9,255
    Likes:
    1,218
    Location:
    Cardiff, Wales
    Is this Homework?
     
  3. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,164
    Likes:
    910
    Location:
    Rochdale UK
    It's the same but you decrement the value..

    Just ensure the variable is signed..
    Code (c):

    #include <stdio.h>
    int main()
    {
       signed int i = 0;

       while(i >= -10)

       {

         printf("Value is %d\n", i);

          i--;

       }
       return 0;
    }
     
     
     
    Last edited: Aug 19, 2017
  4. dave

    Dave New Member

    Joined:
    Jan 12, 1997
    Messages:
    -
    Likes:
    0


     
  5. Parth86

    Parth86 Member

    Joined:
    Dec 25, 2011
    Messages:
    515
    Likes:
    5

    No. its just for practice
    my compiler showing following errors
    hello.c: In function 'main':
    hello.c:10:14: error: expected expression before '%' token
    printf( %d\n", i);
    ^
    hello.c:10:14: error: stray '\' in program
    hello.c:10:18: warning: missing terminating " character
    printf( %d\n", i);
    ^
    hello.c:10:14: error: missing terminating " character
    printf( %d\n", i);
    ^
    hello.c:14:4: error: expected ';' before '}' token
    }
    ^
     
  6. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,164
    Likes:
    910
    Location:
    Rochdale UK
    Just edited the code... I haven't tested this... It was just to show you!! The while(i >= -10); needs to change aswell..
    Code (c):

    #include <stdio.h>
    int main()
    {
       signed int i = 0;

       while(i >= -10)

       {

         printf("Value is %d\n", i);

          i--;

       }
       return 0;
    }

     
     
    • Like Like x 1
  7. Parth86

    Parth86 Member

    Joined:
    Dec 25, 2011
    Messages:
    515
    Likes:
    5
    ok. In first program I am able to print number from 0 to 10 numbers. Now I just want to apply if else condition in my first program. if the numbers is greater than five than print negative numbers and if the numbers is less than five print positive numbers. how to do it
     
  8. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,164
    Likes:
    910
    Location:
    Rochdale UK
    Code (c):

    #include <stdio.h>
    int main()
    {
       signed int i = 0;
       signed int printit;
       while(i <= 10)

       {
         if(i >= 5)
              printit = 0 - i;
         else
              printit =  i;
         printf("Value is %d\n", i);

          i--;

       }
       return 0;
    }

     
    something like this.... Again not tested..
     
  9. dougy83

    dougy83 Well-Known Member

    Joined:
    May 18, 2008
    Messages:
    2,672
    Likes:
    215
    Location:
    Brisbane, Australia
    Maybe printf("Value is %d\n", i); should be printf("Value is %d\n", printit);
     
  10. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,164
    Likes:
    910
    Location:
    Rochdale UK
    Good call!! Good job someone is awake!
     
  11. ci139

    ci139 Active Member

    Joined:
    Apr 12, 2012
    Messages:
    402
    Likes:
    34
    i hope it's not of what i'm thinking you think


    http://www.cplusplus.com/reference/cstdio/printf/
     
  12. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,164
    Likes:
    910
    Location:
    Rochdale UK
    Start again.......

    Code (c):


    #include <stdio.h>
    int main()
    {
       signed int i = 0;
       signed int printit;
       while(i <= 10)

       {
         if(i >= 5)
              printit = 0 - i;
         else
              printit =  i;
         printf("Value is %d\n", printit);

          i++;

       }
       return 0;
    }
     
    Are we all happy now....... I did say untested!!
     
    • Like Like x 1
  13. ci139

    ci139 Active Member

    Joined:
    Apr 12, 2012
    Messages:
    402
    Likes:
    34
    what it has to do with anything - take it all easy - avoid contributing to this annoying "find the loony syndrome" possibly ghosting around in this as in several other net forums
    http://rextester.com/ARII95755 (not shure if this the right C v.)
     

Share This Page