Note: This is a very simple program but still a very important one, because we’ll be using some form of logic to print elements of an array. So better we know ins and outs of printing array elements in whichever order the program demands. So please pay attention to the logic.
Video Tutorial: C Program To Print Elements of Array In Reverse Order
#include<stdio.h>
int main()
{
int a[5], i;
printf("Enter 5 integer numbers\n");
for(i = 0; i < 5; i++)
scanf("%d", &a[i]);
printf("Array elements are:\n");
for(i = 4; i >= 0; i--)
printf("%d\n", a[i]);
return 0;
}
Output: Enter 5 integer numbers 1 2 3 4 5 Array elements are: 5 4 3 2 1
Since the array size is 5, the last index of the array will be (5-1) which is 4. So we initialize i to 4, and keep decrementing the value of i by 1 for each iteration of the for loop. Control exits for loop once i value is equal to 0. In arrays the index starts from 0. Inside for loop, for each iteration, we print the value of i.
#include<stdio.h>
#define N 5
int main()
{
int a[N], i;
printf("Enter %d integer numbers\n", N);
for(i = 0; i < N; i++)
scanf("%d", &a[i]);
printf("Array elements are:\n");
for(i = N-1; i >= 0; i--)
printf("%d\n", a[i]);
return 0;
}
Output: Enter 5 integer numbers 1 2 3 4 5 Array elements are: 5 4 3 2 1
Here we initialize value of i to the last index of the array, which is N-1. We iterate through the for loop until i value is 0(which is the first index of the array), for each iteration of the for loop we decrement the value of i by 1. Inside for loop we print the value of a[i].
Problem Statement: Write macro definitions with arguments for calculation of Simple Interest and Amount.
Store these macro definitions in a file called “interest.h”. Include this file in your program, and use the macro definitions for calculating simple interest and amount.
Output: Enter principal amount 1000 Enter Rate of Interest 9.2 Enter Time Period 2 Simple Interest: 184.00 Total Amount: 1184.00
In this program we take input for Principal amount, rate of interest and time period from the user, and then calculate Simple Interest for those values and also the total amount accumulated after getting simple interest.
Note: In Simple Interest formula we are dividing by 100.0 because the ( Principal_amount * Rate_of_interest * Time ) might yield a floating / double type value, so if we divide it by integer 100 then it might give wrong result.
Problem Statement: Write down macro definitions for the following:
1. To find arithmetic mean of two numbers. 2. To find absolute value of a number. 3. To convert an upper case alphabet to lower case. 4. To obtain the biggest of three numbers.
#define BIGGEST(a, b, c) ( (a > b && a > c) ? a : ( (b > c) ? b : c ) )
int main()
{
int choice, num, repeat;
float a, b, c;
char ch;
do
{
printf("1. Find Arithmetic Mean of 2 numbers\n");
printf("2. Find Absolute Value of a number\n");
printf("3. Convert a Uppercase letter to lowercase\n");
printf("4. Find Biggest of 3 numbers\n");
printf("\nEnter your choice\n");
scanf("%d", &choice);
switch(choice)
{
case 1: printf("Enter 2 numbers\n");
scanf("%f%f", &a, &b);
printf("Arithmetic Mean: %0.2f\n", MEAN(a, b));
break;
case 2: printf("Enter a integer number\n");
scanf("%d", &num);
printf("Absolute value of |%d| is %d\n", num, ABS(num));
break;
case 3: printf("Enter a uppercase alphabet\n");
fflush(stdin);
scanf("%c", &ch);
if( ch >= 65 && ch <= 90)
printf("To Lowercase: %c\n", LOWER(ch));
else
printf("Enter a valid uppercase alphabet\n");
break;
case 4: printf("Enter 3 numbers\n");
scanf("%f%f%f", &a, &b,&c);
printf("Biggest no is %0.2f\n", BIGGEST(a, b, c));
break;
default: printf("Please enter valid choice\n");
}
printf("\nDo you want to continue? Ans: 0 or 1\n");
scanf("%d", &repeat);
printf("\n");
}while(repeat);
return 0;
}
#include<stdio.h>
#define MEAN(a, b) ( (a + b) / 2.0 )
#define ABS(num) ( (num > 0) ? num : (num * -1) )
#define LOWER(ch) ( ch + 32 )
#define BIGGEST(a, b, c) ( (a > b && a > c) ? a : ( (b > c) ? b : c ) )
int main()
{
int choice, num, repeat;
float a, b, c;
char ch;
do
{
printf("1. Find Arithmetic Mean of 2 numbers\n");
printf("2. Find Absolute Value of a number\n");
printf("3. Convert a Uppercase letter to lowercase\n");
printf("4. Find Biggest of 3 numbers\n");
printf("\nEnter your choice\n");
scanf("%d", &choice);
switch(choice)
{
case 1: printf("Enter 2 numbers\n");
scanf("%f%f", &a, &b);
printf("Arithmetic Mean: %0.2f\n", MEAN(a, b));
break;
case 2: printf("Enter a integer number\n");
scanf("%d", &num);
printf("Absolute value of |%d| is %d\n", num, ABS(num));
break;
case 3: printf("Enter a uppercase alphabet\n");
fflush(stdin);
scanf("%c", &ch);
if( ch >= 65 && ch <= 90)
printf("To Lowercase: %c\n", LOWER(ch));
else
printf("Enter a valid uppercase alphabet\n");
break;
case 4: printf("Enter 3 numbers\n");
scanf("%f%f%f", &a, &b,&c);
printf("Biggest no is %0.2f\n", BIGGEST(a, b, c));
break;
default: printf("Please enter valid choice\n");
}
printf("\nDo you want to continue? Ans: 0 or 1\n");
scanf("%d", &repeat);
printf("\n");
}while(repeat);
return 0;
}
Output: 1. Find Arithmetic Mean of 2 numbers 2. Find Absolute Value of a number 3. Convert a Uppercase letter to lowercase 4. Find Biggest of 3 numbers
Enter your choice 1 Enter 2 numbers 41 14 Arithmetic Mean: 27.50
Do you want to continue? Ans: 0 or 1 1
1. Find Arithmetic Mean of 2 numbers 2. Find Absolute Value of a number 3. Convert a Uppercase letter to lowercase 4. Find Biggest of 3 numbers
Enter your choice 2 Enter a integer number 5 Absolute value of |5| is 5
Do you want to continue? Ans: 0 or 1 1
1. Find Arithmetic Mean of 2 numbers 2. Find Absolute Value of a number 3. Convert a Uppercase letter to lowercase 4. Find Biggest of 3 numbers
Enter your choice 2 Enter a integer number -5 Absolute value of |-5| is 5
Do you want to continue? Ans: 0 or 1 1
1. Find Arithmetic Mean of 2 numbers 2. Find Absolute Value of a number 3. Convert a Uppercase letter to lowercase 4. Find Biggest of 3 numbers
Enter your choice 3 Enter a uppercase alphabet S To Lowercase: s
Do you want to continue? Ans: 0 or 1 1
1. Find Arithmetic Mean of 2 numbers 2. Find Absolute Value of a number 3. Convert a Uppercase letter to lowercase 4. Find Biggest of 3 numbers
Enter your choice 3 Enter a uppercase alphabet $ Enter a valid uppercase alphabet
Do you want to continue? Ans: 0 or 1 1
1. Find Arithmetic Mean of 2 numbers 2. Find Absolute Value of a number 3. Convert a Uppercase letter to lowercase 4. Find Biggest of 3 numbers
Enter your choice 4 Enter 3 numbers 20 40 50 Biggest no is 50.00
Do you want to continue? Ans: 0 or 1 0
Formula and Logic
1. Arithmetic mean: If user enters two numbers, then we add those 2 numbers and divide it by 2 to get the result. If user inputs 3 numbers, we first add all these 3 numbers and divide it by 3 to get mean.
In this program, according to problem statement, we need to add 2 numbers and divide it by 2 to get the mean of those 2 user input numbers.
Since we’re taking 2 floating point numbers, we’re dividing the sum of two numbers by 2.0 and not by integer 2. If we divide float or double number with integer number 2, then there is possibility of getting wrong result.
2. Absolute Value: Absolute value is like distance. In whichever direction you move there can only be positive distance. You can’t walk negative 5 kilometre!
So for any integer input by the user, we return it’s positive value by multiplying it by -1, in case user input number is negative.
Note: We could have used built-in method abs() from the library file stdlib.h to get absolute value of user input number. But to use a single built-in method abs() we must include all the things present in stdlib.h file, so we better write definition to calculate absolute value ourselves.
3. Convert a Uppercase alphabet to lowercase: We should know the ASCII value of A and Z, as well as ASCII value of a and z to get the result.
ASCII value range of upper case alphabets: ASCII value of A is 65. ASCII value of B is 66. ASCII value of C is 67.
and so on till Z ..
ASCII value of Z is 90.
ASCII value range of lower case alphabets: ASCII value of a is 97. ASCII value of b is 98. ASCII value of c is 99.
and so on till z ..
ASCII value of z is 122.
If you observe the ASCII values properly, you’ll know that there is a difference of 32 between a and A in it’s ASCII value. So, if user inputs a capital letter, then we simply add 32 to it and display the character – which will be its corresponding lowercase alphabet.
Note: Since we might start to input information from the keyboard repeatedly inside do-while block, scanf() method keeps checking the input buffer. And often times it gets confused with the input buffer and thinks that the user has pressed the enter key. To avoid that we flush out the previous buffer present in input device(ex: keyboard) using function fflush(). fflush takes stdin as argument, so that it can clear the buffer of standard input device. fflush(stdin);
4. Biggest of 3 Numbers: Here we make use of nested ternary or conditional operator. If a is greater than b and c, then we return value of a. ORELSE if b is greater than c, then we return the value of b, else we return the value of c.
Note: We can continue writing macro expansion in next line by making use of macro continuation operator(\). You can see that we’ve broken the line and written the code in next line inside macro expansion of BIGGEST(a, b, c).
Problem State: Write macro definitions with arguments for calculation of area and perimeter of a triangle, a square and a circle.
Store these macro definitions in a file called “areaperi.h”. Include this file in your program, and call the macro definitions for calculating area and perimeter for different squares, triangles and circles.
1. We need to write macro definitions which accept arguments. 2. We need to write macro definitions to calculate area and perimeter of triangle, square and circle. 3. We need to write macro definition inside a separate file called areaperi.h and then include this header file into our source file and make use of the macro definitions to calculate area and perimeter of Triangle, Square and Circle for various user inputs.
Video Tutorial: Using Macros Find Area and Perimeter of Triangle, Square, Circle: C Program
printf("Area of Trianlge is %0.2f\n", TRI_AREA(a,b,c));
break;
case 2: printf("Enter 3 sides of a Triangle\n");
scanf("%f%f%f", &a, &b, &c);
printf("Perimeter of Trianlge is %0.2f\n", TRI_PERI(a,b,c));
break;
case 3: printf("Enter length of side of Square\n");
scanf("%f", &side);
printf("Area of Square is %0.2f", SQR_AREA(side));
break;
case 4: printf("Enter length of side of Square\n");
scanf("%f", &side);
printf("Perimeter of Square is %0.2f", SQR_PERI(side));
break;
case 5: printf("Enter Radius of Circle\n");
scanf("%f", &radius);
printf("Area of Circle is %0.2f\n", C_AREA(radius));
break;
case 6: printf("Enter Radius of Circle\n");
scanf("%f", &radius);
printf("Circumference of Circle is %0.2f\n", C_PERI(radius));
break;
default: printf("\nPlease enter valid choice\n");
}
printf("\n\nDo you want to continue? Ans: 0 or 1\n");
scanf("%d", &repeat);
printf("\n");
}while(repeat);
return 0;
}
#include<stdio.h>
#include "areaperi.h"
int main()
{
float a, b, c, side, radius;
int choice, repeat;
do
{
printf("1. Area of Triangle\n");
printf("2. Perimeter of Triangle\n");
printf("3. Area of Square\n");
printf("4. Perimeter of Square\n");
printf("5. Area of Circle\n");
printf("6. Perimeter of Circle\n");
printf("\nEnter your choice\n");
scanf("%d", &choice);
switch(choice)
{
case 1: printf("Enter 3 sides of a Triangle\n");
scanf("%f%f%f", &a, &b, &c);
printf("Area of Trianlge is %0.2f\n", TRI_AREA(a,b,c));
break;
case 2: printf("Enter 3 sides of a Triangle\n");
scanf("%f%f%f", &a, &b, &c);
printf("Perimeter of Trianlge is %0.2f\n", TRI_PERI(a,b,c));
break;
case 3: printf("Enter length of side of Square\n");
scanf("%f", &side);
printf("Area of Square is %0.2f", SQR_AREA(side));
break;
case 4: printf("Enter length of side of Square\n");
scanf("%f", &side);
printf("Perimeter of Square is %0.2f", SQR_PERI(side));
break;
case 5: printf("Enter Radius of Circle\n");
scanf("%f", &radius);
printf("Area of Circle is %0.2f\n", C_AREA(radius));
break;
case 6: printf("Enter Radius of Circle\n");
scanf("%f", &radius);
printf("Circumference of Circle is %0.2f\n", C_PERI(radius));
break;
default: printf("\nPlease enter valid choice\n");
}
printf("\n\nDo you want to continue? Ans: 0 or 1\n");
scanf("%d", &repeat);
printf("\n");
}while(repeat);
return 0;
}
Output: 1. Area of Triangle 2. Perimeter of Triangle 3. Area of Square 4. Perimeter of Square 5. Area of Circle 6. Perimeter of Circle
Enter your choice 1 Enter 3 sides of a Triangle 5 6 9 Area of Trianlge is 14.14
Do you want to continue? Ans: 0 or 1 1
1. Area of Triangle 2. Perimeter of Triangle 3. Area of Square 4. Perimeter of Square 5. Area of Circle 6. Perimeter of Circle
Enter your choice 2 Enter 3 sides of a Triangle 5 6 9 Perimeter of Trianlge is 20.00
Do you want to continue? Ans: 0 or 1 1
1. Area of Triangle 2. Perimeter of Triangle 3. Area of Square 4. Perimeter of Square 5. Area of Circle 6. Perimeter of Circle
Enter your choice 3 Enter length of side of Square 5 Area of Square is 25.00
Do you want to continue? Ans: 0 or 1 1
1. Area of Triangle 2. Perimeter of Triangle 3. Area of Square 4. Perimeter of Square 5. Area of Circle 6. Perimeter of Circle
Enter your choice 4 Enter length of side of Square 5 Perimeter of Square is 20.00
Do you want to continue? Ans: 0 or 1 1
1. Area of Triangle 2. Perimeter of Triangle 3. Area of Square 4. Perimeter of Square 5. Area of Circle 6. Perimeter of Circle
Enter your choice 5 Enter Radius of Circle 5.5 Area of Circle is 95.03
Do you want to continue? Ans: 0 or 1 1
1. Area of Triangle 2. Perimeter of Triangle 3. Area of Square 4. Perimeter of Square 5. Area of Circle 6. Perimeter of Circle
Enter your choice 6 Enter Radius of Circle 5.5 Circumference of Circle is 34.56
Do you want to continue? Ans: 0 or 1 0
Formulas To Calculate Area and Perimeter
We’ve written the formula to calculate area and perimeter inside areaperi.h file, as macro expansion. Below we list all the formulas we’re using in our program:
Perimeter of a Triangle: ( a + b + c) where: a, b, and c are lengths of sides of the triangle.
Semi-perimeter of a Triangle: ( (a + b + c) / 2 ) where: a, b, and c are lengths of sides of the triangle.
Area of a Triangle: sqrt( S x (S – a) x (S – b) x (S – c) ) where: a, b, and c are lengths of sides of the triangle. S is the semi-perimeter.
Perimeter of Square: ( 4 x side ) where: side is the length of side of the square.
Area of Square: ( side x side) where: side is the length of side of the square.
Perimeter or Circumference of Circle: ( 2 x PI x r ) Area of a Circle: ( PI x r x r ) where: r is radius of the circle. PI is approximately equal to 3.14
Note: M_PI is a macro present inside math.h library file and has value of PI.
We’ve included areaperi.h into main.c file. Now we can make use of all the macros we’ve defined inside areaperi.h
Users are provided with options to select the preferred operations like: 1. Area of Triangle 2. Perimeter of Triangle 3. Area of Square 4. Perimeter of Square 5. Area of Circle 6. Perimeter of Circle
Based on user selection appropriate block of code inside switch-case is executed, wherein we place the relevant macro template to get the desired result.
Note: We can continue writing macro expansion in next line by making use of macro continuation operator(\). You can see that we’ve broken the line and written the code in next line inside macro expansion of TRI_AREA.
Problem State: Write down macro definitions for the following: 1. To test whether a character is a small case letter or not. 2. To test whether a character is an upper case letter or not. 3. To test whether a character is an alphabet or not. Make use of the macros you defined in 1 and 2 above. 4. To obtain the bigger of two numbers.
1. We need to write 4 Macro definitions. 2. We must write macros to find upper and lower case, and then make use of these two macros to find if user entered character is alphabet or not. 3. We can find biggest of 2 numbers by using Ternary / conditional operator in macro expansion.
Video Tutorial: Using Macros Check For Uppercase / Lowercase and Alphabet or Not and Biggest of 2 Numbers: C Program
printf("1. Check if entered character is upper or lower case\n");
printf("2. Check if entered character is alphabet or not\n");
printf("3. Find biggest of 2 numbers\n");
printf("\nEnter your choice\n");
scanf(" %c", &choice);
switch(choice)
{
case'1': printf("\nEnter a character\n");
scanf(" %c", &ch);
if( isUPPER(ch) )
{
printf("Entered character is upper case letter\n");
}
elseif( isLOWER(ch) )
{
printf("Entered character is lower case letter\n");
}
else
{
printf("Please enter a valid alphabet\n");
}
break;
case'2': printf("\nEnter a character\n");
scanf(" %c", &ch);
if( isALPHABET(ch) )
{
printf("Entered character is an alphabet\n");
}
else
{
printf("Entered character is not an alphabet\n");
}
break;
case'3': printf("\nEnter 2 numbers\n");
scanf("%d%d", &a, &b);
BIGGEST(a, b);
break;
default: printf("\nPlease enter valid choice\n");
}
printf("\n\nDo you want to continue? Ans: 0 or 1\n");
scanf("%d", &repeat);
printf("\n");
fflush(stdin);
}while(repeat);
return 0;
}
#include<stdio.h>
#define isUPPER(ch) ( ch >= 'A' && ch <= 'Z' )
#define isLOWER(ch) ( ch >= 'a' && ch <= 'z' )
#define isALPHABET(ch) ( isUPPER(ch) || isLOWER(ch) )
#define BIGGEST(a, b) ( ( a > b ) ? \
printf("%d is the biggest\n", a) : \
printf("%d is the biggest\n", b) )
int main()
{
int a, b, repeat;
char ch, choice;
do
{
printf("1. Check if entered character is upper or lower case\n");
printf("2. Check if entered character is alphabet or not\n");
printf("3. Find biggest of 2 numbers\n");
printf("\nEnter your choice\n");
scanf(" %c", &choice);
switch(choice)
{
case '1': printf("\nEnter a character\n");
scanf(" %c", &ch);
if( isUPPER(ch) )
{
printf("Entered character is upper case letter\n");
}
else if( isLOWER(ch) )
{
printf("Entered character is lower case letter\n");
}
else
{
printf("Please enter a valid alphabet\n");
}
break;
case '2': printf("\nEnter a character\n");
scanf(" %c", &ch);
if( isALPHABET(ch) )
{
printf("Entered character is an alphabet\n");
}
else
{
printf("Entered character is not an alphabet\n");
}
break;
case '3': printf("\nEnter 2 numbers\n");
scanf("%d%d", &a, &b);
BIGGEST(a, b);
break;
default: printf("\nPlease enter valid choice\n");
}
printf("\n\nDo you want to continue? Ans: 0 or 1\n");
scanf("%d", &repeat);
printf("\n");
fflush(stdin);
}while(repeat);
return 0;
}
Output: 1. Check if entered character is upper or lower case 2. Check if entered character is alphabet or not 3. Find biggest of 2 numbers
Enter your choice 1
Enter a character S Entered character is upper case letter
Do you want to continue? Ans: 0 or 1 1
1. Check if entered character is upper or lower case 2. Check if entered character is alphabet or not 3. Find biggest of 2 numbers
Enter your choice 1
Enter a character s Entered character is lower case letter
Do you want to continue? Ans: 0 or 1 1
1. Check if entered character is upper or lower case 2. Check if entered character is alphabet or not 3. Find biggest of 2 numbers
Enter your choice 1
Enter a character $ Please enter a valid alphabet
Do you want to continue? Ans: 0 or 1 1
1. Check if entered character is upper or lower case 2. Check if entered character is alphabet or not 3. Find biggest of 2 numbers
Enter your choice 2
Enter a character A Entered character is an alphabet
Do you want to continue? Ans: 0 or 1 1
1. Check if entered character is upper or lower case 2. Check if entered character is alphabet or not 3. Find biggest of 2 numbers
Enter your choice 2
Enter a character & Entered character is not an alphabet
Do you want to continue? Ans: 0 or 1 1
1. Check if entered character is upper or lower case 2. Check if entered character is alphabet or not 3. Find biggest of 2 numbers
Enter your choice 3
Enter 2 numbers 14 50 50 is the biggest
Do you want to continue? Ans: 0 or 1 0
Here we are using do-while loop to repeatedly show the user choices: If user enters 1, the choices are shown once again. If the user enters 0, then control exits the do-while loop.
Choice 1: Upper or Lower Case Alphabet
All the characters have ASCII value associated with it in C programming. So internally it checks the ASCII value of user entered character against the ASCII values of “A” to “Z”.
ASCII value range of upper case alphabets: ASCII value of A is 65. ASCII value of B is 66. ASCII value of C is 67.
and so on till Z ..
ASCII value of Z is 90.
So all the ASCII values between 65 and 90 (including 65 and 90) are Capital letter alphabets.
Similarly, below we’ve listed the ASCII values of lower case alphabets.
ASCII value range of lower case alphabets: ASCII value of a is 97. ASCII value of b is 98. ASCII value of c is 99.
and so on till z ..
ASCII value of z is 122.
So all the ASCII values between 97 and 122 (including 97 and 122) are Lower case letter alphabets.
According to our problem statement we need to use the macros we defined for “Choice 1” to evaluate if the user entered character is alphabet or not. So if the user entered character is upper or lower case latter than its alphabet or else its not an alphabet.
When you input some data via console window and hit enter key, the enter key or the new line character gets stored inside input buffer. If you’re accepting a single character from keyboard via scanf() function, often times it gets confused with the input buffer and thinks that the user has pressed the enter key as the input character. We can avoid it in 3 ways:
1. Use double scanf() function, as illustrated in above video tutorial. 2. Use a space before %c inside scanf() method. 3. Use fflush(stdin) before every scanf() method which accepts a single character value.
Note: Since we might start to input information from the keyboard repeatedly inside do-while block, scanf() method keeps checking the input buffer. And often times it gets confused with the input buffer and thinks that the user has pressed the enter key. To avoid that we flush out the previous buffer present in input device(ex: keyboard) using function fflush(). fflush takes stdin as argument, so that it can clear the buffer of standard input device. fflush(stdin);
printf("1. Check if entered character is upper or lower case\n");
printf("2. Check if entered character is alphabet or not\n");
printf("3. Find biggest of 2 numbers\n");
printf("\nEnter your choice\n");
scanf("%c", &choice);
switch(choice)
{
case'1': printf("\nEnter a character\n");
fflush(stdin);
scanf("%c", &ch);
if( isUPPER(ch) )
{
printf("Entered character is upper case letter\n");
}
elseif( isLOWER(ch) )
{
printf("Entered character is lower case letter\n");
}
else
{
printf("Please enter a valid alphabet\n");
}
break;
case'2': printf("\nEnter a character\n");
fflush(stdin);
scanf("%c", &ch);
if( isALPHABET(ch) )
{
printf("Entered character is an alphabet\n");
}
else
{
printf("Entered character is not an alphabet\n");
}
break;
case'3': printf("\nEnter 2 numbers\n");
scanf("%d%d", &a, &b);
BIGGEST(a, b);
break;
default: printf("\nPlease enter valid choice\n");
}
printf("\n\nDo you want to continue? Ans: 0 or 1\n");
scanf("%d", &repeat);
printf("\n");
fflush(stdin);
}while(repeat);
return 0;
}
#include<stdio.h>
#define isUPPER(ch) ( ch >= 'A' && ch <= 'Z' )
#define isLOWER(ch) ( ch >= 'a' && ch <= 'z' )
#define isALPHABET(ch) ( isUPPER(ch) || isLOWER(ch) )
#define BIGGEST(a, b) ( ( a > b ) ? \
printf("%d is the biggest\n", a) : \
printf("%d is the biggest\n", b) )
int main()
{
int a, b, repeat;
char ch, choice;
do
{
printf("1. Check if entered character is upper or lower case\n");
printf("2. Check if entered character is alphabet or not\n");
printf("3. Find biggest of 2 numbers\n");
printf("\nEnter your choice\n");
scanf("%c", &choice);
switch(choice)
{
case '1': printf("\nEnter a character\n");
fflush(stdin);
scanf("%c", &ch);
if( isUPPER(ch) )
{
printf("Entered character is upper case letter\n");
}
else if( isLOWER(ch) )
{
printf("Entered character is lower case letter\n");
}
else
{
printf("Please enter a valid alphabet\n");
}
break;
case '2': printf("\nEnter a character\n");
fflush(stdin);
scanf("%c", &ch);
if( isALPHABET(ch) )
{
printf("Entered character is an alphabet\n");
}
else
{
printf("Entered character is not an alphabet\n");
}
break;
case '3': printf("\nEnter 2 numbers\n");
scanf("%d%d", &a, &b);
BIGGEST(a, b);
break;
default: printf("\nPlease enter valid choice\n");
}
printf("\n\nDo you want to continue? Ans: 0 or 1\n");
scanf("%d", &repeat);
printf("\n");
fflush(stdin);
}while(repeat);
return 0;
}
In above source code we’re making use of fflush(stdin) before every scanf() method which accepts single character. fflush(stdin) flushes out the previous buffer present in input device(ex: Keyboard).
Note: We can continue writing macro expansion in next line by making use of macro continuation operator(\). You can see that we’ve broken the line and written the code in next line inside macro expansion to find biggest of 2 numbers.