C++ Program to check input character is number, alphabet or symbol

 

program to check input character is number, alphabet or symbol in c++ language:

 

 

In this article, you’ll learn to check whether an input is entered by the user from the keyboard is an alphabet, symbol or number in C++ program with example and explain in detail.

As we know that:

  • Alphabet = A to Z or a to z
  • Number = 0 to 9
  • Symbol = ~, @, #, $, %, ^, &, *, (, ), _/_, +, =, >, <, /, ?, etc all character are symbol for this program.

Step by step explanation:

  • First, we create three variables, they are lowercase, uppercase, and alpha
  • Print “ Enter an alphabet “ on the screen
  • User give an input then
  • We Store input which is entered by the user in the alpha variable with the help of predefined function cin>> then
  • We check input character is alphabet or not
  • If alphabet then prints on the screen alphabet.
  • Otherwise, print on the screen, not an alphabet

 

Q: write a program in C++ language to check, input is given by user from the keyboard is alphabet or not

Solution:

#include<iostream>

#include<conio.h>

using namespace std;

int main()

{

 

int lowerCase, upperCase;

char alpha;

cout<<“Enter a character: “;

cin>>alpha;

if((alpha>=’a’ && alpha<=’z’) || (alpha>=’A’&& alpha<=’z’))

cout<<alpha<<” is an alphabet”;

else

cout<<alpha<<” is not an alphabet “;

return 0;

 

}

The output of this program:

Enter a character: s

s is an alphabet

 

Note: if the user enters any number or symbol except alphabet then print on screen “ is not an alphabet” because this programming only checking for alphabet or not.

Q: write a program in C++ language to check whether an input is an alphabet or not using do-while, an input is given by user from the keyboard.

Solution:

#include<iostream>

#include<conio.h>

using namespace std;

int main()

{

 

int lowerCase, upperCase;

char alpha;

do{

cout<<“Enter a character: “;

cin>>alpha;

}

while (!(alpha));

if((alpha>=’a’ && alpha<=’z’) || (alpha>=’A’&& alpha<=’z’))

cout<<alpha<<” is an alphabet”;

else

cout<<alpha<<” is not an alphabet “;

return 0;

 

}

 

The output of this program:

Enter a character: /

/ is not an alphabet

 

Q: write a program in C++ language to check whether an input is an alphabet, number or symbol, an input is given by user from the keyboard.

Solution:

#include<iostream>

#include<conio.h>

using namespace std;

int main()

{

 

int lowerCase, upperCase;

char alpha;

cout<<“Enter a character: “;

cin>>alpha;

if((alpha>=’a’ && alpha<=’z’) || (alpha>=’A’&& alpha<=’z’))

cout<<alpha<<” is an alphabet”;

else if(alpha>=’0′ && alpha<=’9′)

cout<<alpha<<” is a number”;

else

cout<<alpha<<” is a symbol”;

return 0;

 

}

 

The output of this program:

Enter a character: $

$ is a symbol

 

Q: write a program in C++ language to check whether an input is an alphabet, number or symbol using do-while, an input is given by user from the keyboard.

Solution:

#include<iostream>

#include<conio.h>

using namespace std;

int main()

{

 

int lowerCase, upperCase;

char alpha;

do{

cout<<“Enter a character: “;

cin>>alpha;

}

while (!(alpha));

if((alpha>=’a’ && alpha<=’z’) || (alpha>=’A’&& alpha<=’z’))

cout<<alpha<<” is an alphabet”;

else if(alpha>=’0′ && alpha<=’9′)

cout<<alpha<<” is a number”;

else

cout<<alpha<<” is a symbol”;

return 0;

 

}

The output of this program:

Enter a character: 5

5 is a number

You may also learn the following programming:

Leave a Reply

Your email address will not be published. Required fields are marked *

itetrade.com