Lesson 13: Hello World! - A Classic -


E-mail this post



Remember me (?)



All personal information that you provide here will be governed by the Privacy Policy of Blogger.com. More...



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?


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++;
}



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.


/*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) &&amp; (j%2!=0) ) continue; // both odd numbers

Same thing:

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





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,...



#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;
}





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.



#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:
, , , , , ,



1 Responses to “Lesson 13: Hello World! - A Classic -”

  1. Anonymous Anonymous 

    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).

Leave a Reply

      Convert to boldConvert to italicConvert to link

 


German Flag Spanish Flag French Flag Italian Flag Portuguese Flag Japanese Flag Korean Flag Chinese Flag British Flag


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


C++ Maniac



Learn C



Additional



#include



Learn Converting



Appendix


Links


Previous posts




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