Month: December 2017

  • How to use for loop in C Programming

     

    For loop is also a type of repetition structure. This repetition structure is simpler in syntax as compared with other 2 loops because all four loop conditions can be defined in single line. Following simple program explains how to use for loop to print numbers from 1-10.

    [js]

    #include <stdio.h>

    int main(void){
    // for loop is a type of repetition structure in C programming. In any loop you need to fulfill following 4 conditions

    for(int i=1; i<=10; i++){ // here in one line we defined control variable i (condition 1),
    //assigned initial value to control variable i (condition 2), defined loop termination condition (condition 3)
    //and incremented the control variable (condition 4)

    printf("%dn", i);

    }

    return 0;
    }
    [/js]

    Output:

  • How to use do-while loop in C Programming

     

    Do-while loop is also a type of repetition structure used to repeat statement(s) for certain number of times. The major difference between while and do while loop is that in do while loop the loop termination condition is checked after the execution of loop body whereas in while loop the loop termination condition is checked before the execution of loop body. Do-while loop at least runs one time no matter if the loop termination condition is true or false. Following simple program explains how to use do-while repetition structure (also called as do-while loop) to print numbers from 1-10.

    [js]
    #include <stdio.h>

    int main(void){
    // do-while loop is a type of repetition structure in C programming. In any loop you need to fulfill following 4        conditions
    int i; // 1st condition: define control variable
    i = 1; // 2nd condition: assign initial value to control variable
    do{
    printf("%dn", i);
    i++; // 4th condition: increment/decrement control variable
    }while(i<=10); // 3rd condition: define loop termination condition

    return 0;
    }
    [/js]

  • How to use while loop in C Programming

     

    Sometimes in C program we need to repeat statements for certain number of times. So in order to repeat statements for number of times we use repetition structure in C programming. There are 3 types of repetition structures available in C programming namely while, do-while and for. Following simple program explains how to use while repetition structure (also called as while loop) to print numbers from 1-10.

    [js]
    #include <stdio.h>

    int main(void){
    // while loop is a type of repetition structure in C programming. In any loop you need to
    //fulfill following 4 conditions
    int i; // 1st condition: define control variable
    i = 1; // 2nd condition: assign initial value to control variable
    while(i<=10){ // 3rd condition: define loop termination condition
    printf("%dn", i);
    i++; // 4th condition: increment/decrement control variable
    }

    return 0;
    }
    [/js]

    Output:

  • How to use switch statement in C Programming

     

    Switch statement is also called as multiple selection structure in C Programming. Switch selection structure is similar to if-else selection structure. The difference is instead of using multiple if-else statement we use ‘cases’ to check for multiple conditions. The ‘default’ case will run when no condition is true. The following simple c program explains how to use switch statement in c programming.

    [js]

    #include <stdio.h>

    int main(void){
    int number1; //defined variable named as number1

    printf("Please Enter Integer Number:n");
    scanf("%d", &number1); // getting variable value from user and assiging value to variable named as number1
    printf("n");

    switch(number1){ // expression to check is written in parenthesis
    case 10: // it’s similar to: if (number1 == 10)
    {
    printf("your entered number is 10n");
    break; // break statement is used to terminate the switch statement
    }
    case 20: // it’s similar to: if (number1 == 20)
    {
    printf("your entered number is 20n");
    break; // break statement is used to terminate the switch statement
    }
    case 30: // it’s similar to: if (number1 == 30)
    {
    printf("your entered number is 30n");
    break; // break statement is used to terminate the switch statement
    }
    default: // the default case will run when none of the above case is true
    {
    printf("you entered number other than 10,20, or 30");
    break; // break statement is used to terminate the switch statement
    }
    }
    [/js]

    Output:

  • How to use if-else statement in C Programming

    In previous post we discussed about if statement in C Programming. If-else statement called as double selection structure in C Programming. In simple words it says “Do something when a condition is true, else (when condition is false) do some other thing”. Following simple c program explains how to use if-else statement in C Programming.

    [js]

    #include <stdio.h>

    int main(void){
    int number1; //defined variable named as number1
    int number2; // defined variable named as number2

    printf("Please Enter First Integer Number:n");
    scanf("%d", &number1); // getting variable value from user and assiging value to variable named as number1
    printf("n");
    printf("Please Enter Second Integer Number:n");
    scanf("%d", &number2); // getting variable value from user and assiging value to variable named as number2

    if(number1 == number2){
    printf("%d is equals to %dn", number1, number2); // first %d will hold the value of the variable mentioned after firs comma i.e number1.
    //second %d will hold the value of the variable mentioned after second comma i.e number2

    }else{
    printf("%d is not equals to %dn", number1, number2); // this statement will always run when number1 is not equals to number2

    if(number1 > number2){ // this condition will be true if number1 is greater than number2
    printf("%d is greater than %dn", number1, number2);

    }else if(number1 < number2){ // this condition will be true if number1 is lesser than number2
    printf("%d is less than %dn", number1, number2);
    }

    }

    return 0;
    }
    [/js]

    Output:

  • How to use if statement in C Programming

     

    If statement is called as selection structure in C Programming. If statement checks a condition and proceed further depending upon the fact that whether the condition being checked is true or false. In simple words it says “Do something when this condition is true”. Following simple program explains how to use if statement to compare 2 numbers entered by the user.

    [js]

    #include <stdio.h>

    int main(void){
    // defining variables, computing their sum and printing sum on the screen

    int number1; //defined variable named as number1
    int number2; // defined variable named as number2

    printf("Please Enter First Integer Number:n");
    scanf("%d", &number1); // getting variable value from user and assiging value to variable named as number1
    printf("n");
    printf("Please Enter Second Integer Number:n");
    scanf("%d", &number2); // getting variable value from user and assiging value to variable named as number2

    if(number1 == number2){ //this condition will be true if number1 is equal to number2
    printf("%d is equals to %dn", number1, number2); // first %d will hold the value of the variable mentioned after firs comma i.e number1.
    //second %d will hold the value of the variable mentioned after second comma i.e number2
    }

    if(number1 >= number2){ // this condition will be true if either number1 is equal to number2 or number1 is greater than number2
    printf("%d is greater than or equals to %dn", number1, number2);
    }

    if(number1 > number2){ // this condition will be true if number1 is greater than number2
    printf("%d is greater than %dn", number1, number2);
    }

    if(number1 <= number2){ // this condition will be true if either number1 is equal to number2 or number1 is lesser than number2
    printf("%d is less than or equals to %dn", number1, number2);
    }

    if(number1 < number2){ // this condition will be true if number1 is lesser than number2
    printf("%d is less than %dn", number1, number2);
    }

    if(number1 != number2){ // this condition will be true if number1 is not equal to number2
    printf("%d is not equals to %dn", number1, number2);
    }

    return 0;
    }
    [/js]

    Output:

  • How to define variables in c programming

    In our previous post we discussed about how to get values from user. Sometimes we need to define variables and assign values to those variables. This simple program explains how to define variables and how to assign values to the variables in c programming.

    [js]
    #include <stdio.h>

    int main(void){
    // defining variables, computing their sum and printing sum on the screen

    int number1; //defined variable named as number1 with data type integer
    int number2; // defined variable named as number2 with data type integer
    int sum; // defined variable named as sum with data type integer

    number1 = 5; // assigned value to variable named as number1
    number2 = 20; // assigned value to variable named as number2
    sum = number1 + number2; // assigned value to variable named as sum

    printf("Sum = %d", sum); // printing the value stored in variable named as sum. %d will hold the value of variable named as sum.
    //Furthermore %d tells the compiler that the value of variable sum should be of the data type decimal integer

    return 0;
    }
    [/js]

    Output:

  • How to get value from user in C Programming

    Sometimes in a program we need to get values from user, process the values entered by user and print / output the results of that processing on the screen. In order to get values from user we use ‘scanf’ function which is part of the stdio.h library. Following simple program explains how to use scanf function to get 2 numbers from user, sum the 2 entered by the user and print the sum of 2 numbers on the output screen.

    [js]

    #include <stdio.h>

    int main(void){
    // defining variables, computing their sum and printing sum on the screen
    int number1; //defined variable named as number1 with data type integer
    int number2; // defined variable named as number2 with data type integer
    int sum; // defined variable named as sum with data type integer

    printf("Please Enter First Integer Number:n");
    scanf("%d", &number1); // getting value from user and assiging entered value to variable named as number1. %d defines that the value to be entered should be an integer
    printf("n");
    printf("Please Enter Second Integer Number:n");
    scanf("%d", &number2); // getting variable value from user and assiging value to variable named as number2. %d defines that the value to be entered should be an integer
    sum = number1 + number2; // summing two numbers and assigning result to variable named as sum

    printf("Sum = %d", sum); // printing the value stored in variable named as sum. %d will hold the value of variable named as sum. Further %d says that I’ll hold the value of data type decimal integer

    return 0;
    }
    [/js]

    Output:

  • How to Output Text/String in C Programming

    In C programming we use printf function (which is part of stdio.h library) to print / output some text or variable on the screen. Following simple C program outputs a text on the screen. It also explains how to move to the new line and how to add tabbed space between words.

    [js]
    #include <stdio.h>

    int main( void ){
    // lines starting with double slashes are called as comments.
    //Comments are used to explain code and they are not executed by compiler

    printf("Welcome to C!n"); // n moves the cursor to the new line
    printf("Hello My Name is Junaid Hassant We added tabbed spacen"); // t adds tabbed space

    return 0;
    }
    [/js]