• Log in or sign up for an account here: https://www.sythe.org/login

need a hand XD

PsychicFreak

Member
Joined
Jun 6, 2008
Sythe Gold
0
Messages
53
need a hand XD
guys how can i create a c program that will ask for 3 numbers from the user?
btw im using dev-C++
 
need a hand XD
Here is a simple program

Code:
#include <stdio.h>

main() {
   int numbers[3];
   int i;

   for (i = 0; i < 3; i++) {
       printf("Please enter a number");
       scanf("%d", &numbers[i]);
   }

}

In case you're wondering what the "%d" is, you can read up here on different format specifiers. http://www.mekong.net/tech/printf.htm
 
need a hand XD
Here is a simple program

Code:
#include <stdio.h>

main() {
   int numbers[3];
   int i;

   for (i = 0; i < 3; i++) {
       printf("Please enter a number");
       scanf("%d", &numbers[i]);
   }

}

In case you're wondering what the "%d" is, you can read up here on different format specifiers. http://www.mekong.net/tech/printf.htm

is this c or c++?
 
need a hand XD
ohw tnx. then what should i do to calculate the total of the three numbers and the average?

im trying to learn this lesson :P pls help me hehe
 
need a hand XD
is this c or c++?

That's in c.

ohw tnx. then what should i do to calculate the total of the three numbers and the average?

im trying to learn this lesson :P pls help me hehe

You can do two a couple things, loop through the array again adding the numbers, add them right after they input them, or just add them out of the loop. I'll do it the second way. Then average them out after the loop.

Code:
#include <stdio.h>

main() {
   int numbers[3];
   int total = 0;
   double average;
   int i;

   for (i = 0; i < 3; i++) {
       printf("Please enter a number");
       scanf("%d", &numbers[i]);
       total += numbers[i];
   }

   /* sizeof returns the number of bytes in a variable, with this we can get the amount of elements */
   average = total / (sizeof(numbers) / sizeof(numbers[0])); 

   printf("The total is %d.  The average is %f.", total, average);
}
 
need a hand XD
Code:
#include <iostream>
#include <conio.h>
using namespace std;
int main()
{
int num[3];    
// input VVVV
for (int b=0;b<3;b++)
{
    cout << "Enter a number\n";
    cin >> num[b];
}
// computation of total
int total = num[0]+num[1]+num[2];
// computation of average
int average = total/3;
//assuming you want printout of this information?
cout << "The total of your three numbers is " << total << ", and the average is " << average <<"\n";
system("Pause");
 return 0;
}

changing int average to double average would probably be more reasonable, as most averages will contain a fraction. Also I used two different libraries, I'm a pretty simplistic programmer so this might not be the best method. Also I use system("Pause"; at the end to wait for a character.
 
Runescape Gamblingrunescape gambling
Back
Top