# C Decision Making Programs | 10+ Examples

In this article, after going through all the fundamentals of C programming languages we will learn C decision-making programs with examples.

Do not forget that the best way of learning programming is by practising and developing your concept.

#### 1 WAP to input two numbers & print the greatest.

``````#include <stdio.h>
int main()
{
int n, m;
// Asking user to enter the two numbers
// Accepting two numbers
scanf("%d %d", &n, &m);
if(n > m)
{
printf("%d is Largest\n", n);
}
else if (m > n)
{
printf("%d is Largest\n", m);
}
else
{
printf("Both are Equal\n");
}
return 0;
}``````

Output

``````Please Enter Two different values
8
6
8 is Largest
``````

We can write the same program using a conditional operator (ternary operator)

``````#include <stdio.h>
int main()
{
int n, m;
// Asking user to enter the two numbers
// Read two numbers from the user
scanf("%d %d", &n, &m);
(n >= m)?((n ==m)?printf("Numbers are equal"):printf("%d is Largest\n", n)):printf("%d is Largest\n", m);
return 0;
}``````

Output

``````Please Enter Two different values
7
8
8 is Largest
``````

#### 2 WAP to find the largest out of three numbers.

Let’s write the steps involved in the program sequentially.

• Firstly we will ask user to enter three integer values.
• The entered value will be stored in the three integer values in num1, num2, and num3
• Compare the number and check if num1 is greater than num2.
• If true, then we will check if num1 is greater than num3.
• If true, then print ‘num1’ as the greatest number.
• If false, then print ‘num3’ as the greatest number.
• If false, then we will check if num2 is greater than num3.
• If true, then print ‘num2’ as the greatest number.
• If false, then print ‘num3’ as the greatest number.

Program

``````#include <stdio.h>
int main()
{
int p, q, r;
printf(" Enter the number1 = ");
scanf("%d", &p);
printf("\n Enter the number2 = ");
scanf("%d", &q);
printf("\n Enter the number3 = ");
scanf("%d", &r);
if (p > q)
{
if (p > r)
{
printf("\n Largest number = %d \n",p);
}
else
{
printf("\n Largest number = %d \n",r);
}
}
else if (q > r)
{
printf("\n Largest number = %d \n",q);
}
else
{
printf("\n Largest number = %d \n",r);
}
return 0;
}``````

Output

``````Enter the number1 = 6
Enter the number2 = 9
Enter the number3 = 5
Largest number = 9``````

#### 3 WAP to check if the year is a leap year or not

Flow chart for checking leap year

Program to check year is a leap year or not

``````#include <stdio.h>
int main()
{
int year;
printf("Enter a year: ");
scanf("%d", &year);

// leap year if perfectly divisible by 400
if (year % 400 == 0) {
printf("%d is a leap year.", year);
}
// not a leap year if divisible by 100
// but not divisible by 400
else if (year % 100 == 0) {
printf("%d is not a leap year.", year);
}
// leap year if not divisible by 100
// but divisible by 4
else if (year % 4 == 0) {
printf("%d is a leap year.", year);
}
// all other years are not leap years
else {
printf("%d is not a leap year.", year);
}

return 0;
}``````

Output

``````//Output-1
Enter a year: 2020
2020 is a leap year.

//Output-2
Enter a year: 2019
2019 is not a leap year.``````

#### 4 WAP to input a character and print its next character.

Ex. If the character entered is ‘a’, display “The next character is b”.

``````#include <stdio.h>
int main()
{
char ch;
printf("Enter a character:\t");
scanf("%c", &ch);
printf("You entered: %c\n", ch);
printf("Next character: %c\n", ch + 1);
}
``````

Output

``````Enter a character: B
You entered: B
Next character: C``````

#### 5 WAP to input a character and print its previous character.

Ex. If the character entered is ‘b’, display “The previous character is a”.

``````#include <stdio.h>
int main()
{
char ch;
printf("Enter a character:\t");
scanf("%c", &ch);
printf("You entered: %c\n", ch);
printf("Previous character: %c\n", ch - 1);
}
``````

Output

``````Enter a character:	K
You entered: K
Previous character: J
``````

#### ASCII Values A-65 Z-90 ; a-97 z-122. Click here for ASCII Table

There are 2 ways of writing this program

Method-1

``````#include<stdio.h>

int main() {
char ch;

printf("\nEnter The Character : ");
scanf("%c", &ch);

if (ch >= 65 && ch <= 90)
printf("Character is Upper Case Letters");
else
printf("Character is Not Upper Case Letters");

return (0);
}``````

Method-2

``````#include<stdio.h>

int main() {
char ch;

printf("\nEnter The Character : ");
scanf("%c", &ch);

if (ch >= 'A' && ch <= 'Z')
printf("Character is Upper Case Letters");
else
printf("Character is Not Upper Case Letters");

return (0);
}``````

Output

``````Enter The Character : A
Character is Uppercase Letters``````

Do not forget to execute the program here by changing the logic and values to see the different outputs in order to have a better understanding of the program.

#### 7 WAP to input a character and change its case.

``````#include<stdio.h>

int main()
{
char ch;
printf("Enter any character : ");
scanf("%ch", &ch);
if(ch>='A' && ch<='Z')
ch=ch+32;
else if(ch>'a' && ch<='z')
ch=ch-32;
printf("Convert case of character : %c",ch);
return 0;
}``````

Output

``````Enter any character : D
Convert case of character : d``````

#### 8 WAP to input a character and check if it is a vowel or a consonant.

``````#include <stdio.h>

int main()
{
char ch;

scanf(" %c", &ch);

if(ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u' ||
ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U')
{
printf("\n %c is a VOWEL.", ch);
}
else
{
printf("\n %c is a CONSONANT.", ch);
}
return 0;
}``````

Output

``````Please Enter an alphabet:
e
e is a VOWEL.``````

#### 9 WAP to find the average of six subjects and display the results as follows

``````#include <stdio.h>

int main()
{
float marks1, marks2, marks3, marks4, marks5, marks6, avg;

printf("Enter marks obtained in subject 1 :");
scanf("%f", &marks1);
printf("Enter marks obtained in subject 2 :");
scanf("%f", &marks2);
printf("Enter marks obtained in subject 3 :");
scanf("%f", &marks3);
printf("Enter marks obtained in subject 4 :");
scanf("%f", &marks4);
printf("Enter marks obtained in subject 5 :");
scanf("%f", &marks5);
printf("Enter marks obtained in subject 6 :");
scanf("%f", &marks6);

avg = (marks1 + marks2 + marks3 + marks4 + marks5 + marks6) / 6;
printf("avg : %0.2f\n", avg);

if (avg > 75)
{
printf("Distinction");
}
else if (avg > 60 && avg <= 75)
{
printf("First Division");
}
else if (avg > 50 && avg <= 60)
{
printf("Second Division");
}
else if (avg > 34 && avg <= 50)
{
}
else
{
}

return 0;
}``````

Output

``````Enter marks obtained in subject 1 :85
Enter marks obtained in subject 2 :85
Enter marks obtained in subject 3 :85
Enter marks obtained in subject 4 :85
Enter marks obtained in subject 5 :85
Enter marks obtained in subject 6 :85
avg : 85.00
Distinction``````

#### 10 WAP to input a character and to print whether a given character is an alphabet, digit or any other character.

``````#include<stdio.h>
int main()
{
char ch;
printf("\nEnter Any Character :");
scanf("%c",&ch);
if(ch>='0' && ch<='9')
{
printf("\n Entered Character is Digit");
}
else
if(ch>='A' && ch<='Z')
{
printf("\n Entered Character is Capital Letter Alphabet");
}
else if(ch>='a' && ch<='z')
{
printf("\n Entered Character is Small Letter Alphabet");
}
else
printf("\n Entered Character is Special Character");
return 0;
}``````

Output

``````Enter Any Character :@
Entered Character is Special Character

Enter Any Character :d
Entered Character is Small Letter Alphabet

Enter Any Character :A
Entered Character is Capital Letter Alphabet``````

Above are some basic questions asked in classes related to c decision making programs.

Do not forget to execute the program here by changing the logic and values to see the different outputs in order to have a better understanding of the program.