BlogsDope image BlogsDope

Common conditional examples in C

July 3, 2017 EXAMPLE C 2526

This post is an extension of the ‘Decide if else’ chapter of C. If you need to learn basics then visit the C course first. You can also practice a good number of questions from practice section.

Finding maximum of three numbers


In this example, we will take the input of three integers from the user and find the maximum number among them.

#include <stdio.h>
int main ()
{
	int x,y,z;

	//taking inputs
	printf("Enter the first number\n");
	scanf("%d",&x);
	printf("Enter the second number\n");
	scanf("%d",&y);
	printf("Enter the third number\n");
	scanf("%d",&z);

	//comparing numbers
	if(x == y && y == z)
	{
		printf("All are equal\n");
	}
	else
	{
		if(x>=y && x>=z)
			printf("%d is the greatest\n",x);
		else if(y>=x && y>=z)
			printf("%d is the greatest\n",y);
		else if(z>=x && z>=y)
			printf ("%d is the greatest\n",z);
	}
	return 0;
}

In first few lines, we are just taking the input from the user and then we are making sure that all the three numbers are not equal and if they are equal then we are just printing “All are equal”. If all of the three numbers are not equal then the compiler will proceed to the statements in the ‘else’.

if(x>=y && x>=z) – This condition will be true if and only if the variable ‘x’ is greater than or equal to the variable ‘y’ and also greater than or equal to the variable ‘z’. Also, all of them can’t be equal because we have already taken care of this in the ‘if’ statement. If this condition is true then the variable ‘x’ is the largest.

Similarly, we are checking for the variables ‘y’ and ‘z’ in else if(y>=x && y>=z) and else if(z>=x && z>=y) respectively.

Check whether a character is an alphabet or digit


The simplest way of checking if a character is an alphabet or digit is by checking its ASCII value. You can download the ASCII chart from here. The ASCII value of 0 is 48 and that of 9 is 57. It means that if the character is a digit then its ASCII value will lie between 48 and 57(inclusive). Also, all the ASCII values of all the upper case alphabets lie between 65 and 90(ASCII values of A and Z are 65 and 90 respectively) and that of lower case alphabets lie between 97 and 122(ASCII values of a and z are 97 and 122 respectively).

So, if ASCII value is in between 48 and 57 then it is a digit and if it is between 65 and 90 or 97 and 122 then it is an alphabet.

#include <stdio.h>
int main ()
{
	char a;
	printf("Enter a character\n");
	scanf(" %c",&a);

	//checking ASCII value
	//if it is in between 48 and 57
	//it is a digit
	if(a>=48 && a<=57)
		printf("It is a digit\n");
	//if it is in between
	//65 and 90
	//or in between
	//97 and 122
	//then it is an alphabet
	else if( (a>=65 && a<=90) || (a>=97 && a<=122) )
		printf("It is an alphabet\n");
	//neither digit nor alphabet
	else
		printf("Neither a digit nor an alphabet\n");
	return 0;
}

We have just implemented the above logic in the code using ‘if’, ‘else if’ and ‘else’.

Check Whether a Character is Vowel or Consonant


In this code, we will first check if the character entered is an alphabet or not by using the logic of the previous example and then just check for the vowel and consonant.

#include <stdio.h>
int main ()
{
	char a;
	printf("Enter a character\n");
	scanf(" %c",&a);

	//if ASCII value is in between
	//65 and 90
	//or in between
	//97 and 122
	//then it is an alphabet
	if( (a>=65 && a<=90) || (a>=97 && a<=122) )
	{
		//checking for vowel
		if(a=='a' || a=='e' || a=='i' || a=='o' || a=='u' || a=='A' || a=='E' || a=='I' || a=='O' || a=='U')
			printf("It is a vowel\n");
		else
			printf("Consonant\n");
	}
	//neither not an alphabet
	else
		printf("Not an alphabet\n");
	return 0;
}

if( (a>=65 && a<=90) || (a>=97 && a<=122) ) –  We are just checking if the entered character is an alphabet or not using its ASCII value as explained in the previous example.

If the character is an alphabet then we are checking for the vowel using if(a=='a' || a=='e' || a=='i' || a=='o' || a=='u' || a=='A' || a=='E' || a=='I' || a=='O' || a=='U').

Number of days in any month


In this example, we will ask the user to enter an integer for the corresponding month( for example, 1 for January, 2 for February and so on) and then we will just print the number of days in that month except February. In the case of February, we will again ask the user to enter the year and then check if the year is a leap year or not. If the year is a leap year then we will print 29, else 28.

So, let’s do it.

#include <stdio.h>
int main ()
{
	int month;
	int year;
	printf("Enter the month\n");
	scanf("%d",&month);
	if (month>= 1 && month<=12)
	{
		if(month==1 || month==3 || month==5 || month==7 || month==8 || month==10 || month==12)
			printf("31\n");
		else if(month==4 || month==6 || month==9 || month==11)
			printf("30\n");
		//month is 2
		//February
		else
		{
			printf("Enter year\n");
			scanf("%d",&year);
			//cheking for century year
			if(year%100 == 0)
			{
				if(year%400 == 0)
					printf("29\n");
                else
                    printf("28\n");
			}
			//not century year
			else if(year%4 == 0)
				printf("29\n");
			else
				printf("28\n");
		}
	}
	else
		printf("Invalid input");
	return 0;
}

if (month>= 1 && month<=12) – We are just checking if the month entered is valid or not and then we just printed the number of days in the month except for February. In the case of February, we again asked the user to enter the year to check for leap year. We know that a year is a leap year if it is divisible by 4 (or by 400 in the case of century year (2000, 2100, 2200)).

if(year%100 == 0) – Checking for century year. If it is a century year then we will just check its divisibility by 400 and we are doing the same with if(year%400 == 0).

else if(year%4 == 0) – The year is not a century year. We just have to check the divisibility with 4.


Liked the post?
Developer and founder of CodesDope.
Editor's Picks
2 COMMENTS

Please login to view or add comment(s).