programming ppt files (final)
DESCRIPTION
final requirement FNDPRGTRANSCRIPT
FUNDAMENTALS OF PROGRAMMING
In this presentation, the switch case and looping in C language will be discussed.
• Sample codes wil be given• Screenshots of output will be shown
• Steps will be explained
http://eglobiotraining.com/
http://eglobiotraining.com/
How to Construct a Basic FOR Loop in the C Language
The core of most modern programming, including those in the C language, is the loop. A loop gives a program the ability to repeat a group of statements, sometimes for a given count or duration, or, often, until a certain condition is met. The C language gives you many ways to create loops in your code, but the most common is the for loop.
http://eglobiotraining.com/
LOOPS: To execute a set of instructions repeatedly until a particular condition is being satisfied.Three types of looping statements in programming are : 1) For Loop2) While Loop3) Do while Loop
For Loop:In for looping statement allows a number of lines represent until the condition is satisfied WHILE LOOP:
In While looping statement allows a number of lines represent until the condition is satisfied
DO WHILE LOOP:In DO WHILE LOOP first execute the statements then it checks the condition.
http://eglobiotraining.com/
CONCLUSION:
In FOR LOOP:No need to initialize variable before the LOOP
In WHILE LOOP:To initialize the variable before the LOOPIncrement/decrement the variable within the LOOP
In DO WHILE LOOP:Once it execute If the condition is TRUE/FALSE.
http://eglobiotraining.com/
PROGRAM # 1Output screenshot :
http://eglobiotraining.com/
#include <stdio.h>int main(){ int c; for(c=0;c<3;c=c+1) { puts("I shall refrain from calling my friends names."); } getchar(); return(0);}
C Language Programming Code :
http://eglobiotraining.com/
In C Language programming, the for keyword is followed by a set of parentheses. Inside the parentheses are three separate items that configure the loop.The c variable is already defined as an int (integer). It's used by the for loop to control how many times the loop — the statements belonging to for — is repeated. First comes the setup (c=0) : The variable c is assigned the value 0. The for statement does this first, before the loop is ever repeated, and then only once.Note that starting at 0 rather than 1 is a traditional C language thing. Zero is the "first" number. Get used to that.
Next comes the exit condition (c<3) : The loop repeats itself as long as the value of variable c is less than 3.
Finally, here's the "do this" part of the loop (c=c+1) : Each time the loop is repeated, the for statement executes this statement. It must be a real C language statement, one that you hope somehow manipulates the variable that's set up in the first step. Here, the value of variable c is increased, or incremented, by one.
Programming Functions :
http://eglobiotraining.com/
PROGRAM # 2Output screenshot :
http://eglobiotraining.com/
#include <stdio.h>
void box(int width, int height);
int main(){
int x, y; printf( "Please input the width of the box: " );scanf( "%d", &x );printf( "Please input the height of the box: " );scanf( "%d", &y );printf( "Draw...\n" );box(x,y); getchar();
}void box(int width, int height){ for (int y = 0; y < height; y++) {
C Language Programming Code (part 1):
http://eglobiotraining.com/
if (y > 0 && y < height - 1) { for (int x = 0; x < width; x++) {
if(x == 0)printf("0");else if( x == width - 1)printf("0\n");elseprintf(" ");
} } else { for (int x = 0; x < width; x++) {
if( x == width - 1) printf("0\n");else printf("0");
} } }
printf("\n");}
(part 2):
http://eglobiotraining.com/
void box(int width, int height); * this is a method that does anything
int x, y; *x is the width, y is the height
box(x,y); *function that will do the things below
printf(“ ");*this is the condition to leave empty space inside the box
printf("0");*this will fill "0"s in the whole row
printf("0\n");*if the width reached the end it will go to the next line with symbols of "\n"
Programming Functions
http://eglobiotraining.com/
PROGRAM # 3Output screenshot :
http://eglobiotraining.com/
#include <stdio.h>
void countdown(int time);int main(){
int time; printf( "Please input the time to perform countdown: " );scanf( "%d", &time );countdown(time); getchar();
}
void countdown(int time){
if(time < 0)time = 0;do{
printf( "%d\n", time );if(time == 0) // if the time reached 0, it will print Happy New Year then end
printf( "Happy New Year!!\n");time--;
}while(time > -1);getchar();
}
C Language Programming Code :
http://eglobiotraining.com/
void countdown(int time); *this is a method that does anything
int time; *the max time
countdown(time);*function that will do the things below
if(time < 0) *if the time is less than 0, it will automatically be "0“
if(time == 0)printf( "Happy New Year!!\n"); *if the time reached 0, it will print “Happy New Year” then end
Programming Functions
http://eglobiotraining.com/
PROGRAM # 4Output screenshot :
http://eglobiotraining.com/
#include <stdio.h>
int exponent ( int number, int power );
int main(){
int number, power;printf( "Please input a number: " );scanf( "%d", &number );printf( "Please input the power of exponent\n (greater than zero): " );scanf( "%d", &power );printf( "The result is %d\n", exponent( number, power ) ); getchar();
}
int exponent (int number, int power){
int last = 1; if (power > 0)
for(int x = 0; x < power; x++)last *= number;
return last;}
C Language Programming Code :
http://eglobiotraining.com/
int exponent ( int number, int power ); /*this is a function that returns int value
printf( "The result is %d\n", exponent( number, power ) ); *exponent(number, power) will automatically perform tasks and return int value
int last = 1; *serve as the temporary number
if (power > 0) *if the power is greater than zero, it will perform for loop until (x < power) is false
last *= number; *same as last = last * number* it means the left "last" will be the output of last * number
Programming Functions
http://eglobiotraining.com/
PROGRAM # 5Output screenshot :
http://eglobiotraining.com/
#include <stdio.h>
void ladder(int top);int main(){
int top;printf( "Please input the height of the ladder: " );scanf( "%d", &top );printf( "Draw...\n" );ladder(top); getchar();
}
void ladder(int top){
for (int y = 1; y < top + 1; y++){
for (int x = y; x > 0; x--)printf("%d",x);
printf("\n");}
}
C Language Programming Code :
http://eglobiotraining.com/
void ladder(int top);*this is a method that does anything
ladder(top); *function that will do the things below
for (int y = 1; y < top + 1; y++)*the width of the ladder will gradually increase
Programming Functions
http://eglobiotraining.com/
II. SWITCH CASESTATEMENTS
Programming Lesson :
http://eglobiotraining.com/
In programming, these are substitutes for long if statements that compare a variable to several "integral" values ("integral" values are simply values that can be expressed as an integer, such as the value of a char). The value of the variable given into switch in C Language programming is compared to the value following each of the cases, and when one value matches the value of the variable, the computer continues executing the program from that point.
Switch case statements . . .
http://eglobiotraining.com/
C uses two different orders in programming to control loop’s flow :
•break – escapes from the nearest outer loop
•continue – inside “while” and “do” loop: switches program execution to test condition, inside “for” loop: switches program execution to “for” loop step and then to condition test (also applies for nearest outer loop)
LOOP FLOW CONTROLS : BREAK AND CONTINUE
http://eglobiotraining.com/
PROGRAM # 1Output screenshot :
http://eglobiotraining.com/
#include <stdio.h>
int plus (int one, int two); int minus (int one, int two);int multi (int one, int two);int divide (int one, int two);int exponent (int number, int power);int factorial ( int number );
int main(){
int choice, x, y;printf( "(1) addition\n" );printf( "(2) subtraction\n" );printf( "(3) multiplication\n" );printf( "(4) division\n" );printf( "(5) exponent\n" );printf( "(6) factorial\n" );printf( "Please input the number of the method you want to perform(1 - 6): " );scanf( "%d", &choice );switch (choice){
C Language Programming Code (part 1):
http://eglobiotraining.com/
case 1: case 2: case 3: case 4:printf( "Please input the 1st number: ");scanf( "%d", &x );printf( "Please input the 2nd number: ");scanf( "%d", &y );break;
case 5:printf( "Please input the number: ");scanf( "%d", &x );printf( "Please input the power: ");scanf( "%d", &y );break;
case 6:printf( "Please input a number: ");scanf( "%d", &x );break;
}switch (choice){
case 1: printf( "result of addition: %d\n" ,plus(x, y));break;
case 2: printf( "result of subtraction: %d\n" ,minus(x, y));break;
case 3: printf( "result of multiplication: %d\n" ,multi(x, y));break;
case 4: printf( "result of division: %d\n" ,divide(x, y));break;
case 5: printf( "result of exponent: %d\n" ,exponent(x, y));break;
case 6: printf( "result of factorial: %d\n" ,factorial(x));break;
}getchar();
}
(part 2):
http://eglobiotraining.com/
int factorial ( int number ){
int last = 1;while (number > 0){
last *= number;number--;
}return last;
}
int exponent (int number, int power){
int last = 1;if (power > 0)
for(int x = 0; x < power; x++)last *= number;
return last;}
int plus (int one, int two){
return one + two;}
(part 3):
http://eglobiotraining.com/
int minus (int one, int two){
return one - two;}
int multi (int one, int two){
return one * two;}
int divide (int one, int two){
return one / two;}
(part 4):
http://eglobiotraining.com/
int plus (int one, int two); int minus (int one, int two);int multi (int one, int two);int divide (int one, int two);int exponent (int number, int power);int factorial ( int number );*these are functions that return int values
int choice, x, y;printf( "(1) addition\n" );printf( "(2) subtraction\n" );printf( "(3) multiplication\n" );printf( "(4) division\n" );printf( "(5) exponent\n" );printf( "(6) factorial\n" );printf( "Please input the number of the method you want to perform(1 - 6): " );
scanf( "%d", &choice );* a "switch" if quite similar to "if statement"*after you input the choice, the switch will find a similar case and perform task*switch will end after it reach "break;"
Programming Functions :
http://eglobiotraining.com/
PROGRAM # 2Output screenshot :
http://eglobiotraining.com/
#include <stdio.h>
int charToint(char letter);
int main(){
char letter;printf( "Please input a character (a - z): " );scanf( "%c", &letter );switch (charToint(letter)%10) {
case 1:printf("It is the %dst character.\n",charToint(letter));break;
case 2:printf("It is the %dnd character.\n",charToint(letter));break;
case 3:printf("It is the %drd character.\n",charToint(letter));break;
default:printf("It is the %dth character.\n",charToint(letter));break;
}getchar();
}
int charToint(char letter){
C Language Programming Code (part 1) :
http://eglobiotraining.com/
switch (letter) {
case 'a': case 'A': return 1;case 'b': case 'B': return 2;case 'c': case 'C': return 3;case 'd': case 'D': return 4;case 'e': case 'E': return 5;case 'f': case 'F': return 6;case 'g': case 'G': return 7;case 'h': case 'H': return 8;case 'i': case 'I': return 9;case 'j': case 'J': return 10;case 'k': case 'K': return 11;case 'l': case 'L': return 12;case 'm': case 'M': return 13;case 'n': case 'N': return 14;case 'o': case 'O': return 15;case 'p': case 'P': return 16;case 'q': case 'Q': return 17;case 'r': case 'R': return 18;case 's': case 'S': return 19;case 't': case 'T': return 20;case 'u': case 'U': return 21;case 'v': case 'V': return 22;case 'w': case 'W': return 23;case 'x': case 'X': return 24;case 'y': case 'Y': return 25;case 'z': case 'Z': return 26;getchar();
}}
(part 2):
http://eglobiotraining.com/
int charToint(char letter); *this is a function that returns int value
char letter;printf( "Please input a character (a - z): " );scanf( "%c", &letter );
* a "switch" if quite similar to "if statement"* after you input the letter, the switch will find a similar case and perform task*switch will end after it reach "break;“
switch (charToint(letter)%10) *"charToint(letter)%10" returns a remainder of charToint(letter)/10
switch (letter) *alphabet ranking of letters a to z
Programming Functions
http://eglobiotraining.com/
PROGRAM # 3Output screenshot :
http://eglobiotraining.com/
#include <stdio.h>
int plus (int one, int two); // these are functions that return int valuesint minus (int one, int two);int multi (int one, int two);int divide (int one, int two);
int main(){
int choice, x, y;printf( "Please input the 1st number: ");scanf( "%d", &x );printf( "Please input the 2nd number: ");scanf( "%d", &y );printf( "(1) addition\n" );printf( "(2) subtraction\n" );printf( "(3) multiplication\n" );printf( "(4) division\n" );printf( "Please input the number of the method you want to perform(1 - 4): " );scanf( "%d", &choice );// a "switch" if quite similar to "if statement"// after you input the choice, the switch will find a similar case and perform task// switch will end after it reach "break;"switch (choice) {
C Language Programming Code :
http://eglobiotraining.com/
case 1: printf( "result of addition: %d\n" ,plus(x, y));break;
case 2: printf( "result of subtraction: %d\n" ,minus(x, y));break;
case 3: printf( "result of multiplication: %d\n" ,multi(x, y));break;
case 4: printf( "result of division: %d\n" ,divide(x, y));break;
}getchar();
}
int plus (int one, int two){
return one + two;}
int minus (int one, int two){
return one - two;}
int multi (int one, int two){
return one * two;}
int divide (int one, int two){
return one / two;}
http://eglobiotraining.com/
int plus (int one, int two); int minus (int one, int two);int multi (int one, int two);int divide (int one, int two);* these are functions that return int values
*a "switch" if quite similar to "if statement“
*after you input the choice, the switch will find a similar case and perform task
*switch will end after it reach "break;“
•a "switch" if quite similar to "if statement“
*after you input the choice, the switch will find a similar case and perform task
*switch will end after it reach "break;"
Programming Functions
http://eglobiotraining.com/
PROGRAM # 4Output screenshot :
http://eglobiotraining.com/
#include <stdio.h>
int main(){
int choice;printf( "(1) facebook\n" );printf( "(2) Yahoo\n" );printf( "(3) twitter\n" );printf( "(4) Tagged\n" );printf( "(5) LinkedIn\n" );printf( "Please input the number of the Network you like (1 - 5): " );scanf( "%d", &choice );switch (choice){
case 1: printf( "I LOVE facebook\n" );break;
case 2: printf( "I like Yahoo\n" );break;
case 3: printf( "I like twitter\n" );break;
case 4: printf( "I like Tagged\n" );break;
case 5: printf( "I like LinkedIn\n" );break;
}getchar();
}
C Language Programming Code :
http://eglobiotraining.com/
*simply get the a number of choice then print
*a "switch" if quite similar to "if statement“
*after you input the choice, the switch will find a similar case and perform task
*switch will end after it reach "break;"
Programming Tips
http://eglobiotraining.com/
PROGRAM # 5Output screenshot :
http://eglobiotraining.com/
#include <stdio.h>
void printabc(int number);
int main(){
int number;printf( "Please input number of characters: " );scanf( "%d", &number );printabc(number);getchar();
}
void printabc(int number){
while (number > 0){
switch(number%3){
case 0:printf("a");break;
case 1:printf("b");break;
case 2:printf("c");break;
}number--;
}printf("\n");
}
C Language Programming Code :
http://eglobiotraining.com/
int number;printf( "Please input number of
characters: " );scanf( "%d", &number );printabc(number);
*input: length of the word*output: word with a, b and c
Programming Functions
http://eglobiotraining.com/
http://www.slideshare.net/upload?from_source=loggedin_homepage_navbar
This presentation is to be submitted for the final
requirements in Fundamentals of Programming.
http://eglobiotraining.com/
Prof. Erwin Globio
http://eglobiotraining.com