Print the consonants circularly using C++

In this tutorial, we are going to learn how to print the consonants in the circular fashion in C++.

The input is an alphabet and the program commences from that alphabet and prints till the previous character circularly.

 Number of consonants and vowels




The total number of consonants in the alphabets are: 21

The total number of vowels in the alphabets are: 5

Input:

 w

The input can be any character (consonant/vowel).

char alpha;
cin>>alpha;
char vowels[5]={'a','e','i','o','u'};

In this program, the lowercase alphabets are used.

In the above code, the input alphabet is obtained using the variable alpha and the vowels are stored in the separate variable vowels.

Conditions used in the program:

  1. The program checks whether the input character is the consonant or vowel.
  2. If the entered input is a vowel then the character index is incremented by 1.
  3. The counter is maintained with count=0 and if the count value is less than 20 then those characters are printed.
  4. If the character is ‘z’ then it is printed and the count is incremented.
char temp_var;
for(char ch=alpha;ch<='z',count<=20;ch++){
   for(int i=0;i<5;i++){
      if(ch==vowels[i]){
         ch++; 
      }
   temp_var=ch;
      if(temp_var=='z){
         ch='b';
         count++;
      }
  }
if(count<=20){
  count++; 
  }
}

In the above code, each character is checked with all the vowels if it matches then character position is incremented to the subsequent consonant and printed .

On reaching the end of the alphabet ‘z’ the loop enters the second if statement and it assigns the variable alpha to the new variable temp_var.

If the temp_var is equal to ‘z’ then the character is printed and the alpha is denoted by  ‘b’.

The previous alphabet is ‘a’ which is a vowel so it is assigned as ‘b’.

C++ code to print the consonants circularly :

#include<iostream.h>

using namespace std;
int main(){
char alpha;
cout<<"Enter the start letter in lowercase:"<<"\t"; 
cin>>alpha;
char vowels[5]={'a','e','i','o','u'};
char temp_var;
int count=0;
cout<<"The Consonants in the Circular Fashion:"<<"\n";
  for(char ch=alpha;ch<='z',count<=20;ch++){
      for(int i=0;i<5;i++){
         if(ch==vowels[i]){
           ch++;
         }
   temp_var=ch;
        if(temp_var=='z'){
          cout<<temp_var;
          ch='b';
          count++;
         }
      }
      if(count<=20){
         cout<<ch;
         count++;
      }
  }
}

In the above code, the alpha (input) is ‘w’ and it is a consonant.

  • The variable ch is initialized with the character alpha.
  • Condition segment of the first for loop checks whether it is less than or equal to ‘z’ and the count is less than or equal to 20.
  • The second for loop is for checking the alphabet with the vowels.
  • If statement checks ch is a vowel if so, the ch is incremented.
  • The if statement checks whether the count is less than or equal to 20 and the character is printed.
Output:
Enter the start letter in lowercase: w
The Consonants in the Circular Fashion:
wxyzbcdfghjklmnpqrstv

We hope this tutorial helped you to understand how to print the consonants circularly.

Read more tutorials,


Leave a Reply

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