// TestAppln.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <string>
usingnamespace std;
// Have to declare the function since it's defined after main function.
void vowelconsonant(string s);
// Win32 console application has _tmain as the starting function.
int _tmain(int argc, _TCHAR* argv[])
{
cout<<"Enter a string :";
string s;
// getline is to be used since cin doesn't read once it encounters space
// or end of line. Hence cin won't work if you are giving more than one word.
getline(cin,s);
// function called
vowelconsonant(s);
return 0;
}
void vowelconsonant(string s)
{
int vowelno = 0;
int space = 0;
int consonantno= 0;
int str = s.length();
cout<<"Lenght of the string equals : "<<str;
for (int i=str-1; i >=0; i--)
{
// checks each character of string, In if condition "==" is used and not "=" for comparison.
if ((s.at(i) == 'a')||(s.at(i) == 'e')||(s.at(i) == 'i' )||(s.at(i) == 'o')||(s.at(i) == 'u'))
vowelno = vowelno + 1;
elseif(s.at(i) == ' ')
space = space + 1;
else
consonantno = consonantno + 1;
}
cout << "\n Vowel: " << vowelno;
cout << "\n Space: " << space;
cout << "\n Consonant:" << consonantno;
// I am using cin just because user can see the output till he doesn't press enter.
cin>>s;
}
That probably won't bring the number down to 1 anyway - the square root of 500 is 22.xxxxxx, and that doesn't fulfill if(n % 2 == 0), and it'll just doubling itself.
If you want to deal with decimal numbers with an int argument, you could create a double in the function and copy the int argument into the double and work using that double to make it easier.
// sqrt.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include<iostream>
#include<math.h>
usingnamespace std;
int goto1(double n);
int _tmain(int argc, _TCHAR* argv[])
{
double n;
cout<<"Enter any integer :";
cin>>n;
goto1(n);
return 0;
}
int goto1(double n)
{
int steps = 0;
// I have also declared n<1000 condition to have a upperbound
while (n > 1 && n < 1000)
{
// modulus function requires the argument to be integer
// so we can go the other way to implement it
// modulus is based on the below concept, and so we are
// implementing modulus and can also have double as the operand.
// not to forget use of double is required in sqrt argument.
if ( (n - ((int)(n/2))*2) == 0)
{
n = sqrt(n);
cout<<"\n"<<n;
}
else
{
n = n*2;
cout<<"\n"<<n;
}
steps += 1;
}
cout <<right<< "\n Number of steps: " << steps << endl;
cin>>n;
return 0;
}