Knowledge in C Programming with Solutions

C if else Statement

C if else StatementThe if-else statement in C is used to perform the operations based on some specific condition. The operations specified in if block are executed if and only if the given condition is true.There are the following variants of if statement in C language.If statementIf-else statementIf else-if ladderNested ifIf StatementThe if statement is used to check some given condition and perform some operations depending upon the correctness of that condition. It is mostly used in the scenario where we need to perform the different operations for the different conditions. The syntax of the if statement is given below.if(expression){  //code to be executed  } Let's see a simple example of C language if statement.#include<stdio.h>    int main(){    int number=0;    printf("Enter a number:");    scanf("%d",&number);    if(number%2==0){    printf("%d is even number",number);    }    return 0;  }    OutputEnter a number:4 4 is even number enter a number:5 If-else StatementThe if-else statement is used to perform two operations for a single condition. The if-else statement is an extension to the if statement using which, we can perform two different operations, i.e., one is for the correctness of that condition, and the other is for the incorrectness of the condition. Here, we must notice that if and else block cannot be executed simiulteneously. Using if-else statement is always preferable since it always invokes an otherwise case with every if condition. The syntax of the if-else statement is given below.if(expression){  //code to be executed if condition is true  }else{  //code to be executed if condition is false  } Let's see the simple example to check whether a number is even or odd using if-else statement in C language.#include<stdio.h>    int main(){    int number=0;    printf("enter a number:");    scanf("%d",&number);     if(number%2==0){    printf("%d is even number",number);    }    else{    printf("%d is odd number",number);    }     return 0;  }    Outputenter a number:4 4 is even number enter a number:5 5 is odd number

C Switch Statement

C Switch StatementThe switch statement in C is an alternate to if-else-if ladder statement which allows us to execute multiple operations for the different possibles values of a single variable called switch variable. Here, We can define various statements in the multiple cases for the different values of a single variable.The syntax of switch statement in c language is given below:switch(expression){    case value1:     //code to be executed;     break;  //optional  case value2:     //code to be executed;     break;  //optional  ......        default:      code to be executed if all cases are not matched;    }  Rules for switch statement in C language1) The switch expression must be of an integer or character type.2) The case value must be an integer or character constant.3) The case value can be used only inside the switch statement.4) The break statement in switch case is not must. It is optional. If there is no break statement found in the case, all the cases will be executed present after the matched case. It is known asfall through the state of C switch statement.Let's try to understand it by the examples. We are assuming that there are following variables.int x,y,z;  char a,b;  float f; Switch case example 2#include <stdio.h>  int main()  {      int x = 10, y = 5;       switch(x>y && x+y>0)      {          case 1:           printf("hi");          break;           case 0:           printf("bye");          break;          default:           printf(" Hello bye ");      }             }  Outputhi

C Loops

C LoopsThe looping can be defined as repeating the same process multiple times until a specific condition satisfies. There are three types of loops used in the C language. In this part of the tutorial, we are going to learn all the aspects of C loops.Why use loops in C language?The looping simplifies the complex problems into the easy ones. It enables us to alter the flow of the program so that instead of writing the same code again and again, we can repeat the same code for a finite number of times. For example, if we need to print the first 10 natural numbers then, instead of using the printf statement 10 times, we can print inside a loop which runs up to 10 iterations.Advantage of loops in c1) It provides code reusability.2) Using loops, we do not need to write the same code again and again.3) Using loops, we can traverse over the elements of data structures (array or linked lists)Types of C LoopsThere are three types of loops in C language that is given below:do whilewhilefordo-while loop in CThe do-while loop continues until a given condition satisfies. It is also called post tested loop. It is used when it is necessary to execute the loop at least once (mostly menu driven programs).The syntax of do-while loop in c language is given below:do{  //code to be executed  }while(condition);  Flowchart and Example of do-while loopdo-while loop in CThe do-while loop continues until a given condition satisfies. It is also called post tested loop. It is used when it is necessary to execute the loop at least once (mostly menu driven programs).The syntax of do-while loop in c language is given below:do{  //code to be executed  }while(condition);  Flowchart and Example of do-while loop

do while loop in C

do while loop in CThe do while loop is a post tested loop. Using the do-while loop, we can repeat the execution of several parts of the statements. The do-while loop is mainly used in the case where we need to execute the loop at least once. The do-while loop is mostly used in menu-driven programs where the termination condition depends upon the end user.do while loop syntaxThe syntax of the C language do-while loop is given below:do{  //code to be executed  }while(condition); Example 1#include<stdio.h>  #include<stdlib.h>  void main ()  {      char c;      int choice,dummy;        do{      printf("\n1. Print Hello\n2. Print Javatpoint\n3. Exit\n");      scanf("%d",&choice);      switch(choice)      {          case 1 :           printf("Hello");           break;          case 2:            printf("Javatpoint");          break;          case 3:          exit(0);           break;          default:           printf("please enter valid choice");          }      printf("do you want to enter more?");       scanf("%d",&dummy);      scanf("%c",&c);      }while(c=='y');  }  Output1. Print Hello 2. Print Javatpoint 3. Exit 1 Hello do you want to enter more? y 1. Print Hello 2. Print Javatpoint 3. Exit 2 Javatpoint do you want to enter more? n

C break statement

C break statementThe break is a keyword in C which is used to bring the program control out of the loop. The break statement is used inside loops or switch statement. The break statement breaks the loop one by one, i.e., in the case of nested loops, it breaks the inner loop first and then proceeds to outer loops. The break statement in C can be used in the following two scenarios:With switch caseWith loopSyntax://loop or switch case   break; Example#include<stdio.h>  #include<stdlib.h>  void main ()  {      int i;      for(i = 0; i<10; i++)      {          printf("%d ",i);          if(i == 5)          break;      }      printf("came outside of loop i = %d",i);        }  Output0 1 2 3 4 5 came outside of loop i = 5

C continue statement

C continue statementThe continue statement in C language is used to bring the program control to the beginning of the loop. The continue statement skips some lines of code inside the loop and continues with the next iteration. It is mainly used for a condition so that we can skip some code for a particular condition.Syntax://loop statements  continue;  //some lines of the code which is to be skipped  Continue statement example 1#include<stdio.h>  void main ()  {      int i = 0;       while(i!=10)      {          printf("%d", i);           continue;           i++;      }  }  Output infinite loop

C goto statement

C goto statementThe goto statement is known as jump statement in C. As the name suggests, goto is used to transfer the program control to a predefined label. The goto statment can be used to repeat some part of the code for a particular condition. It can also be used to break the multiple loops which can't be done by using a single break statement. However, using goto is avoided these days since it makes the program less readable and complecated.Syntax:label:   //some part of the code;   goto label; goto exampleLet's see a simple example to use goto statement in C language.#include <stdio.h>  int main()   {    int num,i=1;     printf("Enter the number whose table you want to print?");     scanf("%d",&num);    table:     printf("%d x %d = %d\n",num,i,num*i);    i++;    if(i<=10)    goto table;    }  Output:Enter the number whose table you want to print?10 10 x 1 = 10 10 x 2 = 20 10 x 3 = 30 10 x 4 = 40 10 x 5 = 50 10 x 6 = 60 10 x 7 = 70 10 x 8 = 80 10 x 9 = 90 10 x 10 = 100

Type Casting in C

Typecasting allows us to convert one data type into other. In C language, we use cast operator for typecasting which is denoted by (type).Syntax:(type)value; Without Type Casting:int f= 9/4;  printf("f : %d\n", f );//Output: 2  With Type Casting:float f=(float) 9/4;  printf("f : %f\n", f );//Output: 2.250000  Type Casting exampleLet's see a simple example to cast int value into the float.#include<stdio.h>  int main(){  float f= (float)9/4;    printf("f : %f\n", f );    return 0;  }      Output:f : 2.250000

Pattern printing using printf

This is the basic pattern printing programs using printf statements.

Introduction to variables

This this the introduction to various variables and its data types.

Arithmetical operations in C programming

The introduction of variables is followed by mathematics operations using operators.

Fibonacci series

This is the fibonacci series in C.