#include <iostream>
using std::cout;
using std::cin;
int main()
{
int num1 = 0;
int num2 = 0;
cout << "Please enter two numbers (remember to put
a space between them):\n";
cin >> num1 >> num2;
const int num_limit = num1 * num2;
for (int i = 1; i <= num_limit; i++)
{
int product = num1 * i;
int product2 = num2 * i;
// test for when multiples are equal
if (product == product2)
{
cout << "The LCM of " << num1 << " and " <<
num2 << " is: ";
}
}
}
I'm trying to get the LCM from two integers which the user enters. The if statement within the for loop isn't doing what I intended it to do, as nothing prints when product and product2 are equal. What is the solution to this little problem?
int gcd(int a,int b)
{
if(b==0)
return a;
else
return gcd(b,a%b);
}
int lcm(int a,int b)
{
//Efficient Solution
// a*b=gcd(a,b)*lcm(a,b)
return (a*b)/gcd(a,b);
}
int main()
{
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int a,b;
cin>>a>>b;
cout<<lcm(a,b);
return 0;
}
Output
15 12
60
Try this out
//#include "stdafx.h"
#include <iostream>
#include <conio.h>
using std::cout;
using std::cin;
int main()
{
int num1 = 0;
int num2 = 0;
cout << "Please enter two numbers (remember to put a space between them):\n";
cin >> num1 >> num2;
int num_limit = num1 * num2,LCM;
for (int i = 1; i <= num_limit; i++)
{
LCM = num1 * i;
// test for when multiples are equal
if (LCM%num2==0) break;
}
cout << "The LCM of " << num1 << " and " << num2 << " is: " << LCM;
_getch();// replace with getch(); if didn't work
}
Related
This is the simple program to convert any base(2-9) to decimal.
It is completely compiled, but the output is not what I expected.
Please let me know what's the problem.
#include <iostream>
#include <cmath>
using namespace std;
int size_int(int num) //the number of num
{
int n = 0;
while(num > 0)
{
num = num/10;
n = n+1;
}
return n;
}
int convert(int num, int b) //conver num base b to base 10
{
int sum = 0;
int n = size_int(num);
while(n>0)
{
sum += (num/pow(10,n-1)) * (pow(b,n-1));
n -= 1;
}
return sum;
}
int main()
{
int num;
cout << "Enter a number to convert: ";
cin >> num;
int b;
cout << "Enter the base of number: ";
cin >> b;
int sum;
sum = convert(num, b);
cout << "Its decimal value is: "<< sum << endl;
return 0;
}
Enter a number to convert: 10110
Enter the base of number: 2
Its decimal value is: 12632
This is the result when I executed.
You calculated your current digit seems not right num/pow(10,n-1).
This is my suggestion:
#include <iostream>
#include <cmath>
using namespace std;
int size_int(int num) //the number of num
{
int n = 0;
while (num > 0)
{
num = num / 10;
n = n + 1;
}
return n;
}
int convert(int num, int b) //conver num base b to base 10
{
int sum = 0;
int n = size_int(num);
int counter = 0;
while (num > 0) {
int temp = num % 10;
num = num / 10;
sum += temp * (pow(b, counter));
counter++;
}
return sum;
}
int main()
{
int num;
cout << "Enter a number to convert: ";
cin >> num;
int b;
cout << "Enter the base of number: ";
cin >> b;
int sum;
sum = convert(num, b);
cout << "Its decimal value is: " << sum << endl;
return 0;
}
So i need to write a program that asks for some numbers from the user (the amount of numbers is determined by the user) and then add them given this formula: ANSWER = FIRST - SECOND + THIRD - FIFTH + ...
where FIRST, SECOND, etc are the first, second and the rest of the numbers input by the user.
The problem is that i can create a loop that stores the numbers but actually, it only updates the value of the "num" variable. This is the code i have written.
#include <iostream>
using namespace std;
int main() {
int num, counter;
double answer;
cout << "Enter integer count: ";
cin >> counter;
for (int i = 0; i < counter; i++) {
cout << "Enter number " << i + 1 << endl;
cin >> num;
}
return 0;
}
Inserting a if-else clause that controls the remainder of the integer division of index i by 2 you can separate even and odd cases to obtain the desidered effect
#include <iostream>
using namespace std;
int main() {
int num, counter;
double answer;
cout << "Enter integer count: ";
cin >> counter;
for (int i = 0; i < counter; i++) {
cout << "Enter number " << i + 1 << endl;
cin >> num;
if(i%2==0)
answer+=num;
else
answer-=num;
}
return 0;
}
You can also do this assuming you don't need to store the numbers input by the user. What I am basically doing is just toggling between +1 and -1 that I then multiply by the number input by the user and then straightforwardly adding it to the answer.
#include <iostream>
#include<cmath>
using namespace std;
int main()
{
int num, counter;
double answer = 0;
cout << "Enter integer count: ";
cin >> counter;
for (int i = 0; i < counter; i++) {
cout << "Enter number " << i + 1 << endl;
cin >> num;
answer += num*pow(-1, i);
}
cout<<answer;
return 0;
}
You can also do:
#include <iostream>
#include<cmath>
using namespace std;
int main()
{
int num, counter;
double answer = 0;
cout << "Enter integer count: ";
cin >> counter;
for (int i = 0; i < counter; i++) {
cout << "Enter number " << i + 1 << endl;
cin >> num;
if(i%2 == 0)answer += num;
else answer -= num;
}
cout<<answer;
return 0;
}
I cannot seem to figure out how to make this work any help would be greatly appreciated as i have been trying for days :(
#include <iostream>
#include <math.h>
using namespace std;
int input;
int sum;
int number1;
int number2;
int number3;
void isArmstrong (int input, int sum)
{
if (input == sum)
cout << input << " is an Armstrong number" << endl;
if (input != sum)
cout << input << " is not an Armstrong number" << endl;
}
cubeOfDigits does not return input and sum to isArmstrong, (return input,sum) error is as follows: expression result unused [-Wunused-value]
int cubeOfDigits (int input, int sum, int number1, int number2, int number3)
{
cout << "Enter an integer between 0-999" << endl;
cin >> input;
number1 = input / 100;
number2 = input % 100;
number3 = number2 % 10;
sum = pow(number1, 3) + pow(number2, 3) + pow(number3, 3);
isArmstrong(input, sum);
return input,sum;
}
main calls cubeOfDigits
int main(void)
{
cout << "Welcome" << endl;
cubeOfDigits(input, sum, number1, number2, number3);
return 0;
}
I think what you intended was..
#include <iostream>
#include <math.h>
using namespace std;
int input;
int sum;
int number1;
int number2;
int number3;
void isArmstrong(int input, int sum){
if(input == sum)
cout << input << " is an Armstrong number" << endl;
if(input != sum)
cout << input << " is not an Armstrong number" << endl;
}
int cubeOfDigits (int input)
{
number1 = input/100;
number2 = input % 100;
number3 = number2 % 10;
number2 = number2/10;
sum = pow(number1,3) + pow(number2,3) + pow(number3,3);
return sum;
}
int main(void){
cout << "Welcome" << endl;
cout << "Enter an integer between 0-999" << endl;
cin >> input;
isArmstrong(input,cubeOfDigits(input));
return 0;
}
change the cubeofdigits function from (int) cubeofdigits to (void) cubeofdigits and then remove the return statement from the end of cubeofdigits function .then it must work.
I need to make ask the user for how many numbers of the Fibonacci sequence displayed to the screen. I have everything done pretty much, but since i am printing two numbers to the screen at a time, it is printing double of what the user enters. I would just divide the number the user enters by two, but the wont work for odd numbers. If anyone can think of a solution for this, thank you. http://en.wikipedia.org/wiki/Fibonacci_number <-- the fibonacci number if you dont know it
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
cout << "How many numbers do you want generated in the Fibonacci sequence.\n";
int num;
cin >> num;
int num1 = 0, num2 = 1;
int sum;
int f = 1;
while (f <= num)
{
cout << num1 << setw(5) << num2 << setw(5);
sum = num1 + num2;
num1 = sum;
num2 = sum + num2;
++f;
}
}
Loop something like:
while ( f <= num )
{
cout << num1 << setw(5);
if ( ++f <= num )
{
cout << num2 << setw(5);
/// rest of calcs
++f;
}
}
Since you're generating two numbers at a time, you need to increment your counter 2 at a time:
int f = 0;
for (; f < num; f += 2) {
// body
}
And then if num is even, print the last one
if (f == num) {
// print num1
}
In a Fibonacci series, you need a minimun of 2, anything less has no meaning. More than 2 you just have to print the sum.
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
cout << "How many numbers do you want generated in the Fibonacci sequence.\n";
int num;
cin >> num;
int num1 = 0, num2 = 1;
int sum;
cout << num1 << setw(5) << num2 << setw(5);
int f = 3;
while (f <= num)
{
sum = num1 + num2;
cout << sum << setw(5);
num1 = num2;
num2 = sum;
++f;
}
cout << std::endl;
}
I would like to analyze the complexity of my code algorithm.Therefore,i must have 2 different programs giving the same functions to allow me to start off.
Currently this is my own code.
I'm not sure if it is allowed that i would like to have someone that could volunteer his own way code to compute summation of factorial for me as the 2nd program code.
Preferrably a nested loop.
#include <iostream>
using namespace std;
int main()
{
int val;
int i;
int a = 0;
int c = 1;
cout << "Please enter a number: ";
cin >> val;
cout << endl;
for (i = 1; i <= val; i++)
{
c = c * i;
a = a + c;
}
cout << "The sum of the factorials is " << a << endl;
system("pause");
return 0;
}
#include <iostream>
using namespace std;
int main()
{
int val;
cout << "Please enter a number: ";
cin >> val;
cout << endl;
static const int results[] = {
0, 1, 3, 9, 33, 153, 873, 5913, 46233, 409113,
4037913, 43954713, 522956313
};
cout << "The sum of the factorials is " << results[val < 0 ? 0 : val] << endl;
system("pause");
return 0;
}
Note that I replicated the defect in the original program which causes it to return the incorrect value if the user enters 0.
This alternate version assumes 32-bit integers because it takes advantage of overflow behavior. Extending to 64-bit integers is left as an exercise.
I do not understand what you do with another nested way but i hope this can help...
#include <iostream>
using namespace std;
int main()
{
int val;
int i;
int a = 0;
int c = 1;
cout << "Please enter a number: ";
cin >> val;
cout << endl;
for (i = 1; i <= val; i++){
c *= i;
a += c;
}
int c2=1;
for (i = val; i > 1; i--){
c2*=i;
c2++;
}
cout << "The sum of the factorials is " << a << endl;
cout << "The sum of the factorials is " << c2 << endl;
system("pause");
return 0;
}
#include <iostream>
using namespace std;
int main()
{
int suma = 0;
int n = 0;
cout << "Sum of factorials\n";
cout << "-------------------------------\n";
cout << "Insert number of n: ";
cin >> n;
int i = 1;
while (i <= n)
{
int factorial = 1;
for(int j=1; j<=i; j++)
{
factorial = factorial * j;
}
suma += factorial;
i++;
}
cout << "Sum of factorials is: " << suma;
system("pause");
return 0;
}