Exercise: Create A Function Named Myfunction and Call It Inside Main
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:
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;
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>
static string belowTwenty[] ={"Zero","One", "Two", "Three", "Four", "Five", "Six", "Seven",
"Eight", "Nine",
"Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen",
"Seventeen", "Eighteen", "Nineteen"};
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 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()
{
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: