 |
sample program for:C Language |
on the discussion today I will give an example of a simple program using a command (for).
The first example
#include <stdio.h>
#include <stdlib.h>
int main(void){
int x;
for(x=1;x<=10;x++){
printf("%d\n",x);
}
return 0;
the role of the code above is to command increments that start variable "x = 1" and the variable is less than or equal to 10 and a code "x ++" is used to add "1 + x" in which the value of "x is 1" and continue until the result is less than or equal to 10
for(x=1;x<=10;x++){
printf("%d\n",x);
X=1 : Used to make the base in the statement (
for).
X<=10 : Informing that the final value of the variable-value
<= 10
X++ : Perform increment on control variable
printf("
%d\n",x) ; displaying the results of the statement for (printf should be inside the curly braces for statement. if not then the result will be different)
the second example
the second program we will try to show the numbers from
"1" to
"10" but only even numbers are out.
#include <stdio.h>
#include <stdlib.h>
int main(void){
long int x;
for(x=1;x<=10;x++){
x = x + 1;
printf("%d\n",x);
}
return 0;
}
x = x + 1;
For example if out
"X = 1" it will be
"X = X + 1". So when read
"1 = 1 + 1" program becomes
"1 = 2" and
"2" will be out in the program. and then number
"2" will be missed because it had been executed by the program. so directly proceed to number
"3 = 3 + 1" and so on until
"10".
The third example
made the statement program (for) to bring up an odd number
#include <stdio.h>
#include <stdlib.h>
int main(void){
int x;
for(x=0;x<=9;x++){
x=x+1;
printf("%d\n",x);
}
return 0;
}
The fourth example
displaying programs from
"100" down to number
"1"
#include <stdio.h>
#include <stdlib.h>
int main(void){
int x;
for(x=100;x>=1;x--){
printf("%d\n",x);
}
return 0;
}
The fifth example
changing the control variables from "x = 7 to x <= 77" by adding "7" at each increment. example "x = 7 and x + = 7" the same as "x = x + 7 so 7 = 7 + 7", which means that "7 = 14".
#include <stdio.h>
#include <stdlib.h>
int main(void){
int x;
int y;
for(x=7;x<=77;x+=7){
printf("%d\n",x);
}
return 0;
}
The sixth example
making the results into a coherent
#include <stdio.h>
#include <stdlib.h>
int main(void){
int x;
for(x = 44; x >= 0;x-=11){
printf("%d\n",x);
}
return 0;
}