This is a true classic. I covered "Hello World!" example in this lesson, which is the one everybody encounters when learning to program – It's just sooner or later. Well, in my lessons –later-. Most of other tutorials start with this example, but I think I'm on the right path of introducing you to it only now. Not much of a new stuff covered here. More like repeating and affirmation.

**Loops**

**Example:**

What is the printed result of following program block?

**Result:**

Hello world 3.x!

Hello world 3.x!

...

...

… and so on for infinite number of times. Even after value 3 is reached, orders under i==3 are executed. Because of “continue” command,**i **doesn’t increase, but the program branches on conditional phrase (i < 5).

**Example:**

Write your own program block that prints multiplying table of numbers up to 100.

By adding single line of code, we accomplish that new table is made from even numbers only:

Same thing:

**Example:**

Write your own program block that prints first N Fibonacci numbers. N is given by keyboard. Algorithm to calculate Fibonacci Numbers:

Fibonacci(n) = Fibonacci(n-1) + Fibonacci(n-2)

Fibonacci(0) = Fibonacci(1) = 1

1, 1, 2, 3, 5, 8, 13, 21,...

**Example:**

Write your own program which reads 2 real numbers and numeric operation executed above them (+, -, /, *) (all given by keyboard). If the given operation is different than allowed, program asks for new operation input.

Technorati Tags: Signed, Unsigned, Array, Loop, Index, printf, Execute

What is the printed result of following program block?

i = 1;

while (i < 5) {

if (i==3) {

printf (“\n Hello world %d.x!”, i);

continue;

} else if (i==4) {

printf (“\n Goodbye %d.x!”, i);

continue;

}

i++;

}

Hello world 3.x!

Hello world 3.x!

...

...

… and so on for infinite number of times. Even after value 3 is reached, orders under i==3 are executed. Because of “continue” command,

Write your own program block that prints multiplying table of numbers up to 100.

/*1*/ int main(void) {

/*2*/ int i,j;

/*3*/

/*4*/ for (i = 1; i <= 10; ++i) {

/*5*/ for (j = 1; j <= 10; ++j)

/*6*/ printf("%3d",i*j);

/*7*/ printf("\n");

/*8*/ }

/*9*/}

By adding single line of code, we accomplish that new table is made from even numbers only:

/*6*/ if ( (i%2!=0) && (j%2!=0) ) continue; // both odd numbers

Same thing:

/*6*/ if ( (i%2==0) || (j%2==0) ) // at least one number is even

Write your own program block that prints first N Fibonacci numbers. N is given by keyboard. Algorithm to calculate Fibonacci Numbers:

Fibonacci(n) = Fibonacci(n-1) + Fibonacci(n-2)

Fibonacci(0) = Fibonacci(1) = 1

1, 1, 2, 3, 5, 8, 13, 21,...

#include <stdio.h>

int main () {

int N, i, f0 = 1, f1 = 1,f = 1;

printf ("\n Input amount of Fibonacci Numbers (N) : \n");

scanf ("%d",&N);

for (i = 0; i <= N; i++) {

if (i > 1) {

f = f1 + f0;

f0 = f1;

f1 = f;

}

printf ("Fibonnaci (%d) = %d \n", i , f);

}

return 0;

}

Write your own program which reads 2 real numbers and numeric operation executed above them (+, -, /, *) (all given by keyboard). If the given operation is different than allowed, program asks for new operation input.

#include <stdio.h>

int main(void) {

float a,b, result;

char operation;

int repeatOperationInput = 1;

printf("Input two numbers: ");

scanf("%f, %f", &a, &b);

do {

printf("Input operation: ");

operation = getche();

printf("\n");

repeatOperationInput = 0;

switch(operation) {

case '+': result = a + b; break;

case '-': result = a - b; break;

case '*': result = a * b; break;

case '/':

if (b == 0) {

printf("Dividing by 0 isn’t allowed.\n");

}

else {

result = a / b;

}

break;

default:

repeatOperationInput = 1;

}

} while (repeatOperationInput);

printf("%f %c %f = %f\n", a, operation, b, result);

// What if b == 0?

}

Technorati Tags: Signed, Unsigned, Array, Loop, Index, printf, Execute

This Website is optimized for Firefox. Users browsing with Internet Explorer may encounter problems while viewing pages.

- Lesson 1
- Lesson 2
- Lesson 3
- Lesson 4
- Lesson 5
- Lesson 6
- Lesson 7
- Lesson 8
- Lesson 9
- Lesson 10
- Lesson 11
- Lesson 12
- Lesson 13
- Lesson 14
- Lesson 15
- Lesson 16
- Lesson 17
- Lesson 18
- Lesson 19

- Binary To Decimal
- Decimal To Binary
- Decimal To Hexadecimal
- Decimal To Decimal
- Hexadecimal To Decimal
- Hex, Octal, Binary

- Lesson 12: Switch-Case, Break; and Continue;
- Lesson 11: Infinite and Finite Loops
- Lesson 10: Program Iterations
- Lesson 9: Two-sided and Multi-sided Selections
- Lesson 8: Conditional and Logical Operators
- Lesson 7: Assigning, Comparison, IF Conditions
- Lesson 6: ASCII code and character variables
- Lesson 5: Operators and Integer operations
- Lesson 4: Casting data types
- Lesson 3: Data types in Registry

Daily Lessons for programming in Visual Studio, using C code.

I tried to test the code for the last example but got this:

POLINK: error: Unresolved external symbol '_getche'.

POLINK: fatal error: 1 unresolved external(s).