Write a program that asks the user for a number n and prints the sum of the numbers 1 to n

By using our site, you acknowledge that you have read and understand our Cookie PolicyPrivacy Policyand our Terms of Service. The dark mode beta is finally here. Change your preferences any time. Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information. Good afternoon,I was asked to make a function called void printStatistics int n which should print the average, sum, min and max number of n numbers passed in to it.

All I managed to do was create a while loop that asked the user for a number until a negative number was entered, but I seem to have some trouble turning it into a function that asks the user to enter a number n times and then printing out the sum, average, min and max.

This is what I have. From what I understood from your question, you wish to have a function printStatistics that takes the parameter of 'int n' with 'n' corresponding to the amount of numbers the user wishes to enter. If I understood this correctly, then this should do the trick:. Learn more. Asked 1 year, 6 months ago. Active 1 year, 6 months ago.

Viewed 2k times. Hexx Hexx 19 4 4 bronze badges.

Given a number n, count all multiples of 3 and/or 5 in set {1, 2, 3, … n}

Hi there! Welcome to Stack Overflow. If you are seeking help finding a bug, I recommend adding the include statements to complete the code and present a runnable sample, an example of your inputs, the resulting output, and your expected output. Right now you are falling short of the requirement for a minimal reproducible example.

Emt scrubs

A note: You seem to be using value for all of your input. This is overwriting the counter you need to have in order to know when to stop collecting input. You forget to compute averageyou forget first value in sum The initialization of min and max from value is an error. Currently if all values given are over the initial value of minmin will not change and the wrong value will be reported. Also note that average is declared as integer, which is probably not what you want.

Active Oldest Votes. Jake Waggoner Jake Waggoner 80 8 8 bronze badges. Nice one, allow me to edit my code.GitHub is home to over 40 million developers working together to host and review code, manage projects, and build software together. If nothing happens, download GitHub Desktop and try again. If nothing happens, download Xcode and try again.

If nothing happens, download the GitHub extension for Visual Studio and try again. Modify the previous program such that only the users Alice and Bob are greeted with their names. Write a program that asks the user for a number n and prints the sum of the numbers 1 to n sumofnumbers.

Modify the previous program such that only multiples of three or five are considered in the sum, e. Write a program that asks the user for a number n and gives him the possibility to choose between computing the sum and computing the product of 1,…,n.

Write a guessing game where the user has to guess a secret number. After every guess the program tells the user whether their number was too large or too small. At the end the number of tries needed should be printed. I counts only as one try if they input the same number multiple times consecutively. Skip to content. Dismiss Join GitHub today GitHub is home to over 40 million developers working together to host and review code, manage projects, and build software together.

Subscribe to RSS

Sign up. Comment and help me if you want. Python Branch: master. Find file. Sign in Sign up.

C Program to Find Largest and Smallest Number among N Numbers

Go back. Launching Xcode If nothing happens, download Xcode and try again. Latest commit Fetching latest commit…. Write a program that asks the user for her name and greets her with her name.

write a program that asks the user for a number n and prints the sum of the numbers 1 to n

Write a program that prints a multiplication table for numbers up to You signed in with another tab or window. Reload to refresh your session.GitHub is home to over 40 million developers working together to host and review code, manage projects, and build software together. If nothing happens, download GitHub Desktop and try again.

write a program that asks the user for a number n and prints the sum of the numbers 1 to n

If nothing happens, download Xcode and try again. If nothing happens, download the GitHub extension for Visual Studio and try again.

Failed ga mpje

Note: if your programming language does not support arbitrary size numbers, printing all primes up to the largest number you can represent is fine too. After every guess the program tells the user whether his number was too large or too small.

At the end the number of tries needed should be printed.

Subscribe to RSS

I counts only as one try if the user inputs the same number consecutively. Skip to content. Dismiss Join GitHub today GitHub is home to over 40 million developers working together to host and review code, manage projects, and build software together. Sign up. Answers for some simple programing problems with ruby way.

Ruby Branch: master. Find file. Sign in Sign up. Go back. Launching Xcode If nothing happens, download Xcode and try again. Latest commit Fetching latest commit…. You signed in with another tab or window. Reload to refresh your session.

You signed out in another tab or window.Error: You don't have JavaScript enabled. This tool uses JavaScript and much of it will not work correctly without it enabled. Please turn JavaScript back on and reload this page. Please enter a title. You can not post a blank message. Please type your message and try again.

Program to calculate Sum of digits of a given number in c

This discussion is archived. I am supposed to write a program that asks the user for a positive non-zero integer value. The program should use a loop to get the sum of all the integers from 1 up to the number entered. Yesterday I wrote this code: import javax. Any insight to what I am doing wrong would be greatly appreciated. This content has been marked as final.

Show 7 replies. Use a variable called sum, and add i to it every time through the loop. It will be on my next hand written exam, and It seems difficult to remember.

write a program that asks the user for a number n and prints the sum of the numbers 1 to n

The easiest thing to understand is a for loop that runs from 1 to 'number' and each time through the loop adds 'i', which is the loop counter, to a variable called sum. As an exercise, write a for-loop that sums the numbers from and uses System.

Go to original post.Forum Beginners Sum of prime numbers function. Nov 7, at am UTC.

Vutla na vako puti

I need to write a function that gives the sum of prime numbers between 1 and N. Then a program that uses this function to output the sum of prime numbers between 1 and N Im just confused as to how to write the function as prime numbers dont increase in fixed intervals?

I understand this is probably ridiculously simple but I'm just beginning Thank you in advance. To find if a number is prime, you need to check if it has any divisors up to square root of n. You can optimize this algorithm a great deal, but just get it working to start with. Thank you so much! I understand it as Naming the function, asks user to enter a number. I dont understand what 'bool prime' means. Then When x is 2 and less than the chosen number, add 1 to it.

When i is less than the squareroot of x, add 1. If i has no remainder then it is not a prime. If it is a prime, add them. Then asks user to enter number, prints the sum of the numbers is: then names function so program uses the method named in the function to get the answer? Thank you. Here is a version that does not need the math. Eg: If nth value is 5, it will take int i and divide it till i - 1.

Note: Program does take care of division by one. Last edited on Nov 7, at am UTC.In this article, we are going to write a Python code to calculate the sum and average of the positive numbers starting from 1 to the given number n using for loop. Goals of this article : In this article, we will see various Python examples that cover the following.

Output : Run Online. Note: The above program loops from 1 to the number entered by the user and adds all numbers to the variable sum.

In the above program, we calculated the sum and average using loop and range function. For example, you want to calculate the sum and average of any five numbers entered by the user. Read more on how to accept the list as an input from the user. You can also use the while loop to calculate the sum and average of n numbers. Here, n can be 2, 5 or any number. Follow these steps:.

Founder of PYnative. Follow me on Twitter. All the best for your future Python endeavors! Free Coding Exercises for Python Developers. Exercises cover Python Basics, Data structure, Data analytics and more. Did you find this page helpful? Let others know about it.

About Vishal Founder of PYnative. Python Quizzes Free Python Quizzes to solve. Quizzes cover Basics, Data structure and more. Free Topic-specific Quizzes. Total 15 Python Quizzes Each Quiz has around questions.

Show All Quizzes. E-Mail Address.By using our site, you acknowledge that you have read and understand our Cookie PolicyPrivacy Policyand our Terms of Service. The dark mode beta is finally here. Change your preferences any time. Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information. GOAL: Write a program that asks the user for a number n and prints the sum of the numbers 1 to n. The program keeps asking for a number until the user enters 0.

I am able to solve the second problem which is until the user enters 0. I am thinking maybe if I use a loop within a loop I can accomplish this. This is my current code.

Finance 2

I've seen other questions answer this programming question but I wanna know how to specifically print the numbers leading up to the number I entered. This prints out the sum of integers and then the answer, then waits for the next input.

Propellerads smartlink

Example output:. Learn more. Asked 1 year, 9 months ago. Active 1 year ago. Viewed 7k times. Angel Valenzuela Angel Valenzuela 3 3 silver badges 12 12 bronze badges. What you want to do? Active Oldest Votes. Several issues with your code: Your while loop will never end. Its intended purpose is not clear.

write a program that asks the user for a number n and prints the sum of the numbers 1 to n

You are summing 1 instead of i each time in your loop. Your print statement only occurs at the end. You can include it within your loop. You do not need to convert integers to string in order to print them. Amit Kumar Amit Kumar 2 2 silver badges 5 5 bronze badges.

Vlad K. Instead of adding nonsense padding, you could have added an explanation as to why this is the correct answer. Sign up or log in Sign up using Google. Sign up using Facebook. Sign up using Email and Password. Post as a guest Name. Email Required, but never shown. The Overflow Blog. The Overflow How many jobs can be done at home? Featured on Meta. Community and Moderator guidelines for escalating issues via new response….


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *