C++ :: Calculate Sum Of Integer And Display To User

Mar 4, 2014

A problem that lets the user enter any positive integer, but you do not have to check for this, and then calculates the sum of the digits and displays this to user. For example, if user enters 14503, the outputted sum of the digits would be 13.

Here is my program

#include <iostream>
int main(void) {
int integer, sum=0;
while(integer) {
sum += integer % 10;
integer /= 10;
} cout << "The sum of the integers numbers is " <<sum;
return 0;
}

View 5 Replies


ADVERTISEMENT

C++ :: Write A Program That Uses Arrays To Calculate Sum And Display It

Nov 4, 2013

SO Im supposed to write a program that uses arrays to calculate the sum and display it. I keep getting the error that says the sum has to be initialized.

#include <iostream>
using namespace std;
//Function prototype
int sumArray(int);
const int NUM_ELEMENTS = 5;

[Code] .....

View 1 Replies View Related

C :: Program That Receives One Positive Integer And Display

Dec 28, 2013

Constructing this right aligned half pyramid? The code I had so far is right aligned and they're not in numbers

Write a program that receives one positive integer and display

_________ _ 1
________ 4 2
_______9 6 3
___16 12 8 4
25 20 15 10 5

when the user key in 5.PS: ignore the line

View 3 Replies View Related

C :: Food Calculator Doesn't Calculate - Choice Menus Not Display Properly?

Dec 7, 2013

I'm new to c i've been trying to write code everyday, and recently ive been reading a book and decided to write my own program with what ive learned with it, basicly you get a choice for how many different main courses you would like to have (right now you can only choose 1) and then you can choose which course you would like, it then asks for the amount of guests you would like and calculates how much food you will need to feed the guests. My code compiles but the program doesn't work properly

Code:
#include<stdio.h>

float guests;
float lbs;
int guest_att(guests)
{
puts("how many guests will be attending?");
scanf("%d",& guests);

[Code] .....

and this is how the program runs

Code:
Scotts-MacBook-Pro:learnc scott$ ./wed
how many main course choices will you have?
1

Please make a selection.
1. Chicken
2. Fish
3. Steak
invalid choice

Please make a selection.
1. Chicken
2. Fish
3. Steak
1
how many guests will be attending? 55

You need 0.000000 pounds of of chicken

Please make a selection.
1. Chicken
2. Fish
3. Steak
invalid choice

Please make a selection.
1. Chicken
2. Fish
3. Steak

View 3 Replies View Related

C :: Program That Displays Each Digit Of Integer In English - Zeros Won't Display

Apr 24, 2014

I'm new to C programming and in my first computer science class. Our assignment was to write a program that displays each digit of an integer in English. I wrote the following but cannot figure out why it won't display zeros. When I execute and type in the number 1,000, I get "zero."

Code:
#include <stdio.h>
int main (void)
{
int x, y = 0;
printf ("This program displays each digit of an integer in English.

[Code] ....

View 5 Replies View Related

C :: Take Integer Input For Temperature And Display Message For Different Ranges For Each Day Of Week

Apr 20, 2014

I'm writing the program below to take an integer input for temperature and display a cheesy message for different ranges for each day of the week.

I tried using two if statements, the first to determine if the input is valid, and the else display that it was invalid and to try again.

The second set of if (if else) statements would evaluate a number input and display the cheesy message based on the value, or if it was out of range, to make you re input that days temperature.

I understand that the else statement is linked to the closest if statement, unless brackets are added to prevent this. I thought my brackets were correct, but I keep getting [Error]'else' without previous if'. The red lines are the if and else statement I'd like to be linked, and tried to do so by enclosing the 2nd if statement in brackets.

The assignment doesn't require the error checking, but if I don't use some form of it I believe it will just loop. Again I have to use the last 4 lines due to instructors rules, and have to use scanf and not strings or arrays.

This program will ask the user what the temperature will be for each day of the week. The integer input from the keyboard will be tested in a series of IF tests and a message will be printed depending on the temperature.

Code] ....

#include <stdio.h>
#define MAX 100
#define MIN 0

int main() {
int day = 1;
int temp;

[Code] ....

View 10 Replies View Related

C++ :: Program To Display Error Message If Variable Entered Not An Integer

Nov 8, 2013

I need to have a program display an error message if the variable entered isn't an integer but then I want it to cin again. I have this but it doesn't work:

cout << "Enter an Integer: " ;
for (;;) {
cin >> var;
if (!cin) {

[Code] ....

I am not sure how to do what I want and this doesn't work, it just repeats That wasn't an int.. over and over again.

View 8 Replies View Related

C :: Calculate Value Of PI With The Accuracy (EPS) Given By User

May 23, 2013

I have to calculate the value of PI with the accuracy (eps) given by the user. I started with this but I just got stuck now, I don't know what is going on with the code or why does it get stuck in the second iteration...

PHP Code:

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main(){    
float eps=0.000001; 

[Code] ....

View 4 Replies View Related

C/C++ :: Calculate Result Of 2 Numbers That User Entered

Oct 29, 2012

In this exercise, write a rational number calculator that ask the user to enter two rational number:

a/b + c/d

and produce the result:

(ad+bc)/bd

View 10 Replies View Related

C++ :: Repeat A Question Over And Over And Calculate Total When User Wants To Quit

Feb 27, 2014

Write a program which is used to calculate GPA based on the grades from different courses Input by the user. User should be able to enter as many course unless they choose to quit. Once the user has completed entering the data, the program should be able to provide a feedback on the GPA. Where A=4, B=3, C=2, D=1.

View 1 Replies View Related

C++ :: Calculate GPA - Divide Depending On How Many Times User Inputs A Value

Feb 27, 2014

For example, to calculate GPA, we add all the grade point of a course and divide by the number of courses we are taking. How do I create a program in such a way that when the user enters grade point for two courses, it will add the grade points for the two course and divide by 2. And if another user enters grade points for six courses, it adds the grade points and divides by 6.

View 2 Replies View Related

C/C++ :: Calculate Percentage Of Even Numbers Entered By User In Array

Nov 18, 2014

exercise in c: write a program which calculates % of even numbers entered by a user in an array;

1) check if user hasn't entered float value; if has issue warning;
2) give the option for user to press "stop" to exit program any time.

int c,sk[100],i,sum1=0, sum2=0, rel;
printf ("How many numbers will you enter?
");
scanf ("%d", &c);
printf ("Enter %d numbers (to exit program press - stop)
", c);

[Code] ....

View 1 Replies View Related

C/C++ :: Program That Calculate PI Based On User Input For Accuracy?

May 24, 2014

I am trying to create a program that will calculate pi based on a user input for accuracy. If the user input .3 then when the leibniz infinite sum value at a particular i becomes less then the input of .3 then the loop will exit.

I have looked at a number of examples on the internet but I feel lost. I have put together working code that will infinitely output sums but I need the loop to stop when the sum value is less then the accuracy value.

My question is what is wrong with my while loop, why will it only give me infinite summations? How do I make it so that the loop will exit when my accuracy input is greater then the sum?

int main () {
double accuracy;
cout<<"Give an accurate number." << flush;//looks nice
cin>>accuracy;
int d;//initialize denominator
double pi = 0.0;
while(accuracy < d){

[Code]...

View 8 Replies View Related

Visual C++ :: Program That Take Input From A User And Calculate It In A Do While Loop

May 15, 2013

I'm trying to create a program that will take input from a user and calculate it in a do-while loop. The program does the calculation but the answer is wrong. The loop also doesn't work. The purpose of the program is to see how much an item will cost after a discount is taken off and tax is added.

#include <iostream>
#include <cmath>
using namespace std;
double original_cost;
double discount;
double tax;
double total;
char answer;

[Code]...

View 2 Replies View Related

C :: Iterate Through A User Inputted Integer?

Oct 5, 2014

I need to allow the user to input an integer of any length and print out the word of each number in the integer. So the user would enter "324562" and then the output would be "three two four five six two". I'm struggling to understand the best way to do it in C. I know I need a loop that goes through the int but I don't know how to do it

View 7 Replies View Related

C :: Calculate Different Measurements For A Cylinder - Width Modifier For End User Input

Sep 12, 2013

I have created a program to calculate different measurements for a cylinder. Anyways, the end user has to input 3 variables.

Enter the lower radius: 6
Enter the upper radius: 5
Enter slant height: 2.236

The numbers are what "End User" would input. But I need to make it look like this when said "End User inputs the numbers.

Enter the lower radius: 6
Enter the upper radius: 5
Enter slant height: 2.236

Here is what I have for it, but I don't know what to put where.

printf("
Enter the lower radius: ");
scanf("%f", &lower_rad);
printf("Enter the upper radius: ");
scanf("%f", &upper_rad);
printf("Enter slant height: ");
scanf("%f", &slant_h);

View 1 Replies View Related

C++ :: Calculate Approximate Age Of User Based On Current Date And Their Birthday

Nov 23, 2012

I have an extra credit project where I'm supposed to calculate an approximate the user's age based on the current date and their birthday. My problem is that I am not getting the proper date and time. C++ keeps giving me some random date. I looked up c++ time format on the internet because this is extra credit and not in my book.

I am currently using dev C++ 4.9.9.2. Here is my current code :

// included libraries
#include <cstdlib>
#include <iostream>
#include <iomanip>
#include <time.h>
#define cls system("cls")
#define pauseOutput system("pause")
void printM(int x);
void age(int m, int d, int y, int &yearAge, int &monthAge, int &dayAge);

[Code] ....

View 1 Replies View Related

C++ :: Return The Digit At User Specified Index Of Integer

Oct 10, 2014

Return the digit at the user specified index of an integer. If the integer has n digits and the index is NOT in the range 0 <=index <n return -1 Start the digit numbering at 0. Example, if user input is 4 (index) and the integer equals 123456790 the return value for the function is 5 (start index at 0) ; if user input is 40 (index) and the integer equals 123456790 the return value for the function is -1

#include <iostream>
#include <istream>
#include <cstdlib>
#include <cassert>
#include <string>
using namespace std;
int getIndex(int, int);

[Code] .....

View 4 Replies View Related

C# :: User Monthly Expenses Integer Array?

Nov 19, 2014

Basically, I need to "write a program to simulate tracking monthly expenses" using an integer array into which the user can enter an expense. After that, it needs to loop back and ask them if they want to display their expenses or add another, which is the main thing I'm not sure how to make happen.

namespace Assignment2 {
class Program {
static void Main(string[] args) {
// display welcome message and ask if user wants to update or display monthly expenses
Console.WriteLine("Hello, this is a program to track your monthly expenses.");

[code]....

View 6 Replies View Related

C :: User Input 6 - Array To Display Even Number From 0 - 6

Apr 5, 2013

If the user puts in a 6. I need the array to display the even number from 0 - 6. Right now what it does is it displays the first six even numbers. Okay as I'm writing this I'm starting to see where my problem might be..

Code:
void sumIntegers () {
int arr[50];
int i = 0;
int num = 0;
int sum = 0;

[Code] .....

View 1 Replies View Related

C :: Calculate Cost For Production Of Open Top Cylinders - User Defined Functions

Feb 20, 2013

I have this program I wrote to calculate cost for the production of open top cylinders and how to translate it into user defined functions. Here are the user defined functions I need to use instruct() get_area() comp_area() and print_area_cost() so the program will be in 4 parts.

Code:
#include <stdio.h>
int main()
{double radius,height,cost,single,all;
float surface_area;
int count;
double pi=3.14159265;
printf("Enter the radius of the base in cm^2:

[Code] ......

View 2 Replies View Related

C++ :: User Input - Calculate Sum Of Only Positive Values While Ignoring Negative Numbers

Jun 19, 2014

So I have to make a program that allows the user to enter both positive and negative numbers and the program is suppose to calculate the sum of only the positive values while ignoring the negative values. Also it is to be a sentinel-controlled loop with a number ending the set of values.

View 4 Replies View Related

C++ :: Program That Asks User To Prompt Integer Number

Nov 18, 2013

Write a program that asks the user to prompt an Integer number and then test whether it is Palindrome Numbers or not.

The following are Palindrome numbers:
1.22
2.333
3.67876
4.44444
5.123454321

View 8 Replies View Related

C++ :: Ask User For A Positive Integer Value And Then Prints Out All Perfect Numbers

Sep 17, 2013

Write a program asks the user for a positive integer value and then prints out all perfect numbers from 1 to that positive integer. I have been trying for some time, i found a way to check if its a perfect number or not but could not find a way to prints out all perfect numbers from 1 to that positive integer. I am here so far.

#include<iostream>
#include<iomanip>
using namespace std;
int main(){
int n,i=1,sum=0;
cout<<"Enter a number: ";

[Code] ....

View 4 Replies View Related

C++ :: Write A Program That Prompts The User To Enter Integer?

Nov 8, 2013

Write a program that prompts the user to enter an integer and then displays that integer as a product of its primes and if it is a prime then it should say so? Above is the question I have been given

#include <iostream>
#include <vector>
using namespace std;
int main () {
int num, result;
cout << "Enter A Number " << endl;
system ("pause");
return 0;
}

This is what I have so far, do I have to use a for loop, a while loop or a do loop,

View 7 Replies View Related

C++ :: Add Or Remove Integer To A Chain Of Integers Created By User

Mar 25, 2014

I got everything in this code running except for my remove function. What the project does is adds or removes an integer to a chain of integers created by the user. My add function works the first time but after that if I try to remove or add I believe it is pointing to the improper location and I don't know how to fix this....

Here is my code:

Header:
// adds "number" to the array pointed to by "arrayPtr" of "size".
// Note the size of the array is thus increased.
void addNumber(int *& arrayPtr, int number, int &size);
// removes a "number" from the "arrayPtr" of "size".
// if "number" is not there -- no action

[Code] .....

View 12 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved