only 1 loop will be used, for loop..

Feb 22, 2016 at 1:39am
my code sir is
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include<stdio.h>
#include<conio.h>
 void main(){
 clrscr();
 int count=1;
 for(int i=1;i<=8;i++){
 if(count<=8){
 if(i==1){
 printf("%d",i);
 if(i==2);
 else{
 if(i%2==0){
 printf("%d",i); 
 count++;
 if(i==8){
 printf("%d",9-i);
}
}
}
}
}
getch();
}

but the output of that is 1.
what will i do so that the output of this problem is
1468
87654321


Please Help!!
Last edited on Feb 22, 2016 at 1:55am
Feb 22, 2016 at 5:24am
that code doesn't even compile (mismatched braces, main must return int)

1
2
3
4
#include <iostream>
int main(){
   std::cout << 1468 << '\n' << 87654321 << '\n';
}
Feb 22, 2016 at 12:11pm
is it cout==printf??? sir the problem here is loop...
Feb 22, 2016 at 12:24pm
The problem is on line 8. Everything after line 8 will only be executed if i==1.

Check your closing braces
Feb 23, 2016 at 5:33am
ahh so that's why.. so should i delete it or change ?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include<stdio.h>
#include<conio.h>
 void main(){
 clrscr();
 int count=1;
 for(int i=1;i<=8;i++){
 if(count<=8){
printf("%d",i);
 if(i==2);
 else{
 if(i%2==0){
 printf("%d",i); 
 count++;
 if(i==8){
 printf("%d",9-i);
}
}
}
}
}
getch();
}




so this will be the result?
ill try it sir thank you
Last edited on Feb 23, 2016 at 5:34am
Feb 23, 2016 at 8:52am
You think to complicated. Use one loop for the first output and a second for the second. Further more better indentation. See:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include<stdio.h>
#include<conio.h>
void main()
{
  clrscr();
  for(int i=1;i<=8;i++)
  {
    if(i==1) // Note: count<=8 is useless
      printf("%d",i);
    else if(i==2)
      ;
    else if(i%2==0)
      printf("%d",i); 
  }

  printf("\n"); 
  for(int i=1;i<=8;i++)
  {
    // Note: if(i==8){ is useless
    printf("%d",9-i);
  }

  getch();
}
Feb 23, 2016 at 12:41pm
but sir we only required only 1 loop will be used. so how im i supposed to do that.
Feb 23, 2016 at 1:21pm
we only required only 1 loop will be used.
Ok, then your if cascade makes a [bit] more sense.

The trick is doubling the end value of the loop:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include<stdio.h>
 int main(){
 int count=8;
 for(int i=1;i<=16;i++){ // Note: 16
 if(i<=8){
 if(i==1)
printf("%d",i);
 if(i==2);
 else{
 if(i%2==0){
 printf("%d",i); 
 }
 }
 }
 else
 {
 if(i==9)
 printf("\n");
 printf("%d",count);
 count--;
}
}
return 0;
}
Without proper indentation!
Feb 24, 2016 at 12:38pm
but sir, we only use this problem

 
for(int i=1;i<=8;i++)


no less no more :(( i tried to much but its to complicated
Feb 24, 2016 at 12:43pm
Is it allowed to use an array?
Feb 24, 2016 at 1:12pm
Please post the full problem. First you just said what the output should be. Then you said it should use a loop. Then you said the bounds of the loop are fixed.

And regarding the output, is it
1468
87654321

or
2468
87654321
The latter would make a whole lot more sense.
Feb 25, 2016 at 8:20am
sir i said at first that "This is my CODE" and i post it "the result is or the output of that is "1" then the big real problem here is that , we should only use 1loop and and the result is/ or the output:
1468
87654321

array is not allowed sir :( its to complicated ..
Please Help.
Feb 25, 2016 at 9:03am
sir i said at first that
Instead you should have posted the requirements given to you.

However. This is the solution:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include<stdio.h>
 int main(){
 int count=1;
 for(int i=1;i<=8;i++){
 if(count<=8){
 if(count==1)
printf("%d",count);
 if(count==2);
 else{
 if(count%2==0){
 printf("%d",count);
 }
 }
 i--; // Note
 }
 else
 {
 if(count==9)
 printf("\n");
 printf("%d",9-i);
}
 count++;
}
return 0;
}
Feb 25, 2016 at 11:35am
Feb 25, 2016 at 12:56pm
sir i said at first that "This is my CODE" and i post it

I understand that you posted your code. What I'm asking is for the actual problem, not the program. Something says you can't use an array. Something says you can't use 2 loops. Something says you must use one loop. Something says the loop must go from zero to 8. What is that something? Can you post it?
1
2
3
4
for(int i=1;i<=8;i++){
    std::cout << 1468 << '\n' << 87654321 << '\n';
    break;
}
Also, are you certain that the first number is 1468 and not 2468?
Feb 25, 2016 at 1:23pm
The problem sir is what should i do about this problem.. actually we only allowed to use only 1 loop "for loop" and array is not allowed and also the output is

1468
87654321

it makes me sick, we only allowed to use 1 loop "for loop" and "if" "else" only sir and the loop will start from 1 to 8 only..
Feb 25, 2016 at 1:34pm
And none of the provided solutions are according to your taste?

The problem sir is what should i do about this problem.. actually we only allowed to use only 1 loop "for loop" and array is not allowed and also the output is
If that are the requirements dhaydens solution does what you want.
Feb 25, 2016 at 1:44pm
hmmmm im just asking some knowledge about programming sir... my course is Geodetic Engineering and i cant relate the world of programming to my course.. its to hard for me. sorry sir. if the output of what you given to me is

1468
87654321.. im asking for another so that i can learn from you and from them.. thank you so much sir..
Last edited on Feb 25, 2016 at 1:45pm
Topic archived. No new replies allowed.