Skip to main content

C Programming Decision Making and Loops Examples(PART 1)

C Programming Decision Making and Loops Examples

You will find necessary examples to create programs containing loops and decision making statements in this article.

To understand examples on this page, you should have the knowledge of following topics:

C Control Flow Examples

Check Whether a Number is Even or Odd






An even number is an integer that is exactly divisible by 2. Example: 0, 8, -24
An odd number is an integer that is not exactly divisible by 2. Example: 1, 7, -11, 15

Example #1: Program to Check Even or Odd

  1. #include <stdio.h>
  2. int main()
  3. {
  4. int number;
  5. printf("Enter an integer: ");
  6. scanf("%d", &number);
  7. // True if the number is perfectly divisible by 2
  8. if(number % 2 == 0)
  9. printf("%d is even.", number);
  10. else
  11. printf("%d is odd.", number);
  12. return 0;
  13. }
Output
Enter an integer: -7
-7 is odd.
In the program, integer entered by the user is stored in variable number.
Check Whether a Character is Vowel or consonant


    The five alphabets A, E, I, O and U are called vowels. All other alphabets except these 5 vowel letters are called consonants.
    This program assumes that the user will always enter an alphabet character.

    Example #1: Program to Check Vowel or consonant

    1. #include <stdio.h>
    2. int main()
    3. {
    4. char c;
    5. int isLowercaseVowel, isUppercaseVowel;
    6. printf("Enter an alphabet: ");
    7. scanf("%c",&c);
    8. // evaluates to 1 (true) if c is a lowercase vowel
    9. isLowercaseVowel = (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u');
    10. // evaluates to 1 (true) if c is an uppercase vowel
    11. isUppercaseVowel = (c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U');
    12. // evaluates to 1 (true) if either isLowercaseVowel or isUppercaseVowel is true
    13. if (isLowercaseVowel || isUppercaseVowel)
    14. printf("%c is a vowel.", c);
    15. else
    16. printf("%c is a consonant.", c);
    17. return 0;
    18. }
    Output
    Enter an alphabet: G
    G is a consonant.
    The character entered by the user is stored in variable c.
    The isLowerCaseVowel evaluates to 1 (true) if c is a lowercase vowel and 0 (false) for any other character.
    Similarly, isUpperCaseVowel evaluates to 1(true) if c is an uppercase vowel and 0 (false) for any other character.
    If both isLowercaseVowel and isUppercaseVowel is equal to 0, the test expression evaluates to 0 (false) and the entered character is a consonant.
    However, if either isLowercaseVowel or isUppercaseVowel is 1 (true), the test expression evaluates to 1 (true) and the entered character is a vowel.
    The program above assumes that the user always enters an alphabet. If the user enters any other character other than an alphabet, the program will not work as intended.

    Example #2: Program to Check Vowel or consonant

    The program below asks the user to enter a character until the user enters an alphabet. Then, the program checks whether it is a vowel or a consonant.
    1. #include <stdio.h>
    2. #include <ctype.h>
    3. int main()
    4. {
    5. char c;
    6. int isLowercaseVowel, isUppercaseVowel;
    7. do {
    8. printf("Enter an alphabet: ");
    9. scanf(" %c", &c);
    10. }
    11. // isalpha() returns 0 if the passed character is not an alphabet
    12. while (!isalpha(c));
    13. // evaluates to 1 (true) if c is a lowercase vowel
    14. isLowercaseVowel = (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u');
    15. // evaluates to 1 (true) if c is an uppercase vowel
    16. isUppercaseVowel = (c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U');
    17. // evaluates to 1 (true) if either isLowercaseVowel or isUppercaseVowel is true
    18. if (isLowercaseVowel || isUppercaseVowel)
    19. printf("%c is a vowel.", c);
    20. else
    21. printf("%c is a consonant.", c);
    22. return 0;
    23. }
    Find the Largest Number Among Three Numbers Entered by User


    This program uses only if statement to find the largest number.

    Example #1

    1. #include <stdio.h>
    2. int main()
    3. {
    4. double n1, n2, n3;
    5. printf("Enter three different numbers: ");
    6. scanf("%lf %lf %lf", &n1, &n2, &n3);
    7. if( n1>=n2 && n1>=n3 )
    8. printf("%.2f is the largest number.", n1);
    9. if( n2>=n1 && n2>=n3 )
    10. printf("%.2f is the largest number.", n2);
    11. if( n3>=n1 && n3>=n2 )
    12. printf("%.2f is the largest number.", n3);
    13. return 0;
    14. }
    This program uses if...else statement to find the largest number.

    Example #2

    1. #include <stdio.h>
    2. int main()
    3. {
    4. double n1, n2, n3;
    5. printf("Enter three numbers: ");
    6. scanf("%lf %lf %lf", &n1, &n2, &n3);
    7. if (n1>=n2)
    8. {
    9. if(n1>=n3)
    10. printf("%.2lf is the largest number.", n1);
    11. else
    12. printf("%.2lf is the largest number.", n3);
    13. }
    14. else
    15. {
    16. if(n2>=n3)
    17. printf("%.2lf is the largest number.", n2);
    18. else
    19. printf("%.2lf is the largest number.",n3);
    20. }
    21. return 0;
    22. }
    This program uses nested if...else statement to find the largest number.

    Example #3

    1. #include <stdio.h>
    2. int main()
    3. {
    4. double n1, n2, n3;
    5. printf("Enter three numbers: ");
    6. scanf("%lf %lf %lf", &n1, &n2, &n3);
    7. if( n1>=n2 && n1>=n3)
    8. printf("%.2lf is the largest number.", n1);
    9. else if (n2>=n1 && n2>=n3)
    10. printf("%.2lf is the largest number.", n2);
    11. else
    12. printf("%.2lf is the largest number.", n3);
    13. return 0;
    14. }
    Though, the largest number among three numbers is found using multiple ways, the output of all these program will be same.
    Enter three numbers: -4.5
    3.9
    5.6
    5.60 is the largest number.
    Find all Roots of a Quadratic equation




    The standard form of a quadratic equation is:
    ax2 + bx + c = 0, where
    a, b and c are real numbers and
    a ≠ 0
    
    The term b2-4ac is known as the discriminant of a quadratic equation. The discriminant tells the nature of the roots.
    • If discriminant is greater than 0, the roots are real and different.
    • If discriminant is equal to 0, the roots are real and equal.
    • If discriminant is less than 0, the roots are complex and different.

    Example: Program to Find Roots of a Quadratic Equation

    1. #include <stdio.h>
    2. #include <math.h>
    3. int main()
    4. {
    5. double a, b, c, discriminant, root1, root2, realPart, imaginaryPart;
    6. printf("Enter coefficients a, b and c: ");
    7. scanf("%lf %lf %lf",&a, &b, &c);
    8. discriminant = b*b-4*a*c;
    9. // condition for real and different roots
    10. if (discriminant > 0)
    11. {
    12. // sqrt() function returns square root
    13. root1 = (-b+sqrt(discriminant))/(2*a);
    14. root2 = (-b-sqrt(discriminant))/(2*a);
    15. printf("root1 = %.2lf and root2 = %.2lf",root1 , root2);
    16. }
    17. //condition for real and equal roots
    18. else if (discriminant == 0)
    19. {
    20. root1 = root2 = -b/(2*a);
    21. printf("root1 = root2 = %.2lf;", root1);
    22. }
    23. // if roots are not real
    24. else
    25. {
    26. realPart = -b/(2*a);
    27. imaginaryPart = sqrt(-discriminant)/(2*a);
    28. printf("root1 = %.2lf+%.2lfi and root2 = %.2f-%.2fi", realPart, imaginaryPart, realPart, imaginaryPart);
    29. }
    30. return 0;
    31. }
    Output 
    Enter coefficients a, b and c: 2.3
    4
    5.6
    Roots are: -0.87+1.30i and -0.87-1.30i
    In this program, library function sqrt() is used to find the square root of a number. To learn more, visit:  sqrt() function.
    Check Whether the Entered Year is Leap Year or not


    A leap year is exactly divisible by 4 except for century years (years ending with 00). The century year is a leap year only if it is perfectly divisible by 400.

    Example: Program to Check Leap Year

    1. #include <stdio.h>
    2. int main()
    3. {
    4. int year;
    5. printf("Enter a year: ");
    6. scanf("%d",&year);
    7. if(year%4 == 0)
    8. {
    9. if( year%100 == 0)
    10. {
    11. // year is divisible by 400, hence the year is a leap year
    12. if ( year%400 == 0)
    13. printf("%d is a leap year.", year);
    14. else
    15. printf("%d is not a leap year.", year);
    16. }
    17. else
    18. printf("%d is a leap year.", year );
    19. }
    20. else
    21. printf("%d is not a leap year.", year);
    22. return 0;
    23. }
    Output 1
    Enter a year: 1900
    1900 is not a leap year.
    Check Whether a Number is Positive or Negative or Zero


    This program takes a number from the user and checks whether that number is either positive or negative or zero.

    Example #1: Check if a Number is Positive or Negative Using if...else

    1. #include <stdio.h>
    2. int main()
    3. {
    4. double number;
    5. printf("Enter a number: ");
    6. scanf("%lf", &number);
    7. if (number <= 0.0)
    8. {
    9. if (number == 0.0)
    10. printf("You entered 0.");
    11. else
    12. printf("You entered a negative number.");
    13. }
    14. else
    15. printf("You entered a positive number.");
    16. return 0;
    17. }

    Example #2: Check if a Number is Positive or Negative Using Nested if...else

    1. #include <stdio.h>
    2. int main()
    3. {
    4. double number;
    5. printf("Enter a number: ");
    6. scanf("%lf", &number);
    7. // true if number is less than 0
    8. if (number < 0.0)
    9. printf("You entered a negative number.");
    10. // true if number is greater than 0
    11. else if ( number > 0.0)
    12. printf("You entered a positive number.");
    13. // if both test expression is evaluated to false
    14. else
    15. printf("You entered 0.");
    16. return 0;
    17. }
    Output 1
    Enter a number: 12.3
    You entered a positive number.
    
    Output 2
    Enter a number: 0
    You entered 0.
    
    .
    Checker Whether a Character is an Alphabet or not


    Example: Program to Check Alphabet

    1. #include <stdio.h>
    2. int main()
    3. {
    4. char c;
    5. printf("Enter a character: ");
    6. scanf("%c",&c);
    7. if( (c>='a' && c<='z') || (c>='A' && c<='Z'))
    8. printf("%c is an alphabet.",c);
    9. else
    10. printf("%c is not an alphabet.",c);
    11. return 0;
    12. }
    Output
    Enter a character: *
    * is not an alphabet
    In C programming, a character variable holds ASCII value (an integer number between 0 and 127) rather than that character itself.
    The ASCII value of lowercase alphabets are from 97 to 122. And, the ASCII value of uppercase alphabets are from 65 to 90.
    If the ASCII value of the character entered by the user lies in the range from 97 to 122 or from 65 to 90, that number is an alphabet. In the program,'a' is used instead of 97 and 'z' is used instead of 122. Similarly, 'A' is used instead of 65 and 'Z' is used instead of 90.
    You can also check whether a character is an alphabet or not using isalpha() function
    Find Sum of Natural Numbers



    The positive numbers 1, 2, 3... are known as natural numbers. The programs below takes a positive integer (let say n) as an input from the user and calculates the sum up to n.

    Example #1: Sum of Natural Numbers Using for Loop

    1. #include <stdio.h>
    2. int main()
    3. {
    4. int n, i, sum = 0;
    5. printf("Enter a positive integer: ");
    6. scanf("%d",&n);
    7. for(i=1; i <= n; ++i)
    8. {
    9. sum += i; // sum = sum+i;
    10. }
    11. printf("Sum = %d",sum);
    12. return 0;
    13. }
    14.  
    The above program takes the input from the user and stores in variable n. Then, for loop is used to calculate the sum upto the given number.

    Example #2: Sum of Natural Numbers Using while Loop

    1. #include <stdio.h>
    2. int main()
    3. {
    4. int n, i, sum = 0;
    5. printf("Enter a positive integer: ");
    6. scanf("%d",&n);
    7. i = 1;
    8. while ( i <=n )
    9. {
    10. sum += i;
    11. ++i;
    12. }
    13. printf("Sum = %d",sum);
    14. return 0;
    15. }
    Output
    Enter a positive integer: 100
    Sum = 5050
    In both programs, the loop is iterated n number of times. And, in each iteration, the value of i is added to sum and i is incremented by 1.
    Though both programs are technically correct, it is better to use for loop in this case. It's because the number of iteration is known.
    The above programs doesn't work properly if the user enters a negative integer. Here's a little modification of the above program to take input from the user until positive integer is entered.

    Example #3: Program to Read Input Until User Enters a Positive Integer

    1. #include <stdio.h>
    2. int main()
    3. {
    4. int n, i, sum = 0;
    5. do {
    6. printf("Enter a positive integer: ");
    7. scanf("%d",&n);
    8. }
    9. while (n <= 0);
    10. for(i=1; i <= n; ++i)
    11. {
    12. sum += i; // sum = sum+i;
    13. }
    14. printf("Sum = %d",sum);
    15. return 0;
    16. }

    Comments

    Popular posts from this blog

    short key of c programming

    short key of c programming Shortcut Keys(Hot Keys)     Operation    Alt + F  -            Open file Alt + F3 -           Close Alt + F + N  -    New file Alt + X  -           Exit turbo c Alt + F5 -          Output Screen Alt + F + L  -    Check programme load or not. Alt + ENTER  - Full screen Shift + Del -       Cut Shift + Insert -   Paste Ctr + Insert -    Copy Alt+F9 -           Compile a program Ctr + F8           Bbreak point Ctrl+F9 -          To run a program Ctrl+s -...

    C LANGUAGE - OVERVIEW C - VARIABLES

    C LANGUAGE - OVERVIEW Advertisements C is a general-purpose, high-level language that was originally developed by Dennis M. Ritchie to develop the UNIX operating system at Bell Labs. C was originally first implemented on the DEC PDP-11 computer in 1972. In 1978, Brian Kernighan and Dennis Ritchie produced the first publicly available description of C, now known as the K&R standard. The UNIX operating system, the C compiler, and essentially all UNIX application programs have been written in C. C has now become a widely used professional language for various reasons − Easy to learn Structured language It produces efficient programs It can handle low-level activities It can be compiled on a variety of computer platforms Facts about C C was invented to write an operating system called UNIX. C is a successor of B language which was introduced around the early 1970s. The language was formalized in 1988 by the American National Standard Institute  A N S I A N S ...

    environment

    It looks like you meant "environment." The environment refers to the surroundings or conditions in which a person, animal, or plant lives or operates. It encompasses various elements, including natural resources, ecosystems, climate, and human-made structures. Protecting and preserving the environment is crucial for the well-being of all living organisms on Earth. Is there anything specific you'd like to discuss or learn about regarding the environment?