You are viewing our Forum Archives. To view or take place in current topics click here.
Need some help C Programming
Posted:

Need some help C ProgrammingPosted:

Almac14
  • Challenger
Status: Offline
Joined: Mar 04, 201212Year Member
Posts: 187
Reputation Power: 7
Status: Offline
Joined: Mar 04, 201212Year Member
Posts: 187
Reputation Power: 7
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
  • Spooky Poster
Status: Offline
Joined: Aug 19, 201013Year Member
Posts: 5,238
Reputation Power: 532
Status: Offline
Joined: Aug 19, 201013Year Member
Posts: 5,238
Reputation Power: 532
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-
  • Blind Luck
Status: Offline
Joined: Nov 02, 201310Year Member
Posts: 4,340
Reputation Power: 1865
Status: Offline
Joined: Nov 02, 201310Year Member
Posts: 4,340
Reputation Power: 1865
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
  • 2 Million
Status: Offline
Joined: Oct 09, 201013Year Member
Posts: 2,358
Reputation Power: 106
Status: Offline
Joined: Oct 09, 201013Year Member
Posts: 2,358
Reputation Power: 106
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
Jump to:
You are viewing our Forum Archives. To view or take place in current topics click here.