How to display all abundant numbers less than a given number - c++

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.

Related

Multiple input in for loop

I have to write a programme in c++ where user enters a number N, then on a second line he enters as many numbers as N, no more. The ouput shoud be the sum of all positive numbers among the entered numbers.
I have to use for loop. Also we have not covered much so far, only if statements.
The code I have tried gives the sum of positive numbers only, but I can't make the programme use N inputs and stop. It either calculates only one or continues as long as user enters numbers.
#include <iostream>
using namespace std;
int main ()
{
int n, sum=0;
cin>> n;
cout<<endl;
cout<<"Enter numbers"<<endl;
for (int i=1; i<=n; i++)
{
cin>>i;
if(i>0)
{sum=sum+i;
}
cout<<sum<<endl;
}
return 0;
}
The problem is that you're using the same variable (i) for looping and input.
for (int i=1; i<=n; i++)
{
cin>>i;
Whatever gets entered in that cin>>i ruins the logic of your program. Add one separate input variable and keep your i for the loop.
Example:
#include <iostream>
int main() {
int n, sum = 0;
std::cout << "How many numbers do you want to enter? \n";
std::cin >> n;
std::cout << std::endl;
std::cout << "Enter numbers: \n";
for(int i = 1; i <= n; i++) {
std::cout << i << ": ";
int input;
if(std::cin >> input) {
if(input > 0) {
sum = sum + input;
}
std::cout << sum << std::endl;
} else
break; // user failed to enter a number
}
}

Rotating an array:Segmentation Fault (SIGSEGV)

Given an array of N size. The task is to rotate array by d
elements where d is less than or equal to N.
Constraints: 1 ≤ T ≤ 200 1 ≤ N ≤ 200 1 ≤ A[i] ≤ 1000
Example input:
1
5
1 2 3 4 5
2
Output
3 4 5 1 2
The program I wrote seems legit but when I tried to run it is giving me segment fault. I even ran the above example I'm getting the correct output.
The source is GeeksforGeeks: Rotating and Array.
#include <bits/stdc++.h>
using namespace std;
int main() {
int test_case, numb, from, arr[200];
cin >> test_case;
while (test_case--) {
cin >> numb;
for (int i = 0; i < numb; i++) {
cin >> arr[i];
}
cin >> from;
for (int i = from; i < numb; i++) {
cout << arr[i] << " ";
}
for (int j = 0; j < from; j++) {
cout << arr[j] << " ";
}
cout << "\n";
}
return 0;
}
What are the changes which my code needs? What can I do to avoid such errors in the future?click_to_see_segment_fault
Just submitted your exact code on Rotating an Array | Geek for Geeks (site given in the question) . It works perfectly , and no run time error was encountered .
Run the following input and will see weird results. The problem is when the from is greater than the number of elements itself. Basically you need to do a check to see whether the from itself is more than the number of elements or not numb.
#include <bits/stdc++.h>
using namespace std;
int main() {
int test_case, numb, from, arr[200];
cout<<"\nEnter number of test cases:";
cin >> test_case;
cout<<"Test cases = "<<test_case<<endl;
while (test_case--) {
cout<<"Enter count of elements:";
cin >> numb;
cout<<"Count= "<<numb<<endl;
cout<<"Enter the elements:";
for (int i = 0; i < numb; i++) {
cin >> arr[i];
}
cout<<"Entered elements are :";
for (int i = 0; i < numb; i++) {
cout << arr[i]<<" ";
}
cout<<"\nHow many times to rotated? :";
cin >> from;
cout<<"\nRotate it "<<from<<" times:\n";
for (int i = from; i < numb; i++) {
cout << arr[i] << " ";
}
for (int j = 0; j < from; j++) {
cout << arr[j] << " ";
}
cout << "\n";
}
return 0;
}
Lets provide the input as follows:
Enter number of test cases:1
Test cases = 1
Enter count of elements:1
Count= 1
Enter the elements:2
Entered elements are :2
How many times to rotated? :2
Rotate it 2 times:
2 32666
You see the mistake? The location where 32666 is a problem here. It could crash too in your case.
#include <bits/stdc++.h>
using namespace std;
int main() {
int test_case, numb, from, arr[200];
cout<< "Enter the numbe of test cases"<<endl;
cin >> test_case;
if(test_case > 200)
{
cout<<"Number of test cases above limit";
return 0;
}
while (test_case--) {
cout<<"START TEST CASE"<<test_case<<endl;
cout<<"Enter the number of elements in the array"<<endl;
cin >> numb;
if(numb > 200)
{
cout<<"Array size more than expected skipping testcase"<<endl;
continue;
}
cout<<"Enter the elements of array"<<endl;
for (int i = 0; i < numb; i++) {
cin >> arr[i];
}
cout<<"Enter the number of rotations"<<endl;
cin >> from;
if(from > numb || from < 0)
{
cout <<"rotation index out of range skipping testcase"<<endl;
continue;
}
for (int i = from; i < numb; i++) {
cout << arr[i] << " ";
}
for (int j = 0; j < from; j++) {
cout << arr[j] << " ";
}
cout << "\n";
}
return 0;
}
Extending #Charlie's answer,
The program checks for numbs ranges and from's range.

How to add some numbers inputed by the user given a specific formula

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;
}

Sum of long integer array in c++

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]

How would I make the program receive user input?

Ok, so, on my last question my program was finding the standard deviation of a set of numbers. Today, my instructor told me that it needs to get multiple numbers from the user. I have no clue on how about going to do this. Any advice on how to do this? Code is accepted. Please and thanks.
#include "stdafx.h" //No Flipping Clue
#include <iostream> //Needed For "cout"
#include <iomanip> //Needed To Round Decimal Points
#include <math.h> //Needed For "sqrt()" And "pow()"
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
//Used To Round The Decimal Points 2 Places
cout << setiosflags(ios::fixed|ios::showpoint);
cout << setprecision(1);
//Declaring
double Numbers[] = {65, 49, 74, 59, 48}; //Work On Making This A User Input
double Mean = 0, Items = 0, Sum = 0, Deviation = 0;
int Counter;
//Finds The Mean Of The Set Of Numbers
for (Counter = 0; Counter < sizeof(Numbers) / sizeof(double); Counter++)
{
for (Counter = 0; Counter < sizeof(Numbers) / sizeof(double); Counter++)
{
Sum += Numbers[Counter]; //Adds All Numbers In Array Together
}
Items = sizeof(Numbers) / sizeof(double); //Gets The Number Of Items In The Array
Mean = Sum / Items; //Finds The Mean
}
//Finds The Standard Deviation
for (Counter = 0; Counter < sizeof(Numbers) / sizeof(double); Counter++)
{
Deviation += pow((Numbers[Counter] - Mean), 2) / Items; //Does Math Things...
}
Deviation = sqrt(Deviation); //Square Roots The Final Product
cout << "Deviation = " << Deviation << endl; //Print Out The Standard Deviation
system("pause");
return 0;
}
A quick google search would have done the job ... C++ Input/Ouput
int number;
cin >> number;
Exemple:
int nbNumbers;
cout << "How many numbers do you need :" << endl;
cin >> nbNumbers;
double numbers[nbNumbers];
for(int i = 0; i < nbNumbers; ++i)
{
cout << "Enter a Number :" << endl;
cin >> numbers[i];
}
there you go:
cout<<"how many numbers you want to enter?";
cin>>n;
double Numbers[n];
cout<<"enter numbers";
for(int i=0;i<n;i++){
cin>>Numbers[i];
}
Here is an example of vector. It takes input from user until user enters -1.
#include <stdio.h>
#include <array>
int main()
{
int number;
vector<int> userInput;
do
{
cout << "Enter a number (-1 to quit): ";
cin >> number;
userInput.push_back(number);
} while (number != -1);
for (vector < int >::iterator it = userInput.begin(); it < userInput.end() - 1; it++)
{
cout << endl << *it;
}
system("pause");
return 0;
}