I know this question asked many times but I am facing different problem in my code, I try to calculate sum of long integers range between 2-15.
Code:
long array[20];
long NUMBERS;
cout << "How many numbers ? : ";
cin >> NUMBERS;
long sum=0;
for (int i = 0; i < NUMBERS;i++){
cout << "Input number " << (i+1) << " : ";
cin >> array[i];
}
cout << "Calculate Sum" << endl;
for (int i = 0; i < NUMBERS;i++){
sum = sum + array[i];
}
cout << "Sum is : " << sum << endl;
When I input these three numbers.
1234567
123456
12345
Output:
Sum is : 1370368
but actual answer is : 3703627.
I try these solutions
summing-large-numbers and sum-of-alternate-elements-of-integer-array but still not get right solution, also how we can solve this problem if user input different number with different ranges.
This isn't about programming, but math...
Hope this helps: http://www.wikihow.com/Add-Large-Numbers
(As a simple example, add 1 and 11. What is the result? 12 or 21?)
my code sum large number with string.
first you enter number of numbers you want to sum(to 25).
and then you enter the number (to 180 'each number').
#include <iostream>
#include <stdlib.h>
using namespace std;
int main (){
int band;
cin >> band;
string string_of_number[25];
for (int i=0; i<band; i++){ //for get all string number
cin >> string_of_number[i];
}
int strings_length[band];
for (int i=0; i<band; i++){ //for get all length of strings
strings_length[i]=string_of_number[i].length();
}
int answer[180];
for(int i=0; i<180; i++){
answer[i]=0;
}
int remaner=0;
int sum=0;
int last=180;
for (int h=0; h<band; h++){ // for sum strings with sum answer one by one
int j=179;
for (int i=strings_length[h]; i>=0; i--){
if(string_of_number[h][i]=='\0'){
i--;
}
sum = (remaner + (string_of_number[h][i]-'0') + answer[j]) %10;
remaner = ((string_of_number[h][i]-'0') + answer[j]) / 10;
answer[j]=sum;
if (i==0 && remaner>0){
j--;
answer[j]+=remaner;
remaner=0;
}
if(last>j)
last=j;
j--;
}
}
for(; last<180; last++){
cout << answer[last];
}
}
It seems that your program assumes all numbers are 7 digit:
1234567
123456[0]
12345[00]
Related
#include <iostream>
using namespace std;
int main()
{
int product;
int n;
int num1;
int num2;
cout << "Enter the number of numbers you'll input: ";
cin >> n;
int numbers[n];
for(int i = 0; i < n; i++){
cout << "";
cin >> numbers[i];
}
for(int i = 0; i < n; i++){
for(int m = 0; m < n; m++){
if(i != m){
product = numbers[m] * numbers[i];
if(product < numbers[m - 1] * numbers[i] && i != m - 1){
product = numbers[m - 1] * numbers[i];
num1 = numbers[m - 1];
num2 = numbers[i];
}
}
}
}
cout << "" << num1 << " " << num2 << "" << endl;
return 0;
}
This is what I've been able to do so far. Sometimes it works fine and sometimes it doesn't. It depends on the order of the numbers. I noticed that the product is the correct value but when I print the two numbers that get multiplied they aren't correct.
I want it to print the two numbers.
In your example you overwrite the product without checking and also you constantly calculate products regardless.
Just go through the Array once, on your way you compare for the smallest two negative numbers and same time for the largest positive numbers. In the end you are left with the factors forming the largest possible product from
either direction, this you only have to compare and automatically you held your factors too.
I want to calculate the sum of digits in each elements in array. The problem is with this code it only calculates the the sum of odd indexes (1,3,5...) in array. And in console it shows some random numbers for even indexes (0,2,4...)
Can anybody tell me what is the problem?
And yes I need to use it as array
Here are output values:
Enter how many numbers you want to calculate sum of digits: 5
Enter those numbers: 12
Enter those numbers: 33
Enter those numbers: 44
Enter those numbers: 22
Enter those numbers: 33
Sum of 0 number is: 4
Sum of 1 number is: 6
Sum of 2 number is: 40
Sum of 3 number is: 4
Sum of 4 number is: 11730950
#include <iostream>
int main(int argc, char** argv)
{
int n;
int temp;
int pom;
cout << "Enter how many numbers you want to calculate sum of digits: ";
cin >> n;
int numbers[n];
int sum[n];
for (int i = 0; i < n; i++)
{
cout << "Enter those numbers: ";
cin >> numbers[i];
}
for (int i = 0; i < n; i++)
{
while (numbers[i] > 0)
{
temp = numbers[i] % 10;
sum[i]+= temp;
numbers[i] = numbers[i]/10;
}
}
for (int i = 0; i < n; i++)
{
cout << "Sum of " << i << " number is: " << sum[i] << endl;
}
return 0;
}
You need to initialize the sum array, like this:
int sum[n] {};
otherwise, the first time you read from an element of sum you have undefined behaviour.
Also, variable length arrays are not part of standard c++. If you don't know the size of the array at compile time, just use a std::vector.
If you absolutely must use an array, then you will need to dynamically allocate it, like this:
int * arr = new int[n]{};
#include <iostream>
using namespace std;
int main() {
int a,temp,sum=0;
cin>>a;
int arr[a];
for(int i=0;i<a;i++)
{
cin>>arr[i];
}
for(int i=0;i<a;i++)
{
sum=0;
while(arr[i]>0)
{
temp=arr[i]%10;
sum+=temp;
arr[i]=arr[i]/10;
}
cout<<sum<<" ";
}
}
i've just learned cpp and want to know is there a shorter way to display a sequential number.
this is my code
#include<iostream>
using namespace std;
int main(){
int n;
cout<<"Input number: ";
cin>>n;
cout<<"The next 5 rows of number is :"<<n+1<<n+2<<n+3<<n+4<<n+5;
}
A simple loop should solve your problem:
int main(){
int n;
cout << "Input number: ";
cin >> n;
cout << "The next 5 rows of number are: ";
for (int i = n + 1; i <= n + 5; i++) {
cout << i << ' ';
}
}
Numbers should be input as:
53,55,57,58,54
Output:
Missing number is 56
Input should be able to be as many numbers as wanted.
This is what I have so far:
#include <iostream>
#include <vector>
#include <string>
using namespace std;
int getMissingNo (int a[], int n)
{
int i, total;
total = (n+1)*(n+2)/2;
for ( i = 0; i< n; i++)
total -= a[i];
return total;
}
int main()
{
int a[] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
cout << "Enter number of numbers in sequence: ";
int numInSeq;
cin >> numInSeq;
cout << endl;
cout << "Enter numbers in sequence: ";
for (int i = 0; i < numInSeq; i++)
{
cin >> a[i];
}
cout << endl;
int miss = getMissingNo(a,numInSeq);
cout << "Missing number: " << miss << endl << endl;
return 0;
}
The only thing I am missing is being able to enter the numbers separated by commas and I need to edit getMissingNo so it can be any sequence of numbers not just one that starts with 1.
Simple solution if you know the range.
For a given range, let S be the sum of all the numbers within that range.
For a given array with a missing number, let MS be the sum of the numbers in this array.
The missing number is S - MS
I am having trouble with translating my algorithm into c++ code that will display all abundant numbers less than the inputted number. I want to create a program that will display all abundant numbers less than an inputted number. For example if I entered a number like 19, the console should print out 12 and 18. The algorithm is this.
Scan through each number
Check whether its an abundant number
If it is an abundant number, then print it out
I am using Microsoft Visual Studios 2013 IDE and a compiler that supports c++11.
This is the code I have so far
#include <iostream>
using namespace std;
int main()
{
int number;
cout << "Enter a number" << endl;
cin >> number;
for (int i = 1; i < number; i++)
{
// Don't know what to put here
}
char inputCharacter;
cin >> inputCharacter;
return 0;
}
I didn't knew, what abundant numbers are, but if you mean this: https://en.wikipedia.org/wiki/Abundant_number
Then this may help you:
#include <iostream>
using namespace std;
int main()
{
int number;
cout << "Enter a number" << endl;
cin >> number;
for (int i = 1; i < number; i++)
{
int sum = 0;
for(int k = 1; k < i; k++)
{
if(i % k == 0)
{
sum += k;
}
}
if(sum > i)
{
cout << i << endl;
}
}
char inputCharacter;
cin >> inputCharacter;
return 0;
}
If you need any explanation about the algorithm, just ask for it.
int number;
cout << "Enter a number" << endl;
cin >> number;
for (int i ==1; i<number; i++)
{
int temp =0;
for (int j ==1; j<i; j++)
{
if (i%j==0)
{
temp+=j;
}
}
if (temp>i)
{
cout<<j;
}
}
this uses modular division and will accumulate all numbers which are factors.
if the above returns 1 its abundant i think... if i got the right idea of what abundancy is.