newbie here. if-else, while, loop. thanks!

Problem Statement:

Design a program that will allow the user to select from the following :
[1] Math algorithms
[2] Figures
[3] Exit

[1]MATH ALGORITHMS
a. Euclidean Algorithm
In number theory, the Euclidean algorithm is used for finding the greatest common divisor for two integers a and b (not zero) is based on the following fact:
If r is the remainder when a is divided by b, then gcd (a,b) = gcd (b,r).
See the following Pseudocode:
Euclid(a,b) {
while (b not 0) {
interchange(a,b)
b := b mod a
}
return(a)
}

b. The Ulam Sequence
A mathematician named Ulam proposed generating a sequence of numbers from any positive integer n (n > 0) as follows:
• If n is 1, stop.
• If n is even, the next number is n/2.
• If n is odd, the next number is 3*n + 1.
• Continue with this process until reaching 1.
Ex: an input of 3 gives ->10 ->5 -> 16 ->8 -> 4 -> 2 ->1

c. Fibonacci Series
Fibonacci numbers was invented by Leonardo of Pisa (Fibonacci). He defined fibonacci numbers as a growing population of immortal rabbits. Series of Fibonacci numbers are: 1,1,2,3,5,8,13,21,...
The Fibonacci sequence is 0,1,1,2,3,5,8,13,… where the first two terms are 0 and 1, and each term thereafter is the sum of the two preceding terms; that is Fib[n]=Fib[n-1] + Fib[n-2]. Display the Fibonacci series: example, if n=6, the program should display the value 0,1,1,2,3,5.

d. Fermat Little test
If 2^N modulo N = 2 then N has a high probability to be a prime number.
Ex:
let N=11 (we know that 11 is a prime).
2^11 mod 11 = 2048 mod 11 = 2, then N has a high probability to be a prime number.
Using this information, design a program to list all prime numbers that are less than 100.

[2]FIGURES/SHAPES
a. Print a rectangle. Read a height and width and print a rectangle with that height and width. This example output has height 3 and width 7.
*******
*******
*******

b. Print a triangle. Read a number and print a triangle of that size. This example is of size 6.
*
**
***
****
*****
******

c. Print a rectangle outline. Read a height and width and print a rectangle that looks like the one below. This example has a height of 5 and width of 7. You might choose one of two approaches to this problem.
First Approach, Break it down into three problems: a line of stars; lines consisting of a star, blanks, and a star; and again a line of stars.
Second Approach, At each position write either a star or blank. Use an if statement to test for first or last row of first or last column, in which case a star is printed, otherwise print a blank.

*******
* *
* *
* *
*******
In this figure, i must make it rectanglar outline.
just an outline, without the * inside.


I'm new here. I'm just a student so do not expect something good. I really need some help from you guys. Thanks in advance!

Please, I need only simple codes. like if-else, looping, switch. please do not add something advance. make it for beginners. thanks again!

So far, this is my effort. I don't understand why an error says illegal if without matching if.


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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include <iostream>
using namespace std;

int main ()

{
	int ans, num, a, b, c, d;
	int h = 0;
	int w = 0;
	char ans2;

	cout << "[1] Math algorithms " << endl;
	cout << "[2] Figures " << endl;
	cout << "[3] Exit " << endl;
	cout << "Enter your choice. ";
		cin >> ans;
	cout << endl;

	if (ans == '2')
		cout << endl;
		cout << " [a] Print a Rectangle " << endl;
		cout << " [b] Print a Triangle " << endl;
		cout << " [c] Print a Rectangle Outline " << endl;
		cout << " Enter your choice. ";
			cin >> ans2;
		{
			if (ans2 == 'a')
				cout << endl;
				cout << "   Print a Rectangle" << endl;
				cout << "    Enter Height. ";
					cin >> h;
				cout << "    Enter Width. ";
					cin >> w;
					{
						for (c=1; c<=h; c++)
						{
							for (d=1; d<=w; d++)
							{
								cout << "*";
							}
									cout << endl;
						}
					}
					
			else if (ans2 == 'b')
				cout << endl;
				cout << "   Print a Triangle" << endl;
				cout << "    Enter a Number. ";
					cin >> num;
				for (a = 1; a <= num; a++)
				{
					for (b = 1; b <= a; b++)
					{
						cout << "*";
					}
					
					cout << endl;

				}
		}
	}

		cout << endl;
	
	return 0;
}


Last edited on
you have to write the '{' directly after the if statement to which the following block belongs
like this..
if (ans == '2') {
or this
1
2
if (ans == '2')
{
you just must not write an expression in between the if clause and the {
still says illegal
The problem is what they're describing, only you have the problem in multiple places... not just that one place.

Remember that for if/else/while/for/etc -- multi-line code has to be inside of braces.

Look on line 27 in your code:

1
2
3
			if (ans2 == 'a')
				cout << endl;
				cout << "   Print a Rectangle" << endl;


since you don't have braces... only the cout << endl; line is part of the if. The folling "Print a rectangle" line will be printed whether or not the above if condition is true. It has nothing to do with the if.

Then on line 45, you have an else -- but there isn't any if before it. That's why you're getting the error.

To fix, you have to put everything you want in the if in {braces}
got it disch! thanks.

any idea for my program? for the math algorithms? i only made the figures. :D
why is it when you select the figures then you choose the rectangle outline it shows "Press any key to continue"??

thanks
Topic archived. No new replies allowed.