Very new to c++. Making this factorial program, and somehow I've scraped through the code and built something, but the value it returns is always 10 times the correct value. For example, factorial for 5 is computed at 1200 instead of 120, and factorial of 3 is computed at 60. Why the extra unit?
This is the code:
#include <iostream>
using namespace std;
int main() {
int i, j, num;
cout << "Compute factorial: " << endl;
cin >> num;
for (i=1; i<num; i++){
for (j=num; j>1; j--)
num *=(j-i);
cout << num;
}
return 0;
}
All you need is one loop. This way should be much simpler.
#include <iostream>
using namespace std;
int main() {
int i, j=1, num;
cout << "Compute factorial: " << endl;
cin >> num;
for (i=num; i>0; i--){
j *= i;
}
cout << j << endl;
}
for (i=1; i<num; i++){
num *=i;
}
This should be enough
I think you are not quite clear about for loop,and be careful with the change of num
try this piece of code, hope it will help
#include <iostream>
using namespace std;
int main() {
int i, j, num;
cout << "Compute factorial: " << endl;
cin >> num;
int output_num = 1;
for (i=1; i<=num; i++){
output_num *= i;
}
cout << output_num;
return 0;
}
Use this code :
#include<iostream>
using namespace std;
int main()
{
int num,fact=1;
cout<<" Enter the no. to find its factorial:";
cin>>num;
for(int a=1;a<=num;a++)
{
fact=fact*a;
}
cout<<"Factorial of a given Number is ="<<fact<<endl;
return 0;
}
Output :
using two loops for computing factorial is very complex..... and you do not need it.
Try this
#include <iostream>
using namespace std;
int main() {
int i,num;
cout << "Compute factorial: " << endl;
cin >> num;
for(i=num;i>1;--i)
num*=(i-1);
cout << num;
return 0;
}
Related
I'm working on building a loop right now that only shows the end value. However, the code is showing all the integers though.
Here's my code:
#include <iostream>
using namespace std;
int sum = 0;
void findMultiples(int n){
cout <<"Enter a positive integer:"<<endl;
for(int i = 0; i <= n; i++)
if (i % 3 == 0)
cout << "Sum: "<<(sum =sum+i)<<endl;
}
int main() {
int num;
cin>>num;
findMultiples(num);
return 0;
}
You are using for then if then showing output. The for and if scope area is one line without { }, so you are printing and summing at the same time and it is each time in the scope of if statement.
#include<iostream>
using namespace std;
int sum = 0;
void findMultiples(int n){
cout <<"Enter a positive integer:"<<endl;
for(int i = 0; i <= n; i++)
if (i % 3 == 0)
sum =sum+i;
cout << "Sum: "<<sum<<endl;
}
int main() {
int num;
cin>>num;
findMultiples(num);
return 0;
}
Your cout statements are in the wrong places. Try this instead:
#include <iostream>
using namespace std;
int findMultiples(int n){
int sum = 0;
for(int i = 0; i <= n; i++){
if (i % 3 == 0)
sum += i;
}
return sum;
}
int main() {
cout << "Enter a positive integer:" << endl;
int num;
cin >> num;
cout << "Sum: " << findMultiples(num) << endl;
return 0;
}
#include <iostream>
using namespace std;
int main(){
int t;
cin >> t;
while(t-->0){
int n;
int a[n];
int max=0;
cin >> n;
for(int i=0; i<n; i++){
cin >> a[i];
}
max=a[0]+a[1]+a[2];
for(int i=3; i<n; i++){
if((a[i]+a[i-1]+a[i-2])>max){
max=a[i]+a[i-1]+a[i-2];
}
}
// cout << "max = " << max << endl;
if((a[n-1]+a[n-2]+a[0])>max)
{
max=(a[n-1]+a[n-2]+a[0]);
}
if((a[n-1]+a[1]+a[0])>max)
{
max=(a[n-1]+a[1]+a[0]);
}
cout << max <<endl;
}
return 0;
}
This is a simple cpp program for this problem : https://www.codechef.com/COG2020/problems/COG2002#.
When I am adding a print statement, which is reduntant then it gives correct answer. And when I remove it, then I get a wrong answer.
My input is :
2
3
1 2 2
3
1 2 2
Please tell what could be the possible reasons. I have searched google for any explanations but couldn't find any :(
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'm beginner in C++, and I've got a question about a simple sum code in c++.
Here is my code :
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
int n;
int sum;
int arr_i = 0;
cin >> n;
vector<int> arr(n);
while (arr_i != n)
{
cin >> arr[arr_i];
sum += arr[arr_i];
//cout << sum << endl;
if (arr_i == n - 1)
cout << sum;
arr_i++;
}
return 0;
}
The output doesn't print the correct answer without "cout << sum" before the if condition.
How can I solve this problem ?
You forget to initialize sum to 0.
int sum = 0;
As the previous post mentioned, sum was not initialized to 0. In terms of good practices and styles its a good idea to initialize any variables that are modified within a loop right before the loop body so that someone reading the code can easily grasp the context of your variables.
int main()
{
int n;
int sum;
int arr_i;
cin >> n;
vector<int> arr(n);
sum = 0;
arr_i = 0;
while (arr_i != n)
{
cin >> arr[arr_i];
sum += arr[arr_i];
//cout << sum << endl;
if (arr_i == n - 1)
cout << sum;
arr_i++;
}
return 0;
}
or as I prefer a "for" loop...
int main()
{
int n;
int sum;
int arr_i;
cin >> n;
vector<int> arr(n);
for (sum = 0, arr_i = 0; arr_i != n; arr_i++)
{
cin >> arr[arr_i];
sum += arr[arr_i];
//cout << sum << endl;
if (arr_i == n - 1)
cout << sum;
}
return 0;
}
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;
}