Input a single character and print a message

Asked By 20 points N/A Posted on -
qa-featured

 I need help i want to input a single character and print a message "It is a vowel" if it is a vowel otherwise print message

"It is a constant". By using if-else structure and OR operator only?

SHARE
Best Answer by Bryan Miguel
Best Answer
Best Answer
Answered By 0 points N/A #114147

Input a single character and print a message

qa-featured

#include "stdafx.h"
#include <iostream>
#include <conio.h>
using namespace std;

int _tmain()
{
    char v;
    cout<<"Enter a character: ";
    cin>>v;

    if(v=='a'|| v=='e' || v=='i' || v=='o' || v=='u')
    {
        cout<<"It is vowel";
    }
    else
    {
        cout<<"It is constant";
    }
    getche();
}

Answered By 0 points N/A #114149

Input a single character and print a message

qa-featured

 

It’s an easy task and just follow the instructions and code given below-
 
(Some comments are given for detail explanation of the lines)
 
#include <iostream>
using name space std;
int main()
{
char var;        //declaring a character variable
cout<<""Enter a character value heren"";
cin>>var;                                 //for reading or scanning the input for the character variable
// ternary operator ? :  is used here if-else logic. ? represents if and : represents else
(var == 'a' || var == 'e' || var == 'i' || var == 'o' || var == 'u') ? cout<<""Vowel"" : cout<<""Constant"";
}
 
 
Answered By 0 points N/A #114151

Input a single character and print a message

qa-featured

In C++ if-else is decision Operator. As in real life we have the option and in particular condition we do specific task. Same as IF-ELSE used in programming for taking decisions.

Syntax of if-else

 

void main()

{

   if(condition)

   {

    // if condition is true pointer will run this part of the program

   }

 else

 {

   in false condition pointer will run this part.

 }

}

 your program

#include<iostream.h>

#include<conio.h>

void main()

{

  char ch;

 cout<<"Enter a character";

 cin>>ch;

 if(ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u'||ch=='A'||ch=='E'||ch=='I'||ch=='O'||ch=='U')

  {

    cout<<"its a vowel";

  }

 else

 {

   cout<<"its a consonant";

 }

}

Here I have used OR operator in IF condition .by the use of OR(||) operator compiler run the true part of if condition if any of the given condition is true otherwise ELSE part would be run.

 

Related Questions