Need some help C ProgrammingPosted:

Status: Offline
Joined: Mar 04, 20124 Year Member
Posts: 177
Reputation Power: 6
I basically just need help with putting my code into a loop so I can shorten it and make it alot easier for myself next time I need to do this. If anyone can help me or point me in the best direction, it would be appreciative.

if ((num < 0) || (num > 32767))
         {
           printf("Outside of Range\n");
         }
     // Value is inside of range
     else
         {
         
              // line 1 below
               
              num4 = num / 10000;
              digit4 = num4 % 10;
           printf("%d   ", digit4);
 
              num3 = num / 1000;
              digit3 = num3 % 10;
           printf("%d   ", digit3);
           
              num2 = num / 100;
              digit2 = num2 % 10;
           printf("%d   ", digit2);
 
              num1 = num / 10;
              digit1 = num1 % 10;
           printf("%d   ", digit1);
           
              num0 = num / 1;
              digit0 = num0 % 10;
           printf("%d   ", digit0);
           
           // line 2 below
 
              num3 = num / 1000;
              digit3 = num3 % 10;
           printf("\n%d   ", digit3);
           
              num2 = num / 100;
              digit2 = num2 % 10;
           printf("%d   ", digit2);
 
              num1 = num / 10;
              digit1 = num1 % 10;
           printf("%d   ", digit1);
           
              num0 = num / 1;
              digit0 = num0 % 10;
           printf("%d   ", digit0);
           
           // line 3 below
           
           num2 = num / 100;
              digit2 = num2 % 10;
           printf("\n%d   ", digit2);
 
              num1 = num / 10;
              digit1 = num1 % 10;
           printf("%d   ", digit1);
           
              num0 = num / 1;
              digit0 = num0 % 10;
           printf("%d   ", digit0);
           
           // line 4 below
           
           num1 = num / 10;
              digit1 = num1 % 10;
           printf("\n%d   ", digit1);
           
              num0 = num / 1;
              digit0 = num0 % 10;
           printf("%d   ", digit0);
           
           // line 5 below
                                         
           num0 = num / 1;
              digit0 = num0 % 10;
           printf("\n%d   \n", digit0);
           
           }
#2. Posted:
-Deano
  • Rated Awesome
Status: Offline
Joined: Aug 19, 20106 Year Member
Posts: 4,439
Reputation Power: 317
Motto: Thanks Jay for the gold! | PCMR
I don't really understand what you are asking for.

To loop this entire block, based on the value of 'num', do the following.


while (num >= 0 && num <= 32767) {
  // your code.
}
#3. Posted:
Gavin-
  • Gold Member
Status: Offline
Joined: Nov 02, 20132 Year Member
Posts: 3,046
Reputation Power: 328
Finding it hard to understand what you mean but if the question requires a loop then use a while loop like so , If you have the question give it to me in a reply.



while (num >= 0 && num <= 32767) {

System.out.println(" Your will be charged 5% interest");
}

----------------------------------------------------------------------------------------------
If you are using if/elseif/else

if(num > 0 && num <= 32767)

{
System.out.println(" Your will be charged 5% interest");


else
System.out.println(" Your will be charged 10% interest");
#4. Posted:
Xaldin
  • 1K Rainmaker
Status: Offline
Joined: Oct 09, 20105 Year Member
Posts: 1,955
Reputation Power: 84
If I understand for this to be a short loop you'd have to put your variables into something like an array. instead of num1 num2 num3 just have Numbers[i] and use a counter
Users browsing this topic: None
Jump to:


RECENT POSTS

HOT TOPICS