Functions

closed account (LN7oGNh0)
I am trying to create a function which divides all numbers under 1000 by 5. (I already know how to get all numbers under 1000) The main problem that I am having is how do I specify whether the number is going to an integer or a decimal? I know that floating point numbers are used for decimals... but how do I makes sure that when I run the program, all decimals are not accounted for???

Thank you.
did u try for your program?

if u know how to do

ur float variable will not count the decimal if your answer don't have decimal.
sound weird if u ask this question .
but if you wan your answer only integer
just create with an integer variable

if your answer is 5.6666
and if and only if your variable is integer. your answer will always be 5 only
closed account (LN7oGNh0)
Yes, but I forgot to say that i needed to add up the sum of these numbers. So if 5/3 came up, it would be 1. Basically and instance of a decimal popping up would result int eh calculation of the sum being incorrect. I want the compiler to ignore every time a decimal came up.

Also, I'm trying a different method which involves counting all the numbers by fives up to 1000. Then adding up the sum of these integers. Now, I don't know how to add up the numbers because I have used a for loop.
Last edited on
so your total just create as a int variable , and i not so understand about your question. You can provide your output here to show me as well. for my reference

1
2
3
4
5
6
int total = 0;

//Example
int number1 = 0;
int number2 = 0;
total = number1 / number2;

but then i read your question again. your number should be have array? since you need add up of these numbers
closed account (LN7oGNh0)
Basically I have made a for loop to count all the numbers up 10 1000.
for (unsigned int num = 1000; num > 0; --num)

Now, I want a function that divides all these numbers by 5. After that I want to add all these numbers up. The problem is that when (for example) 1 comes up, 1 divided by 5 equals 0.2 and I only want integers to be included when all the numbers are added up.

Alternatively, I used a for loop to count by fives to 1000.
for (unsigned int num = 0; num < 1000; num += 5)
Now I wan to add every number that is counted. (e.g. 5, 10, 15...)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>

using namespace std;


int main(){

	double total = 0;

	for( int i = 1000 ; i > 1 ; i-- ){
		total += (i/5);
	}

	int total1 = 0 ;
	total1 = total;

	cout << total << endl;
	cout << total1 << endl;

	system( "pause" );
	return 0;
}


i make a simple prototype , hope it's your requirement but i think it's not . haha
so . you should provide a simple output if i made wrong.
closed account (LN7oGNh0)
Thanks thats what I wanted!
Topic archived. No new replies allowed.