Program to Print Corresponding Alphabet for the Given Number

Program to Print Corresponding Alphabet for the Given Number

This program will print the Corresponding Alphabet for the given Number .

Input :

Logic To Print the Alphabets :

  • Get the input number from the user and store it in variable n .
  • By using while loop find the number of alphabets to print by using sum of increasing powers of 26 .
  • We are using 26 because there are only 26 alphabets . which is considered as a 26 digit number system.
  • Now the n variable is subtracted with generated powers of 26 to get the actual alphabet to print .
  • The string variable p is used to store the alphabets to print.
  • n is divided by 26 and the corresponding alphabet for the remainder obtained is stored in p in reverse order .
  • Now the alphabet is obtained and it is Printed .

Program :

#include<iostream>
#include<math.h>                      //for pow() function
using namespace std;
int main()
{
    int n,s=0;                        //n=input number,s=for comparison
    cout<<"Enter the Number : ";
    cin>>n;
    int i=0;                          //i=finding no of chars to print
	while(s<n)
	{   
		i++;                          //this loop finds no.of chars to print
		s=s+(int)pow(26,i);
	}  
	int del=0;                        
	for(int j=0;j<i;j++)
	{
		del=del+(int)pow(26,j);       //del=finds the value to be removed from n
	}
	n=n-del;                          //n is reassigned to print particular alphabet in identified no.of digits
	string p;                         //p=stores the alphabets
	for(int j=0;j<i;j++)
	{
		char c=(char)((n%(26))+65);   //finds the char for resultant number
		p=c+p;                        //concatenates the alphabets in reverse order
		n=n/26;                       //each time n is divided by 26
	}
	cout<<p<<"\n";                    //prints the alphabets
    return 0;
}

 

Suriya27

A Programmer who have his programming skills on C,C++, Java and also interested in Android Development. He follows the famous quotes by Linus Torvalds ” Talk is cheap, Show me the code “ .