Click here to Skip to main content
15,880,405 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
1st line- 2 10 30 68 130
2nd line- 1 2 3 5 8 13 21 34 55
3rd line- 1 4 7 10 13 16 19 22 25

please only use for loop for this as my teacher gave a task :(


C++
#include<stdio.h>
#include<conio.h>
void main()
{
    int a,b,c;
    for(a=1;a<=25;a+=3)
    {	
        printf("\n\t3rd line:%d",a);
    }
    for(b=2;b<=55;b++)
    b+=3;
    c=b+10;
    {
        printf("\n\t\t\t:2nd:%d\n%d",b,c);
    }
    
getch();   

i just wrote this one , it gives 3rd line correctly but what about the 1st two lines they are'nt that simple.
Posted
Updated 3-Dec-12 3:28am
v4

C#
#include<stdio.h>
main()
{

    int a=1,b=1,i,c;


    printf("2nd line :");
    for(i=0;i<9;i++)
        {
        c=a;
        a=b;
        b=c+b;
        printf("%d ",a);
        }
    printf("\n");

        printf("1st line :");
    for(i=1;i<6;i++)
    {
        printf("%d ",(i*i*i)+i);

    }
    printf("\n");

    printf("3rd line :");
        for(a=1;a<=25;a+=3)
        {
             printf("%d ",a);
        }
        printf("\n");

}
 
Share this answer
 
Your teacher gave the task to you. At least make an effort to solve it.
 
Share this answer
 
Comments
jahanxb 3-Dec-12 8:48am    
@richard i know this is cheating but at least i should have something to get an idea . i am not a freaking genious :p
Richard MacCutchan 3-Dec-12 8:51am    
You don't need to be a genius to write a few simple loops. And if you do not even know that much then I suggest you go back and read your course notes.
jahanxb 3-Dec-12 8:55am    
right now i am doing that :p
To understand those series, there is one trick which works well: form the first differences (i.e. b-a, c-b, d-c, e-d...) and observe.

If you can't find a regularity, then repeat the process, forming the second differences, and so on.

This will allow you to figure out a formula or a computational procedure.

Last hint: start with the third.
 
Share this answer
 
v2
Comments
jahanxb 3-Dec-12 8:55am    
well i am gonna try it , but my teacher said no use of condtions and less number of variables
YvesDaoust 3-Dec-12 9:10am    
In what way does this contradict my suggestion ???

1 and 3 can be done with a single variable; 2 requires two of them.
Think about what you are trying to do in your loops.

  1. Set the start value to the first number in the sequence.
  2. Display the current value of the sequence.
  3. If current value equals last number, quit.
  4. Calculate the next number in the sequence.
  5. Go back to step 2 and repeat.
 
Share this answer
 
Comments
jahanxb 3-Dec-12 8:58am    
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
for(a=1;a<=25;a+=3)


{ printf("\n\t3rd line:%d",a);
}
for(b=2;b<=55;b++)
b+=3;
c=b+10;
{

printf("\n\t\t\t:2nd:%d\n%d",b,c);
}

getch();

i just wrote this one , it gives 3rd line correctly but what about the 1st two lines they are'nt that simple
Richard MacCutchan 3-Dec-12 9:21am    
It took me less than a minute with Google to find the answer to the line 1 problem. Or you could find a fellow student who understands mathematics and ask them (Hint: it's a simple polynomial).

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