Click here to Skip to main content
15,922,512 members
Please Sign up or sign in to vote.
1.00/5 (4 votes)
See more:
1. Output should have 20 in nombers
2. program should contain only one loop or condition or ternary operator or anything
3. shouldn't initialize any number available in the series except 1
4. should not tested any nos available in the series (like i<10, i<=5)

[Update]
I wrote and tried many times as like below
C++
#include <stdio.h>
#include <stdlib.h>

int main()
{
    static int i,j;
    printf("%d",i);
    for(i=1,j=1;i<11,j<(1000/100);i++,j++)
    {
        printf("%d",i);

        printf("%d",10-j);
    }
    return 0;
}
Posted
Updated 15-Dec-17 9:12am
v3
Comments
Krunal Rohit 20-Oct-15 4:50am    
Homework!

-KR
Anupkumar_vj 21-Oct-15 13:30pm    
Its was asked in job interview, finding solution but not getting. I want answer for next some other interview
Mohibur Rashid 20-Oct-15 18:02pm    
Have you learned for loop yet?
Anupkumar_vj 21-Oct-15 13:30pm    
Yea. I learnt for loop. But only one loop should be used
Philippe Mori 21-Oct-15 13:22pm    
You cannot use comma operator to check 2 conditions. If i and j are always equal, there is no point to have 2 variables. You cannot have embedded loop using a single for... Well, you can simulate in using modulo and division operators...

We do not do your homework: it is set for a reason. It is there so that you think about what you have been told, and try to understand it. It is also there so that your tutor can identify areas where you are weak, and focus more attention on remedial action.

Try it yourself, you may find it is not as difficult as you think!

If you meet a specific problem, then please ask about that and we will do our best to help. But we aren't going to do it all for you!

So start by doing it manually, and work out how you do it.
Then you can look at translating that into code.
 
Share this answer
 
Comments
Patrice T 20-Oct-15 5:39am    
+5
1) Read your course carefully.
2) See how it is related to your exercise.
3) Write down on a paper how you are going to do things in peseudo-code.
4) Open your IDE, or a text editor if you prefer.
5) Begin to code according to point 3.
6) At some point, you may experience difficulty, you may then come back here ans ask a specific question on a specific issue.
 
Share this answer
 
Comments
Patrice T 20-Oct-15 5:39am    
+5
Your code is confused !

Here is a first correction which gives you the first part of the wanted result.
C++
#include <stdio.h>
#include <stdlib.h>

int main() {
    static int i;
    for(i=0; i<11; i++) {
        printf("%d",i);
    }
    // Here is the place to make the second part of what you want
    return 0;
}

I let you complete the code for the end of the result.

Nota: it is a good habit to always initialise all your variables. you see that my code don't use uninitialised variables contrary to your code.

[Update]
Ok the piece of code do only half of the job. Here is a lead to solution:
Actual piece of code use f(x)= x
In text below:
x      -5 -4 -3 -2 -1  0  1  2  3  4  5
f(x)=x -5 -4 -3 -2 -1  0  1  2  3  4  5
?       5  4  3  2  1  0  1  2  3  4  5
?      -5 -4 -3 -2 -1  0 -1 -2 -3 -4 -5
?       5  6  7  8  9 10  9  8  7  6  5

line 1 is values of x
line 2 is actual f(x)
which mathematical function gives results of line 3 ?
What change gives results of line 4 ?
What change gives results of line 5 ?
 
Share this answer
 
v4
Comments
Anupkumar_vj 21-Oct-15 13:34pm    
There I can't add another loop bcz it violates specified rule.
Patrice T 21-Oct-15 14:52pm    
My update is a lead to solution.
Anupkumar_vj 22-Oct-15 4:04am    
You have given slight mathematical idea..
I used my own idea like this
#include <stdio.h>
#include <stdlib.h>

int main()
{
int i,j;
for(i=1,j=100/50;i<21;i++,j=j+2)
{
printf("%d",((i-1)-(i-1)/10*(j-22)));
}
return 0;
}
Will it satisfy rules ? yes, its giving output in codeblock comipler..
Patrice T 22-Oct-15 14:05pm    
You are using the integer division trick to cancel a part of the formula.
Matt T Heffron 22-Oct-15 12:03pm    
+5
what about this?

C++
int recurPrint(int n){
    printf("%d",(9)-n);
    if(n){                   /*the only condition*/
        recurPrint(n-1);
    }
    printf("%d",(10)-n);
}
int main(){
    recurPrint(9);            /*the only initialization*/
    printf("\n");
}
 
Share this answer
 
v3
Comments
Patrice T 21-Oct-15 2:16am    
Just the loop is missing
Anupkumar_vj 21-Oct-15 13:37pm    
I think printf returns 21 but output should have 20 in numbers.
Patrice T 21-Oct-15 14:54pm    
See result as 0 1 2 3 4 5 6 7 8 9 10 9 8 7 6 5 4 3 2 1
Anupkumar_vj 22-Oct-15 4:05am    
You have given slight mathematical idea..
I used my own idea like this
#include <stdio.h>
#include <stdlib.h>

int main()
{
int i,j;
for(i=1,j=100/50;i<21;i++,j=j+2)
{
printf("%d",((i-1)-(i-1)/10*(j-22)));
}
return 0;
}
Will it satisfy rules ? yes, its giving output in codeblock comipler..
//I used my own idea and did like this

C++
#include <stdio.h>
#include <stdlib.h>

int main()
{
    int i,j;
    for(i=1,j=100/50;i<21;i++,j=j+2)
    {
     printf("%d",((i-1)-(i-1)/10*(j-22)));
    }
    return 0;
}

Will it satisfy rules ? yes, its giving output in codeblock comipler..
 
Share this answer
 
v2
Comments
Patrice T 22-Oct-15 15:17pm    
You can replace j=100/50 with j=2
the loop can be simplified this way
for(i=0,j=2;i<20;i++,j=j+2) {
printf("%d",((i)-(i)/10*(j-22)));
}

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900