C programming Q1 Write a program that uses a recursive funct

C++ programming:

Q.1 Write a program that uses a recursive function to compute the sum of the squares of all the numbers smaller or equal to a given number. For example, if the function is called with 3, then the function should return 12 +22 +3 2=14.

Q.2 Write a program that takes three strings/words from the user and store them into three string type variables. The program then combines all three words into a single string using an appropriate “string.h” library function and also computes the lengths of the three entered words. The program finally displays the newly constructed string and the lengths on screen.

Q.3 Write a program that takes three composite (Non-prime) positive integers from the user and computes their greatest common divisor (gcd).

Q.4 (challenging problem NOT does not carry points): Write a program that takes few composite (Non-prime) positive integers from the user and computes their greatest common divisor (gcd).

Solution

// C++ code
#include <iostream>
#include <iomanip>
#include <fstream>
#include <vector>
#include <stdlib.h>   
#include <math.h>
#include <string.h>
using namespace std;

// question 1
int sumSquare(int n)
{
if(n == 0)
return 0;
else
return (n)*(n) + sumSquare(n-1);
}

// question 2
void stringfunction()
{
string str1, str2, str3, finalstr= \"\";

cout << \"\ String string1: \";
cin >> str1;
cout << \"String string2: \";
cin >> str2;
cout << \"String string3: \";
cin >> str3;

finalstr = finalstr + str1;
finalstr = finalstr + \" \";
finalstr = finalstr + str2;
finalstr = finalstr + \" \";
finalstr = finalstr + str3;

cout << \"\ Final string: \" << finalstr << endl;
cout << \"String1 length: \" << str1.size() << endl;
cout << \"String2 length: \" << str2.size() << endl;
cout << \"String3 length: \" << str3.size() << endl << endl;
}

// question 3
int gcd(int x, int y)
{
while (x %= y)
{
int t = x;
x = y;
y = t;
}
return y;
}

int main()
{
// question 1
int n;
cout << \"Enter n: \";
cin >> n;
cout << \"sum of the squares of all the numbers smaller or equal to \" << n << \": \" << sumSquare(n) << endl;

// question 2
stringfunction();

// question 3
int x, y, z;
cout << \"\ Enter number1: \";
cin >> x;
cout << \"Enter number2: \";
cin >> y;
cout << \"Enter number3: \";
cin >> z;
int result = gcd(gcd(x, y), z);
cout << \"gcd of \" << x << \" \" << y << \" \" << z << \" is \" << result << endl << endl;

// question 4
int number;
int g;
cout << \"Enter number(-1 when done): \";
cin >> number;
g = number;

while(true)
{
cout << \"Enter number(-1 when done): \";
cin >> number;

if(number == -1)
break;

else
g = gcd(g,number);
}
cout << \"gcd of all numbers: \" << g << endl;
return 0;
}

/*
output

Enter n: 3
sum of the squares of all the numbers smaller or equal to 3: 14

String string1: my
String string2: name
String string3: ayush

Final string: my name ayush
String1 length: 2
String2 length: 4
String3 length: 5


Enter number1: 6
Enter number2: 8
Enter number3: 12
gcd of 6 8 12 is 2

Enter number(-1 when done): 3
Enter number(-1 when done): 6
Enter number(-1 when done): 9
Enter number(-1 when done): 12
Enter number(-1 when done): 15
Enter number(-1 when done): -1
gcd of all numbers: 3

*/

C++ programming: Q.1 Write a program that uses a recursive function to compute the sum of the squares of all the numbers smaller or equal to a given number. For
C++ programming: Q.1 Write a program that uses a recursive function to compute the sum of the squares of all the numbers smaller or equal to a given number. For
C++ programming: Q.1 Write a program that uses a recursive function to compute the sum of the squares of all the numbers smaller or equal to a given number. For

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site