SE freshman. meed help for basic programming.
Here's the problem
>user entres a positive interger
>add up all the number up to this number
>but dont add any number that is a multiple of any of the digits in the number
example : number 235
add up all the number to 235 esxcept all the multiples of 2, 3 or 5.
also if one of the digits is 0 dont add the multiples of 10 instead of zero
so i have to write this shit in pseudo code and i thought i had solved it, but when i tried to code it to test it didnt work.
any of you guys can help out with this shit.
c++ code :#include <iostream>
#include <vector>
using namespace std;
int main ()
{
cout << "enter a positive integer \n";
int n, i, j, temp, reste, resultat;
bool exclure;
cin >> n;
vector<int> v;
temp = n;
cout << "array contains : \n" ;
while(temp != 0)
{
reste = temp % 10;
temp = temp / 10;
if (reste == 0 ) reste = 10;
v.push_back(reste);
cout << reste << ",\n";
}
resultat = 0;
i = 0;
j = 0;
exclure = false;
cout << "size of array : " << v.size() << "\n";
while ( i <= n)
{
while (j < v.size())
{
if (i % v[j] == 0)
{
exclure = true;
cout << "exclus : multiple of " << v[j] << " | i = " << i << " | j = " << j << "\n";
}
j++;
}
cout << boolalpha << exclure << endl;
if (exclure == false)
{
resultat += i ;
i++;
j = 0;
cout << "somme = " << resultat << endl;
}
exclure = false;
}
cout << "somme : " << resultat << "\n";
}
some of this is in french but you can get the basic idea please help
>>56650740
Works on my machine :^>
>>56650856
sure it runs but its not the ecpected answer
if i enter 9 i should get 36 but i get 45
if i enter 12 i shouds get 0 because all numbers are multiples of one
it doesnt exclude like i want it to but i cant seem to locate the error
>>56650740
>ITT: Stupid people in 4chan will make my homework because i'm lazy and I can't program.
I recommend you to find a job at mc'donals and quit programming my friend...
>>56650740
else j=0
at least figure that much out
If you can't solve this by yourself, then get ready for your life in eternal poverty.
You need to reset j for each number.
Please start using for and for_each loops when you need to iterate over collections.
>>56650936
thanks ill try to solve it with this hint
Holy shit this is an easy problem, just work through it maing..
Int j=0
For (i=1 ; i < input; i++)
{
If (i % 5 !=0 )&&( i % 3 !=0)&&(i%2!=0)
j+=i
}
Cout << j << endl;
Just throw in any other conditionals like the if it has a 0 in it? (So 102 gets skipped?) I didnt quite get that in your OP
>>56651134
Oh i didnt read the OP carefully, just change the numbers in the conditional to match the number from input.
Id just cast the input as a string, break it apart by char, then cast each one back to an int
You should decompose your program into functions so that it reads sort of like pseudo code. That way you can focus on individual parts more easily.
There's no reason to have 10 variables thrown all over multiple nested loops.
I didn't try compiling or running it but here is something you should aim for:#include <iostream>
#include <vector>
using namespace std;
vector<int> decomposeIntoDigits(int n)
{
vector<int> digits;
int digit;
while(n != 0)
{
digit = n % 10;
if (digit = 0)
digit = 10;
digits.push_back(digit);
n = n / 10;
}
}
//is n multiple of at least one number in nums
bool isMultiple(int n, vector<int>& nums)
{
for (int i = 0; i < nums.size(); i++)
{
if (n % nums[i] == 0)
return true;
}
return false;
}
int main()
{
cout << "enter a positive integer \n";
int n;
cin >> n;
vector<int> digits = decomposeIntoDigits(n);
int sum = 0;
for (int i = 1; i <= n; i++)
{
if (!isMultiple(i, digits))
sum += i;
}
cout << "somme : " << sum << "\n";
}
>>56651213
decomposeIntoDigits lacks return digits;
Python3:import sys;
number = input("Number: ");
factorSet = set();
for i in number:
if i in ["1", "2", "3", "4", "5", "6", "7", "8", "9"]:
factorSet.add(int(i));
elif i == "0":
factorSet.add(10);
else:
print("Invalid character: " + i);
sys.exit();
accumulator = 0;
for i in range(1, int(number) + 1):
if 0 not in [i % x for x in factorSet]:
accumulator += i;
print(accumulator);
Guile Scheme:(use-modules (srfi srfi-1) (srfi srfi-26))
(define (foo number)
(let* ((digits (unfold (cute = 0 <>)
(cute remainder <> 10)
(cute quotient <> 10) number))
(curried-remainder (lambda (x) (lambda (y) (remainder x y))))
(pred (lambda (x) (fold (lambda (y p) (and (not (= y 0)) p))
#t
(map (curried-remainder x) digits)))))
(apply + (filter pred (iota number)))))
(display (foo (read)))
>>56652353
Forgot to handle 0.(use-modules (srfi srfi-1) (srfi srfi-26))
(define (foo number)
(let* ((digits (unfold (cute = 0 <>)
(cute remainder <> 10)
(cute quotient <> 10) number))
(curried-remainder (lambda (x) (lambda (y) (remainder x y))))
(pred (lambda (x) (fold (lambda (y p) (and (not (= y 0)) p))
#t
(map (curried-remainder x) (delq 0 digits))))))
(apply + (filter pred (iota number)))))
(display (foo (read)))
>>56650740
You serious?
You literally wrote the whole program in your post.
How can people be this dumb but yet still be able to breathe?