C code for if & if else statement with diagram


If statement in C programming with example

When we need to execute a block of statements only when a given condition is true then we use if statement. In the next tutorial, we will learn C if..else, nested if..else and else..if.

C – If statement

Syntax of if statement:
The statements inside the body of “if” only execute if the given condition returns true. If the condition returns false then the statements inside “if” are skipped.

if (condition)
{
     //Block of C statements here
     //These statements will only execute if the condition is true
}

Flow Diagram of if statement

C-if-statement

Example of if statement

#include 
int main()
{
    int x = 20;
    int y = 22;
    if (x<y)
    {
        printf("Variable x is less than y");
    }
    return 0;
}

Output:

Variable x is less than y

Explanation: The condition (x

Example of multiple if statements

We can use multiple if statements to check more than one conditions.

#include 
int main()
{
    int x, y;
    printf("enter the value of x:");
    scanf("%d", &x);
    printf("enter the value of y:");
    scanf("%d", &y);
    if (x>y)
    {
	printf("x is greater than y
");
    }
    if (x<y)
    {
	printf("x is less than y
");
    }
    if (x==y)
    {
	printf("x is equal to y
");
    }
    printf("End of Program");
    return 0;
}

In the above example the output depends on the user input.

Output:

enter the value of x:20
enter the value of y:20
x is equal to y
End of Program

C – If..else, Nested If..else and else..if Statement with example

In the last tutorial we learned how to use if statement in C. In this guide, we will learn how to use if else, nested if else and else if statements in a C Program.

C If else statement

Syntax of if else statement:
If condition returns true then the statements inside the body of “if” are executed and the statements inside body of “else” are skipped.
If condition returns false then the statements inside the body of “if” are skipped and the statements in “else” are executed.

if(condition) {
   // Statements inside body of if
}
else {
   //Statements inside body of else
}

Flow diagram of if else statement

C If else flow diagram

Example of if else statement

In this program user is asked to enter the age and based on the input, the if..else statement checks whether the entered age is greater than or equal to 18. If this condition meet then display message “You are eligible for voting”, however if the condition doesn’t meet then display a different message “You are not eligible for voting”.

#include 
int main()
{
   int age;
   printf("Enter your age:");
   scanf("%d",&age);
   if(age >=18)
   {
	/* This statement will only execute if the
	 * above condition (age>=18) returns true
	 */
	printf("You are eligible for voting");
   }
   else
   {
	/* This statement will only execute if the
	 * condition specified in the "if" returns false.
	 */
	printf("You are not eligible for voting");
   }
   return 0;
}

Output:

Enter your age:14
You are not eligible for voting

Note: If there is only one statement is present in the “if” or “else” body then you do not need to use the braces (parenthesis). For example the above program can be rewritten like this:

#include 
int main()
{
   int age;
   printf("Enter your age:");
   scanf("%d",&age);
   if(age >=18)
	printf("You are eligible for voting");
   else
	printf("You are not eligible for voting");
   return 0;
}

C Nested If..else statement

When an if else statement is present inside the body of another “if” or “else” then this is called nested if else.
Syntax of Nested if else statement:

if(condition) {
    //Nested if else inside the body of "if"
    if(condition2) {
       //Statements inside the body of nested "if"
    }
    else {
       //Statements inside the body of nested "else"
    }
}
else {
    //Statements inside the body of "else"
}

Example of nested if..else

#include 
int main()
{
   int var1, var2;
   printf("Input the value of var1:");
   scanf("%d", &var1);
   printf("Input the value of var2:");
   scanf("%d",&var2);
   if (var1 != var2)
   {
	printf("var1 is not equal to var2
");
	//Nested if else
	if (var1 > var2)
	{
		printf("var1 is greater than var2
");
	}
	else
	{
		printf("var2 is greater than var1
");
	}
   }
   else
   {
	printf("var1 is equal to var2
");
   }
   return 0;
}

Output:

Input the value of var1:12
Input the value of var2:21
var1 is not equal to var2
var2 is greater than var1

C – else..if statement

The else..if statement is useful when you need to check multiple conditions within the program, nesting of if-else blocks can be avoided using else..if statement.

Syntax of else..if statement:

if (condition1) 
{
   //These statements would execute if the condition1 is true
}
else if(condition2) 
{
   //These statements would execute if the condition2 is true
}
else if (condition3) 
{
   //These statements would execute if the condition3 is true
}
.
.
else 
{
   //These statements would execute if all the conditions return false.
}

Example of else..if statement

Lets take the same example that we have seen above while discussing nested if..else. We will rewrite the same program using else..if statements.

#include 
int main()
{
   int var1, var2;
   printf("Input the value of var1:");
   scanf("%d", &var1);
   printf("Input the value of var2:");
   scanf("%d",&var2);
   if (var1 !=var2)
   {
	printf("var1 is not equal to var2
");
   }
   else if (var1 > var2)
   {
	printf("var1 is greater than var2
");
   }
   else if (var2 > var1)
   {
	printf("var2 is greater than var1
");
   }
   else
   {
	printf("var1 is equal to var2
");
   }
   return 0;
}

Output:

Input the value of var1:12
Input the value of var2:21
var1 is not equal to var2

As you can see that only the statements inside the body of “if” are executed. This is because in this statement as soon as a condition is satisfied, the statements inside that block are executed and rest of the blocks are ignored.

Important Points:
1. else and else..if are optional statements, a program having only “if” statement would run fine.
2. else and else..if cannot be used without the “if”.
3. There can be any number of else..if statement in a if else..if block.
4. If none of the conditions are met then the statements in else block gets executed.
5. Just like relational operators, we can also use logical operators such as AND (&&), OR(||) and NOT(!).

    

    By er ravi Bhadana 👍👍



HOW TO MAKE SYMBOLS WITH KEYBOARD


Useful Info: HOW TO MAKE SYMBOLS WITH KEYBOARD.
=================
Alt + 0153..... ™... trademark symbol
Alt + 0169.... ©.... copyright symbol
Alt + 0174..... ®....registeredtrademark symbol
Alt + 0176 ...°......degree symbol
Alt + 0177 ...±....plus-or-minus sign
Alt + 0182 ...¶.....paragraph mark
Alt + 0190 ...¾....fraction, three-fourths
Alt + 0215 ....×.....multiplication sign
Alt + 0162...¢....thecent sign
Alt + 0161.....¡......upside down exclamation point
Alt + 0191.....¿.....upside down question mark
Alt + 1...........smiley face
Alt + 2 ......☻.....black smiley face
Alt + 15.....☼.....sun
Alt + 12......♀.....female sign
Alt + 11.....♂......male sign
Alt + 6.......♠.....spade
Alt + 5.......♣......Club
Alt + 3............. Heart
Alt + 4.......♦......Diamond
Alt + 13......♪.....eighth note
Alt + 14......♫......beamed eighth note
Alt + 8721.... ∑.... N-ary summation (auto sum)
Alt + 251.....√.....square root check mark
Alt + 8236.....∞.....infinity
Alt + 24.......↑.....up arrow
Alt + 25......↓......down arrow
Alt + 26.....→.....right arrow
Alt + 27......←.....left arrow
Alt + 18.....↕......up/down arrow
Alt + 29......↔... left right arrow..

Don't Forget to Share it on your Wall..!!
[8/19, 3:42 AM]  +91 81379 67796 : More than 100 Keyboard Shortcuts must read & Share.
=================
1. CTRL+C (Copy)
2. CTRL+X (Cut)
3. CTRL+V (Paste)
4. CTRL+Z (Undo)
5. DELETE (Delete)
6. SHIFT+DELETE (Delete the selected item permanently without placing the item in the Recycle Bin)
7. CTRL while dragging an item (Copy the selected item)
8. CTRL+SHIFT while dragging an item (Create a shortcut to the selected item)
9. F2 key (Rename the selected item)

10. CTRL+RIGHT ARROW (Move the insertion point to the beginning of the next word)
11. CTRL+LEFT ARROW (Move the insertion point to the beginning of the previous word)
12. CTRL+DOWN ARROW (Move the insertion point to the beginning of the next paragraph)
13. CTRL+UP ARROW (Move the insertion point to the beginning of the previous paragraph)
14. CTRL+SHIFT with any of the arrow keys (Highlight a block of text)
SHIFT with any of the arrow keys (Select more than one item in a window or on the desktop, or select text in a document)
15. CTRL+A (Select all)
16. F3 key (Search for a file or a folder)
17. ALT+ENTER (View the properties for the selected item)
18. ALT+F4 (Close the active item, or quit the active program)
19. ALT+ENTER (Display the properties of the selected object)
20. ALT+SPACEBAR (Open the shortcut menu for the active window)

21. CTRL+F4 (Close the active document in programs that enable you to have multiple documents opensimultaneously)
22. ALT+TAB (Switch between the open items)
23. ALT+ESC (Cycle through items in the order that they had been opened)
24. F6 key (Cycle through the screen elements in a window or on the desktop)
25. F4 key (Display the Address bar list in My Computer or Windows Explorer)
26. SHIFT+F10 (Display the shortcut menu for the selected item)
27. ALT+SPACEBAR (Display the System menu for the active window)
28. CTRL+ESC (Display the Start menu)
29. ALT+Underlined letter in a menu name (Display the corresponding menu) Underlined letter in a command name on an open menu (Perform the corresponding command)
30. F10 key (Activate the menu bar in the active program)

31. RIGHT ARROW (Open the next menu to the right, or open a submenu)
32. LEFT ARROW (Open the next menu to the left, or close a submenu)
33. F5 key (Update the active window)
34. BACKSPACE (View the folder onelevel up in My Computer or Windows Explorer)
35. ESC (Cancel the current task)
36. SHIFT when you insert a CD-ROMinto the CD-ROM drive (Prevent the CD-ROM from automatically playing)
Dialog Box - Keyboard Shortcuts
1. CTRL+TAB (Move forward through the tabs)
2. CTRL+SHIFT+TAB (Move backward through the tabs)
3. TAB (Move forward through the options)
4. SHIFT+TAB (Move backward through the options)
5. ALT+Underlined letter (Perform the corresponding command or select the corresponding option)
6. ENTER (Perform the command for the active option or button)

C code for for matrices


 

Example(a): Program to Add Two Matrices

  1. #include
  2. int main(){
  3. int r, c, a[100][100], b[100][100], sum[100][100], i, j;
  4. printf("Enter number of rows (between 1 and 100): ");
  5. scanf("%d", &r);
  6. printf("Enter number of columns (between 1 and 100): ");
  7. scanf("%d", &c);
  8. printf(" Enter elements of 1st matrix: ");
  9. for(i=0; i<r; ++i)
  10. for(j=0; j<c; ++j)
  11. {
  12. printf("Enter element a%d%d: ",i+1,j+1);
  13. scanf("%d",&a[i][j]);
  14. }
  15. printf("Enter elements of 2nd matrix: ");
  16. for(i=0; i<r; ++i)
  17. for(j=0; j<c; ++j)
  18. {
  19. printf("Enter element a%d%d: ",i+1, j+1);
  20. scanf("%d", &b[i][j]);
  21. }
  22. // Adding Two matrices
  23. for(i=0;i<r;++i)
  24. for(j=0;j<c;++j)
  25. {
  26. sum[i][j]=a[i][j]+b[i][j];
  27. }
  28. // Displaying the result
  29. printf(" Sum of two matrices: ");
  30. for(i=0;i<r;++i)
  31. for(j=0;j<c;++j)
  32. {
  33. printf("%d ",sum[i][j]);
  34. if(j==c-1)
  35. {
  36. printf(" ");
  37. }
  38. }
  39. return 0;
  40. }

Output

Enter number of rows (between 1 and 100): 2
Enter number of columns (between 1 and 100): 3

Enter elements of 1st matrix:
Enter element a11: 5
Enter element a12: 3
Enter element a13: 4
Enter element a21: 1
Enter element a22: 2
Enter element a23: 3
Enter elements of 2nd matrix:
Enter element a11: -4
Enter element a12: 6
Enter element a13: 3
Enter element a21: 5
Enter element a22: 6
Enter element a23: 3

Sum of two matrices: 
1   9   7   

6   8   6 

Example{b}: Program to Multiply Two Matrices

  1. #include
  2. int main()
  3. {
  4. int a[10][10], b[10][10], result[10][10], r1, c1, r2, c2, i, j, k;
  5. printf("Enter rows and column for first matrix: ");
  6. scanf("%d %d", &r1, &c1);
  7. printf("Enter rows and column for second matrix: ");
  8. scanf("%d %d",&r2, &c2);
  9. // Column of first matrix should be equal to column of second matrix and
  10. while (c1 != r2)
  11. {
  12. printf("Error! column of first matrix not equal to row of second. ");
  13. printf("Enter rows and column for first matrix: ");
  14. scanf("%d %d", &r1, &c1);
  15. printf("Enter rows and column for second matrix: ");
  16. scanf("%d %d",&r2, &c2);
  17. }
  18. // Storing elements of first matrix.
  19. printf(" Enter elements of matrix 1: ");
  20. for(i=0; i<r1; ++i)
  21. for(j=0; j<c1; ++j)
  22. {
  23. printf("Enter elements a%d%d: ",i+1, j+1);
  24. scanf("%d", &a[i][j]);
  25. }
  26. // Storing elements of second matrix.
  27. printf(" Enter elements of matrix 2: ");
  28. for(i=0; i<r2; ++i)
  29. for(j=0; j<c2; ++j)
  30. {
  31. printf("Enter elements b%d%d: ",i+1, j+1);
  32. scanf("%d",&b[i][j]);
  33. }
  34. // Initializing all elements of result matrix to 0
  35. for(i=0; i<r1; ++i)
  36. for(j=0; j<c2; ++j)
  37. {
  38. result[i][j] = 0;
  39. }
  40. // Multiplying matrices a and b and
  41. // storing result in result matrix
  42. for(i=0; i<r1; ++i)
  43. for(j=0; j<c2; ++j)
  44. for(k=0; k<c1; ++k)
  45. {
  46. result[i][j]+=a[i][k]*b[k][j];
  47. }
  48. // Displaying the result
  49. printf(" Output Matrix: ");
  50. for(i=0; i<r1; ++i)
  51. for(j=0; j<c2; ++j)
  52. {
  53. printf("%d ", result[i][j]);
  54. if(j == c2-1)
  55. printf(" ");
  56. }
  57. return 0;
  58. }

Output

Enter rows and column for first matrix: 3
2
Enter rows and column for second matrix: 3
2
Error! column of first matrix not equal to row of second.

Enter rows and column for first matrix: 2
3
Enter rows and column for second matrix: 3
2

Enter elements of matrix 1:
Enter elements a11:2 
Enter elements a12:1 
Enter elements a13:2 
Enter elements a21:3 
Enter elements a22:1 
Enter elements a23:4 

Enter elements of matrix 2:
Enter elements b11:2 
Enter elements b12:-1 
Enter elements b21:2 
Enter elements b22:3 
Enter elements b31:1 
Enter elements b32:1 

Output Matrix:
8  3

12  4

Example{c} Program to Find Transpose of a Matrix

This program takes a matrix of order r*c from the user and computes the transpose of that matrix.

To understand this example, you should have the knowledge of following C programming topics:

In this program, user is asked to entered the number of rows r and columns c. The value of r and cshould be less than 10 in this program.

The user is asked to enter elements of the matrix (of order r*c).

Then, the program computes the transpose of the matrix and displays it on the screen.

Example[d]: Program to Find Transpose of a Matrix

  1. #include
  2. int main()
  3. {
  4. int a[10][10], transpose[10][10], r, c, i, j;
  5. printf("Enter rows and columns of matrix: ");
  6. scanf("%d %d", &r, &c);
  7. // Storing elements of the matrix
  8. printf(" Enter elements of matrix: ");
  9. for(i=0; i<r; ++i)
  10. for(j=0; j<c; ++j)
  11. {
  12. printf("Enter element a%d%d: ",i+1, j+1);
  13. scanf("%d", &a[i][j]);
  14. }
  15. // Displaying the matrix a[][] */
  16. printf(" Entered Matrix: ");
  17. for(i=0; i<r; ++i)
  18. for(j=0; j<c; ++j)
  19. {
  20. printf("%d ", a[i][j]);
  21. if (j == c-1)
  22. printf(" ");
  23. }
  24. // Finding the transpose of matrix a
  25. for(i=0; i<r; ++i)
  26. for(j=0; j<c; ++j)
  27. {
  28. transpose[j][i] = a[i][j];
  29. }
  30. // Displaying the transpose of matrix a
  31. printf(" Transpose of Matrix: ");
  32. for(i=0; i<c; ++i)
  33. for(j=0; j<r; ++j)
  34. {
  35. printf("%d ",transpose[i][j]);
  36. if(j==r-1)
  37. printf(" ");
  38. }
  39. return 0;
  40. }

Output

Enter rows and columns of matrix: 2
3

Enter element of matrix:
Enter element a11: 5
Enter element a12: 6
Enter element a13: 8
Enter element a21: 3
Enter element a22: 9
Enter element a23: 1

Entered Matrix: 
5  6  8 

3  9  1  


Transpose of Matrix:
5  3 

6  9  

8  1  

Example[E] Inverse Matrix of 3x3 in C  

C code for inverse of 3×3matrix

  1. #include
  2.  
  3. int main(){
  4.  
  5. int a[3][3],i,j;
  6. float determinant=0;
  7.  
  8. printf("Enter the 9 elements of matrix: ");
  9. for(i=0;i<3;i++)
  10. for(j=0;j<3;j++)
  11. scanf("%d",&a[i][j]);
  12.  
  13. printf(" The matrix is ");
  14. for(i=0;i<3;i++){
  15. printf(" ");
  16. for(j=0;j<3;j++)
  17. printf("%d ",a[i][j]);
  18. }
  19.  
  20. for(i=0;i<3;i++)
  21. determinant = determinant + (a[0][i]*(a[1][(i+1)%3]*a[2][(i+2)%3] - a[1][(i+2)%3]*a[2][(i+1)%3]));
  22.  
  23. printf(" Inverse of matrix is: ");
  24. for(i=0;i<3;i++){
  25. for(j=0;j<3;j++)
  26. printf("%.2f ",((a[(i+1)%3][(j+1)%3] * a[(i+2)%3][(j+2)%3]) - (a[(i+1)%3][(j+2)%3]*a[(i+2)%3][(j+1)%3]))/ determinant);
  27. printf(" ");
  28. }
  29.  
  30. return 0;
  31. }
  32.  
  33. Type this code on your computer and execute input and output. Best of luck 

Program to Find the Trace & Normal of a given Matrix

This is a program to find the trace & normal of a given matrix.

Problem Description

This C Program find the trace & normal of a given matrix. Here trace of the matrix is the sum of the elements of the main diagonal i.e the diagonal from the upper left to the lower right of a matrix. Normal of the matrix is the square root of the sum of all the elements.

Problem Solution

1. Create a matrix and define all its elements.
2. To evaluate normal of the matrix, take sum of all the elements of the array and calculate the square root of it.
3. To evaluate trace of the matrix, take sum of the main diagonal elements.

Program/Source Code

Here is source code of the C program to find the trace & normal of a given matrix. The program is successfully compiled and tested using Turbo C compiler in windows environment. The program output is also shown below.

  1.     /*
  2.      * C program to find the trace and normal of a matrix
  3.      *
  4.      * Trace is defined as the sum of main diagonal elements and
  5.      * Normal is defined as square root of the sum of all the elements
  6.      */
  7.  
  8.     #include 
  9.     #include 
  10.  
  11.     void main ()
  12.     {
  13.  
  14.         static int array[10][10];
  15.         int i, j, m, n, sum = 0, sum1 = 0, a = 0, normal;
  16.  
  17.         printf("Enter the order of the matrix
    ");
  18.         scanf("%d %d", &m, &n);
  19.  
  20.         printf("Enter the n coefficients of the matrix 
    ");
  21.         for (i = 0; i < m; ++i) 
  22.         {
  23.             for (j = 0; j < n; ++j)
  24.             {
  25.                 scanf("%d", &array[i][j]);
  26.                 a = array[i][j] * array[i][j];
  27.                 sum1 = sum1 + a;
  28.             }
  29.         }
  30.  
  31.         normal = sqrt(sum1);
  32.         printf("The normal of the given matrix is = %d
    ", normal);
  33.         for (i = 0; i < m; ++i) 
  34.         {
  35.             sum = sum + array[i][i];
  36.         }
  37.  
  38.         printf("Trace of the matrix is = %d
    ", sum);
  39.  
  40.     }
Program Explanation

1. Declare a matrix and define all its elements.
2. Declare variables for storing the normal and trace of the matrix.
3. Find the sum of all the elements of the matrix using nested for loop.
4. Evaluate normal by passing the above calculated sum to sqrt() function.
5. Take sum of all the main diagonal elements of the array to calculate trace.
6. Print trace and normmal

Runtime Test Cases

Enter the order of the matrix
3 3
Enter the coefficients of the matrix
3  7 9
2 6 10
8 5 9
The normal of the given matrix is = 21
Trace of the matrix is = 18

C code for if &amp; if else statement with diagram

If statement in C programming with example BY er RAVI BHADANA  |  FILED UNDER:  C-PROGRAMMING When we need to execute a block of statements ...