How to sum digits from integer in c++? [closed] - c++

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I want to sum digits of an integer. If I have 123 I want to sum 1+2+3. I found a program that works well but I don't understand how it works. Can you explain how it works?
#include <iostream>
using namespace std;
int main()
{
int n, sum;
cout << "Enter integer\n";
cin >> n;
sum = n/100 + (n/10)%10 + n%10;
cout << "sum = " << sum << endl;
}
How n/100 generates 1 from 123, (n/10)%10 2 from 123 and n%10 3 from 123

sum = n/100 + (n/10)%10 + n%10;
1) n/100
(n=123)
in this statement 123/100 means ans is = 1
2)(n/10)%10
here (123/10) firstly evaluate and ans is = 12, and then 12%10 gets evaluate and ans is = 2
3)n%10 again 123%10 evaluate ans is 3
then statement becomes
sum = 1 + 2 + 3
Note: % symbol gives remainder

These are very simple maths.
Here, n = 123
100 | 123 | 1
100
_________
23
Therefore the quotient is 1.
The same way, for 123/10,
10 | 123 | 12
10
___________
23
20
___________
3
So, 123/10 = 12. Now for (123/10)%10 = 12%10,
10 | 12 | 1
10
______
2
Therefore, (123/10)%10 = 12%10 = 2.
The same way, 123%10 = 3
Therefore, the answer is: 123/100 + (123/10)%10 + 123%10 = 1 + 2 + 3 = 6
(Note: a % b, here b divides a and returns the remainder)

Related

Need Explanation for C++ output [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
#include <iostream>
int main()
{
int i = 100,sum=0;
for(int i =0; i!=10;++i)
sum+=i;
std::cout<<i<<" "<<sum<<std::endl;
return 0 ;
}
I'm a beginner in C++ , the output of the code is 100 45 . I understand 100 as its block scope but why 45?
As sum is declared in the outer scope and not redefined inside your loop, the loop is operating on the outer sum which means it's value ends up equivalent to the cumulative value of the loop scoped i which would be:
1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 = 45
The i variable is instead redefined in the loop scope (int i=0;), therefore while in the loop block it goes from 0 to 9, but once out of the loop the i variable is taken into account is the one with 100 assigned.
Next time, if you have the tools, I'd recommend stepping through the loop with a debugger and having a look at what the variables and values are doing.
You display i and sum right after it.
At the start of your loop i=100 and as you declare another i in the scope of your loop, when the code goes out of the scope of your loop, it display the value of the first i, that is 100 and sum = 0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 = 45.

How modulus operation works in C++? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
Why do I get this output: 0 1 2 3 0 1 2 3 0 1 after running the code below? Doesn't the modulus operation finds the remainder after division of one number by another?
#include <iostream>
using namespace std;
int main ()
{
for (int i=0; i< 10; ++i)
cout << i % 4 << " ";
}
The answer is correct. '%' mean "reminder". The % operator is remainder operator. The A % B operator actually answer the question “If I divided A by B using integer arithmetic, what would the remainder be?”
dividend = quotient * divisor + remainder
0 % 4 = 0
1 % 4 = 1
2 % 4 = 2
3 % 4 = 3
4 % 4 = 0
5 % 4 = 1
.....
etc..
For negative number...
1 % (-4) = 1
(-2) % 4 = -2
(-3) % (-4) = -3
With a remainder operator, the sign of the result is the same as the sign of the dividend
you can read more at What's the difference between “mod” and “remainder”?
Yes, that's how modulus works. The output is correct.
0 % 4 = 0
1 % 4 = 1
2 % 4 = 2
3 % 4 = 3
4 % 4 = 0
5 % 4 = 1
...
Take the number, remove as many 4's as you can. Whatever is left over is the modulus.
It does.0 / 4 = 0 remainder 01 / 4 = 0 remainder 1and so on.
Modulus operator returns the remainder after dividing the first number with the second one.
0 % 4 = 0
1 % 4 = 1
2 % 4 = 2
3 % 4 = 3
4 % 4 = 0
5 % 4 = 1
6 % 4 = 2
7 % 4 = 3
8 % 4 = 0
9 % 4 = 1

acending repetitious function without array 1 22 333 4444 55555 [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
F is a function that number x has been repeated in an ascending order f(x).
x : 1 2 3 4 5 6 7 8 9 10
f(x): 1 2 2 3 3 3 4 4 4 4
my function gets 'x' and gives 'f(x)' and it has to do it without array but it goes wrong in high numbers.
int main()
{
int n;
cin>>n;
int i=1,a=1;
if(n==1)
cout<<'1';
else{
while(true){
a++;
i=i+a;
if(i>=n)
break;
}
}
cout<<a;
return 0;
}
TL;DR
f(x) = floor(0.5 + sqrt(1 + 8 * (x - 1)) / 2)
Explanation
Well, since this is a mathematical problem, just solve it with math ;)
One thing to notice is the correlation between the table and the triangular numbers:
h(x) = sum(range(1, x)) = x*(x + 1)/2 //triangular number
x 1 2 3 4 5 6 7 8 9 10
f(x) 1 2 2 3 3 3 4 4 4 4
h(f(x)) 1 3 3 6 6 6 10 10 10 10
So how does that help us? Well, we can write a new equation:
h(f(x)) = x | x = max({n | f(n) = f(x)})
And logically for the inverse the following should apply:
h^-1(x) = f(x)
No we've got two options:
Call it a day and just solve the rest via brute-force:
i = 1
sum = 0
while sum < x:
sum += i
i++
return i - 1
Or build our function h^-1(x):
h(x) = y = (x+1)x/2
h^-1(y) = x with h(x) = y
x ^ 2 + x - 2y = 0
solve for x using the quadratic formula:
x = 0.5 +/- sqrt(1 + 8y) / 2
Now this formula still lacks a few things:
we get two results, one of which is negative. We can just throw the negative result away, so +/- turns into +
this formula is 0-based. To be honest, I'm still trying to figure out why. Solution: simply decrement y by 1 to get the proper result
while this formula returns the correct result for the matching numbers, i.e. y = 3 -> x = 3, it returns floating-point numbers for other input, so we'll have to round down appropriately
Putting it together:
f(x) = floor(0.5 + sqrt(1 + 8 * (x - 1)) / 2)
int f(int x) {
return (x * (x + 1)) / 2;
}
int main() {
int n;
cin >> n;
int left = 1, right = n;
while(left < right) {
int mid = left + (right - left) / 2;
int val = f(mid);
if(val >= n) {
right = mid;
}
else {
left = mid + 1;
}
}
cout << left;
return 0;
}
Use binary search. Right now I am in mobile. I will add the explanation later if needed. Let me know if you don't understand anything.

select n number of element from an array [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
There is given array of size > n , we need to select n number of element from an array.
For example : array contain 112 element and n = 50, so select 50 number such that distance between every two selected number is more or less equal (equal distance is not possible of course except for size%n == 0 ).
If anyone suggest any idea that would work .
Example :
array = 1 2 3 4 5
n = 1
output : 1 or any another number depending on proposed algo.
n = 2
output : 1 3 or 2 4 or 1 4...
n = 3
output : 1 3 5
n = 4
output : 1 3 4 5 or 1 2 4 5
n = 5 :
output 1 2 3 4 5
Basically in case of n=1,2,4 there are more then one possible combination so I need to devise an algo which would pick numbers in uequally distributed manner.
One approach would be dividing the number of elements by the number of desired elements in the selection in floating point, and using rounding to determine the index:
double dist = ((double)size) / n;
int *res = new int[n];
for (int i = 0 ; i != n ; i++) {
res[i] = orig[round(dist*i)];
}
For your example of 112 and 50 the value of dist would be 2.24 and the sequence of indexes selected from the array would be
0 0
1 2
2 4
3 7
4 9
5 11
......
45 101
46 103
47 105
48 108
49 110

to print the output prescribed [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
#include<iostream>
#include<math.h>
#include<stdlib.h>
using namespace std;
main()
{
int q,a,b,i,number;
cin>>q;
while(q--)
{
cin>>a>>b;
int a[b];
int number=1;
for(i=0;i<b;i++)
{
if(i==0)
{
a[i]=number;
number++;
}
a[i]=number;
if(number>0)
number=number*-1;
}
}
}
/*the above code i tried a little bit , but it's incomplete and may be incorrect too, you may help
i want to print the sequence as 1,2,-2,3,-3,3,4,-4,4,-4,5,-5,5,-5,5..... till n , where n is size of array in c++?*/
Chef's kid, Junior chef loves playing with different series. Chef, impressed by his son's curiosity, gifts him a special series S on his birthday
S=1,2,-2,3,-3,3,4,-4,4,-4,.............
Now chef, eager to check the intelligence of his son, gives him q queries to solve. Each query consists of two positions a and b, and Junior_chef is required to calculate the sum of all integers from a to b.
Input
The first line of the input contains a single integer q, which denotes the number of queries. Next q lines consist of two integers, a and b.
Output
Print the answer in single line.
Constraints
1<=q<=10 , 1<=a,b<=10^12
Sample Input
1
1 4
Sample Output
4
Explanation : As the series is 1,2,-2,3, therefore the sum will be 4.
Do you mean the following?
#include <iostream>
int main()
{
while ( true )
{
std::cout << "Enter a non-negative number (0-exit): ";
int n = 0;
std::cin >> n;
if ( n <= 0 ) break;
for ( int i = 1; i <= n; i++ )
{
for ( int j = 0; j < i; j++ ) std::cout << ( j % 2 ? -i : i ) << ' ';
}
std::endl( std::cout );
}
return 0;
}
If to enter sequentially 1 2 3 4 5 6 7 0 then the output will be
Enter a non-negative number (0-exit): 1
1
Enter a non-negative number (0-exit): 2
1 2 -2
Enter a non-negative number (0-exit): 3
1 2 -2 3 -3 3
Enter a non-negative number (0-exit): 4
1 2 -2 3 -3 3 4 -4 4 -4
Enter a non-negative number (0-exit): 5
1 2 -2 3 -3 3 4 -4 4 -4 5 -5 5 -5 5
Enter a non-negative number (0-exit): 6
1 2 -2 3 -3 3 4 -4 4 -4 5 -5 5 -5 5 6 -6 6 -6 6 -6
Enter a non-negative number (0-exit): 7
1 2 -2 3 -3 3 4 -4 4 -4 5 -5 5 -5 5 6 -6 6 -6 6 -6 7 -7 7 -7 7 -7 7
Enter a non-negative number (0-exit): 0