• 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.

how to print the negative number of any number

Status
Not open for further replies.

Parth86

Member
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.
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:

alec_t

Well-Known Member
Most Helpful Member
Is this Homework?
 

Ian Rogers

User Extraordinaire
Forum Supporter
Most Helpful Member
It's the same but you decrement the value..

Just ensure the variable is signed..
C:
#include <stdio.h>
int main()
{
   signed int i = 0;

   while(i >= -10)

   {

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

      i--;

   }
   return 0;
}
 
Last edited:

Parth86

Member
Is this Homework?
No. its just for practice
It's the same but you decrement the value..

Just ensure the variable is signed..
C:
#include <stdio.h>
int main()
{
   signed int i = 0;

   while(i <= -10)

   {

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

      i--;

   }
   return 0;
}
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
}
^
 

Ian Rogers

User Extraordinaire
Forum Supporter
Most Helpful Member
Just edited the code... I haven't tested this... It was just to show you!! The while(i >= -10); needs to change aswell..
C:
#include <stdio.h>
int main()
{
   signed int i = 0;

   while(i >= -10)

   {

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

      i--;

   }
   return 0;
}
 

Parth86

Member
Just edited the code... I haven't tested this... It was just to show you!! The while(i >= -10); needs to change aswell..
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
 

Ian Rogers

User Extraordinaire
Forum Supporter
Most Helpful Member
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..
 

dougy83

Well-Known Member
C:
...
     if(i >= 5)
          printit = 0 - i;
     else
          printit =  i;
     printf("Value is %d\n", i);
...
something like this.... Again not tested..
Maybe printf("Value is %d\n", i); should be printf("Value is %d\n", printit);
 

Ian Rogers

User Extraordinaire
Forum Supporter
Most Helpful Member
Start again.......

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!!
 
Status
Not open for further replies.

EE World Online Articles

Loading
Top