Request for help

G

gpy5504

1. Write a program which prompts the user to enter an integer between
1 and 9.
Based on the inputted number, the program outputs a triangular textual
shape using digits
(see below).
Sample Execution:
Enter N: 5
12345
1234
123
12
1

2.1. The Fibonacci sequence is a sequence of numbers beginning
1, 1, 2, 3, 5, 8, 13, 21, ...
The first two elements are defined to be 1. Each of the other elements
is the sum of its
two predecessors. Write a program that takes a value for N in the
range 1 to 30 and
displays the first N elements of the Fibonacci sequence.
Sample Execution:
Enter N: 9
Fibonacci sequence: 1 1 2 3 5 8 13 21 34

2. An integer P is called a prime number if the only positive integers
that divide P are
1 and P itself. Write a program which inputs a number N between 2 and
100 and then
loops through integers 2 to N and displays only those numbers which
are prime.
Sample Execution:
Enter N: 15
2 3 5 7 11 13
 
I

Ian Collins

1. Write a program which prompts the user to enter an integer between
1 and 9.
Based on the inputted number, the program outputs a triangular textual
shape using digits
(see below).
Sample Execution:
Enter N: 5
12345
1234
123
12
1

2.1. The Fibonacci sequence is a sequence of numbers beginning
1, 1, 2, 3, 5, 8, 13, 21, ...
The first two elements are defined to be 1. Each of the other elements
is the sum of its
two predecessors. Write a program that takes a value for N in the
range 1 to 30 and
displays the first N elements of the Fibonacci sequence.
Sample Execution:
Enter N: 9
Fibonacci sequence: 1 1 2 3 5 8 13 21 34

2. An integer P is called a prime number if the only positive integers
that divide P are
1 and P itself. Write a program which inputs a number N between 2 and
100 and then
loops through integers 2 to N and displays only those numbers which
are prime.
Sample Execution:
Enter N: 15
2 3 5 7 11 13

Where's you code?
 
G

gpy5504

i need to write out the program by myself.
for question 3. i m writting the system as :
if (N_fibonacci == 1 )
current = previous1;

else if (N_fibonacci ==1)
current = previous2;

else {
counter= 3;
while (counter <= N fibonacci)
{
current = previous1 + previous2
previous1 = previous2
previous2 = current;
counter ++;
}

but the answer that program gave me is 1 3 4 5 1 5 3 7 4 and not the
answer that i want which is Fibonacci sequence: 1 1 2 3 5 8 13 21 34.
May i know what are the program.
and the other two questions i do not have any ideas to do the
programming ..
 
N

Nick Keighley

1. Write a program which prompts the user to enter an integer between
1 and 9.
Based on the inputted number, the program outputs a triangular textual
shape using digits
(see below).
Sample Execution:
Enter N: 5
12345
1234
123
12
1

pseudo code for the problem

(define (print-row n)
(let loop ((i 1))
(if (not (zero? i))
(begin
(display i)
(loop (- i 1)) )))
(print #\newline) )


(define (print-triangle n)
(if (not (zero? n))
(print-row n)
(print-triangle (- n 1)) ))
 
N

Nick Keighley

i need to write out the program by myself.

woop-i-doop what will the universities think of next!

When posting code to usenet (ie. here), post a
1. short
2. complete
3. compilable (unless you are having compilation problems)
program that illustrates your problem. You have not posted a complete
program and what you have posted is syntactically incorrect.
for question 3. i m writting the system as :
if (N_fibonacci == 1 )
current = previous1;

else if (N_fibonacci ==1)
current = previous2;

else {
counter= 3;
while (counter <= N fibonacci)
{
current = previous1 + previous2
previous1 = previous2
previous2 = current;
counter ++;

}

the layout is hard to follow. better (but still wrong) would have been
this:-

int main ()
{
if (N_fibonacci == 1 )
current = previous1;
else if (N_fibonacci == 1)
current = previous2;
else
{
counter = 3;
while (counter <= N fibonacci)
{
current = previous1 + previous2;
previous1 = previous2;
previous2 = current;
counter ++;
}
}
}

there is no output, none of the variables are defined and there is no
loop. You test if N_fibonacci is equal to 1 twice.

Get a piece of paper and draw 5 colums write "N_fibonacci" at the top
of the first column "previous1" at the head of the second column etc.
Put a question mark under each variable to show they are unitialised.
Then go through the program one line at time and update the columns as
appropriate. Ensure you and the progarm get the same answer. Put extra
printf(0s in to see values as they change. This should give you
insight.

Come back here if you still have problems. And don't post program
fragments again!
but the answer that program gave me is 1 3 4 5 1 5 3 7 4 and not the
answer that i want which is Fibonacci sequence: 1 1 2 3 5 8 13 21 34.
May i know what are the program.
and the other two questions i do not have any ideas to do  the
programming ..

write a progarm to print the numbers 1 to 5
 
N

Nick Keighley

pseudo code for the problem

(define (print-row n)
    (let loop ((i 1))
        (if (not (zero? i))
            (begin
                (display i)
                (loop (- i 1)) )))
    (print #\newline) )

(define (print-triangle n)
    (if (not (zero? n))
        (print-row n)
        (print-triangle (- n 1)) ))

a version that actuallyworked wouldlook more like this:-

(define (print-row n)
(let loop ((i 1))
(if (<= i n)
(begin
(display i)
(loop (+ i 1)) )))
(display #\newline) )

(define (print-triangle n)
(if (not (zero? n))
(begin
(print-row n)
(print-triangle (- n 1)) )))
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,479
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top