How do you calculate the sum of the Integers is between 0 to 1000?
Examples integer is 945: the sum of all digits is 18.
This is my assignment but I don't know where to start and I need your help...
I must use the C++ language do it... hope u all can help me...
Thanks all
10 51058
Here's a hint:
"modulo"
in c++ the % operator
...google that.
Here's a hint:
"modulo"
in c++ the % operator
...google that.
sorry i duno wat u meaning.. if can do the sample teach me... i reali duno how 2 do the programming.. i jus learn from tis years... thanks for your post
Banfa 9,065
Recognized Expert Moderator Expert
Modulo arithmatic is arithmatic that wraps at a given value,
for instance in modulo 4
0 + 1 = 1
1 + 1 = 2
2 + 1 = 3
3 + 1 = 0
18 modulo 4 is 2. This is the remainder when 18 is divided by 4 using only whole numbers
18 / 4 = 4 remainder 2
The C/C++ % operator encompases this as it provides the integer remainder when 1 integer is divided by another
18 % 4 = 2;
DaLeach is (correctly in my opinion) suggesting that you can use the % operator (and interger divide /) to perform your calculation.
In integer arithmatic
18 % 10 = 8;
18 / 10 = 1;
We are not going to write a program for you as you will not learn anything but if you have an attempt at writing a program we will help you with any errors in it.
Write something, make sure it compiles without warnings or ask if there are errors or warnings you don't understand. if it doesn't work post your code and we'll help.
Hi,
Here is the program you wanted but it will work only for unsigned int values that two bytes data bytes. Its an 3 lines program & you can easily understand. -
#include <stdio.h>
-
#include <stdlib.h>
-
-
void main(void)
-
{
-
unsigned int Val = 65234, i=0, Res=0;
-
-
for(i=1;i<1000000;i=(i*10))
-
Res += (Val%i*10)/i;
-
printf("%d\n\n",Res);
-
}
this is a program to a input a natural number and output the sum of its digits but doesn't work for numbers greater than 10.Could someone pls help me. -
#include<iostream.h>
-
#include<conio.h>
-
void main()
-
{
-
clrscr();
-
int x,a,r,sum=0,sum2=0;
-
cout<<"Enter any natural number: ";
-
cin>>x;
-
a=x;
-
while(x!=0)
-
{
-
r=x/10;
-
if(r==0) sum2=sum2+x;
-
else sum=sum+r;
-
x=x/10;
-
-
}
-
cout<<"the sum of digits of "<<a<<"= "<<sum+sum2;
-
getch();
-
}
Banfa 9,065
Recognized Expert Moderator Expert
Replace -
r=x/10;
-
if(r==0) sum2=sum2+x;
-
else sum=sum+r;
-
which I can not see what you expected to do, with
Remove variable sum2
Hi
I think the code will be like this :
If the Input is an Integer then this code will be works fine. -
int sun_dig(int num)
-
{
-
int ctr,rem,sum=0;
-
while(num!=0)
-
{
-
rem=num%10;
-
sum=sum+rem;
-
num=num/10;
-
}
-
return sum;
-
}
the integer is between 0 to 1000. how 2 do sum the digits? examples integer is 945. the sum of all digits is 18. tis is my assigment but i duno how 2 do... i need u all help... my letchurer say must use the C++ language do it... hope u all can help me... tis friday i need pass up. thanks all
Hi,
Now that u have got so many answers, so there is no point in giving any hint. just try this code without using modulus. -
unsigned int Val = 501;
-
-
unsigned char array[15];
-
-
itoa(Val, array, 10);
-
-
Val = 0;
-
-
for(int i=0; i< (strlen(array)); i++)
-
{
-
Val += (array[i]-48);
-
}
-
-
printf("%d\n\n", Val);
Hey i think you can use this following logic.
As you want to add 0-to-1000 where each bit addintion also there. -
total_sum=0;
-
for (input=0;input<=1000;input++) {
-
sum=0; //Every time get reset.
-
-
for (i=0;i<4;i++) {
-
sum=sum+ (input >> (i*4))%(10) ;
-
}
-
total_sum=total_sum+sum;
-
}
Note: total_sum= Sum of all 0-to-1000 with each bit addition.
sum=sum of each number with bit addition [ex.945 will reperesent 18]
>>( 4*i) = will do the bit shift and with modulo it will give the that integer
value.
Ex. for 945 the for loop will executed 3 times
First i=0, No shift so remainder =5
Second i=1, Shift by 4 bit so the Number is now 0094,Remainder gives 4
Third i=2, (094) Shifted by 4 bit so the Number is now 0009,Remainder
gives 9.
Just run the code and see whether its working. :) :D :p
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: Westcoast Sheri |
last post by:
What is the most efficient way of extracting the first two digits in a
string?
The following is wrong for me, because it only gives me the first
instance of two digits together:
$string = ujdk3ca94abc
preg_match("/\d{2}/",$string,$result);
echo "$result";
//prints 94. However, the result I am looking for is 39
|
by: Shreyas Kulkarni |
last post by:
hi there,
recently i have got a problem regarding calculation of sum of digits in
a floating point or precision number. the weird behaviour of
compiler/language is preventing me from calculating the sum of all
digits. the compiler doesnt store the number as given by the user. some
of the precision digits are lost or changed (at will!). so by any
method, i m unable to reliably calculate the sum of provided number;
except i input the...
|
by: Kosio |
last post by:
Hello,
I know of a way to extract digits from a number using the %10 and
divide by 10. But I am wondering if there is an algorithm out there
that does not use a divide by 10 feature.
The reason I ask is that I am programming on a RISC system where
division is quite expensive, and I want to be able to extract the
digits from an integer (the integer will be from 1 to 6 digits long,
and I know how many digits are in the number).
|
by: Luke Wu |
last post by:
Is there a C function that returns the number of digits in an input
int/long?
example:
numdigits(123) returns 3
numdigits(1232132) returns 7
|
by: Kuljit |
last post by:
I am doing Engineering(B.Tech) in Computer Science.
I have a question for which i am struggling to write a C code(program).
It struck me when we were being taught about a program which counts the
number of digits in a given number.
I request to help me out in solving the below said question.
Ask the user to enter a decimal/float number(eg. 32.8952), then count
the number of digits in that number after the decimal point(4 in this
case).
| |
by: Anoop |
last post by:
Hi All
I am getting two different outputs when i do an operation using
string.digits and test.isdigit(). Is there any difference between the
two. I have given the sample program and the output
Thanks for ur inputs
Anoop
|
by: jmcgill |
last post by:
Hello.
Is there a method for computing the number of digits, in a given numeric
base, of N factorial, without actually computing the factorial?
For example, 8! has 5 digits in base 10; 10! has 7 digits in base 10.
Is there a way to compute that information without actually evaluating
the factorial?
|
by: Ulrich Dorda |
last post by:
I need a pytho nscript to read numbers(with loads of digits) from a
file, do some basic math on it and write the result out to another file.
My problem: I don't get python to use more digits:
In order to try this I type:
The normal precision one:
1.2345678912345679000000000e+000
|
by: ocean |
last post by:
Write a program that prompts the user to input an integer and then outputs both the individual digits of the number and the sum of the digits.
For example, it should output the individual digits of 3456 as 3 4 5 6; output the individual digits of 8030 as 8 0 3 0;
output the individual digits of 2345526 as 2 3 4 5 5 2 6; output the individual digits of 4000 as 4 0 0 0.
#include<iostream>
using namespace std;
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
| |
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |