0% found this document useful (0 votes)
163 views8 pages

Exercise: Create A Function Named Myfunction and Call It Inside Main

The document contains code snippets and explanations for various C++ exercises: 1. The first exercise creates a function called myFunction and calls it from main. 2. The second exercise defines a greeting string variable and assigns it the value "Hello". 3. The third exercise creates a pointer variable ptr that points to a string variable called food. The rest of the document contains sample inputs and outputs for C++ programs to check prime numbers, print pyramids, reverse strings, convert numbers to words, and check if a number is a power of three.

Uploaded by

Jhay El Gamba
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
163 views8 pages

Exercise: Create A Function Named Myfunction and Call It Inside Main

The document contains code snippets and explanations for various C++ exercises: 1. The first exercise creates a function called myFunction and calls it from main. 2. The second exercise defines a greeting string variable and assigns it the value "Hello". 3. The third exercise creates a pointer variable ptr that points to a string variable called food. The rest of the document contains sample inputs and outputs for C++ programs to check prime numbers, print pyramids, reverse strings, convert numbers to words, and check if a number is a power of three.

Uploaded by

Jhay El Gamba
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Exercise:

Create a function named myFunction and call it inside main().

Answer:
void myFunction(){
cout<< “I just got executed!”;
}
int main(){
myFunction();
return 0;
}

Exercise:

Fill in the missing part to create a greeting variable of type string and assign it the value Hello.

Answer:
string greeting = “Hello”;
Exercise:

Create a pointer variable with the name ptr, that should point to a string variable named food:

Answer:

string food = “Pizza”;


string* ptr = &food;

Assessment Test
1. Write a program in C++ to check whether a number is prime or not. Sample Output: Input a
number to check prime or not: 13 The entered number is a prime number.

Answer:
#include <iostream>
using namespace std;
int main()
{
int num1, ctr = 0;
cout << " Input a number to check prime or not: ";
cin>> num1;
for (int a = 1; a <= num1; a++)
{
if (num1 % a == 0)
{
ctr++;
}
}
if (ctr == 2)
{
cout << " The entered number is a prime number. \n";
}
else {
cout << " The number you entered is not a prime number. \n";
}
}

2. Write a program in C++ to make such a pattern like a pyramid with numbers increased by 1.
Sample Output:

Answer:
#include <iostream>
#include <string>
using namespace std;

int main()
{
int i,j,spc,rows,k,t=1;
cout << " Input number of rows: ";
cin >> rows;
spc=rows+4-1;
for(i=1;i<=rows;i++)
{
for(k=spc;k>=1;k--)
{
cout<<" ";
}
for(j=1;j<=i;j++)
cout<<t++<<" ";
cout<<endl;
spc--;
}
}

3. Write a C++ program to reverse a given string. Example: Sample Input: w3resource Sample
Output: ecruoser3w

Answer:
#include <iostream>
#include <string>
using namespace std;

string reverse_string(string str) {


string temp_str = str;
int index_pos = 0;

for (int x = temp_str.length()-1; x >= 0; x--)


{
str[index_pos] = temp_str[x];
index_pos++;
}
return str;
}
int main()
{
cout << "Original string: w3resource";
cout << "\nReverse string: " <<
reverse_string("w3resource");
return 0;
}

4. Write a C++ program to convert a givern non-negative integer to english words. Example:
Sample Input: 12 Sample Output: Twelve Sample Input: 29 Sample Output: Twenty Nine

Answer:
#include <stdlib.h>
#include <iostream>
#include <string>
#include <vector>

using namespace std;

static string belowTwenty[] ={"Zero","One", "Two", "Three", "Four", "Five", "Six", "Seven",
"Eight", "Nine",
"Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen",
"Seventeen", "Eighteen", "Nineteen"};

static string belowHundred[]={"","", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy",


"Eighty", "Ninety" };
static string overThousand[]={"Hundred", "Thousand", "Million", "Billion" };

string number_to_words_below_hundred(long long int num) {


string result;

if (num == 0) {
return result;
}else if (num < 20) {
return belowTwenty[num];
} else if (num < 100) {
result = belowHundred [num/10];
if (num%10 > 0) {
result += " " + belowTwenty[num%10];
}
}else {
result = belowTwenty[num/100] + " " + overThousand[0];
if ( num % 100 > 0 ) {
result += " " + number_to_words_below_hundred( num % 100 );
}
}
return result;
}

string number_to_words(int num) {


if (num ==0 ) return belowTwenty[num];
vector<string> ret;
for( ;num > 0; num/=1000 ) {
ret.push_back( number_to_words_below_hundred(num % 1000) );
}

string result=ret[0];
for (int i=1; i<ret.size(); i++){
if (ret[i].size() > 0 ){
if ( result.size() > 0 ) {
result = ret[i] + " " + overThousand[i] + " " + result;
} else {
result = ret[i] + " " + overThousand[i];
}
}

}
return result;
}

int main()
{

long long int num = 0;

cout << "\n" << num << " -> " << number_to_words(num) << endl;
num = 12;
cout << "\n" << num << " -> " << number_to_words(num) << endl;
num = 29;
return 0;
}

5. Write a C++ programming to check if a given integer is a power of three or not. Sample Input: 9
Sample Output: true Sample Input: 15 Sample Output: False

Answer:

You might also like