• Register
Welcome to Kodlogs Q&A, programming questions and answer website.

Most popular tags

java program python php javascript android c# r spring c mysql arrays python-3 tomcat ggplot2 numpy exception spring-boot jdbc eclipse ssl sockets string x html sql-server python-2 7 java-8 intellij-idea entity-framework macos pandas minecraft ios hibernate csv security json windows ssl-certificate heap-memory scikit-learn winforms illegalstateexception keytool matlab redirect code program junit pip maven ubuntu math facebook jquery cryptography sql asp list loop int net class https fatal-error struts java-applet runtime-error apache-spark pyspark garbage-collection sql-server-2005 for-loop quirks-mode internet-explorer-9 internet-explorer machine-learning keras indentation-error unit-testing android-studio jupyter-notebook virtualenv webclient visual-studio-2010 webpack compiler-errors numpy-ndarray curl vagrant aspnet dataframe nullpointerexception javafx jsp django jpa docker wordpress servlet indentation xcode typeerror scanner sum deployment socketexception git linux loops css util oracle cmd recursion search tcp syntax connection float datetime httpwebrequest main function number import file html5 elasticsearch-5 elasticsearch app-transport-security ios10 ios9 cdn cloudflare wampserver plpgsql salt-stack sysv parent-child android-edittext textview visual-studio-2012 android-workmanager unique-index tsql unity2d unity3d electron theano maven-compiler-plugin pickle assetbundle rstudio bar-chart python-tesseract tomcat6 primefaces solr easymock jvm-crash tomcat7 apache-httpclient-4 web-services woodstox discord jersey-2 prepared-statement resultset sencha-cmd sencha-touch-2 sencha-touch powermock fips apex x509certificate ibm-bpm websphere-7 file-permissions file-io accesscontrolexception grizzly atmosphere slick2d lwjgl informetica twitter-bootstrap-3 jax-rs resteasy spring-mvc spring-security-kerberos spring-security kerberos long-integer mapnik pytorch git-review vpython continuation homebrew xgboost conda visual-studio android-asynctask stack-trace user-interface jaxws-maven-plugin maven-3 maven-2 browser-notsupportedexception google-maps-api-3 google-maps visual-studio-code truststore firebase-authentication aws-organizations amazon-web-services php-7 string-formatting cplusplus visual-studio-2015 net-core net-mvc msbuild extension-methods foreign-keys windows-services react-redux reactjs inputstream facebook-graph-api entity-framework-4 reportingservices-2005 reporting-services mips linear-regression anova deep-learning block-device melt reshape2 floating-point webpack-dev-server javascrip 0-lollipop android-5 statsmodels avx eclipseide javafx-2 php-not-recognized laravel command-line tinyurl classnotfoundexception atom-editor android-emulator android-sdk-tools ionic2 cordova foobar2k tcplistener net-2 net-4 ole-db-provider export-to-excel openrowset windows-10 vagrantfile glmnet jvm-arguments global-variables orm classformatexception eof upstart qt ipv4 graphviz pydot atom curses unityeditor pandoc tesseract xls oledb redux spotify cxf wamp nio stacked prediction headless hyperlink outlook jackson keystore applicationcontext pipeline iis jstl encryption perfect-square objective-c carthage xcode8 indexing standard-deviation sudo xampp postgresql connector apple mdf kubernetes destructor gettime arithmeticexception gmail ioexception heuristics milliseconds reporting disk cpu npm modx-revolution goldsky modx prevnext javascript-dictionary stack-smashing device-monitor radio-button android-actionbaractivity android-activity android-fragments java-long unqualified-id ora-12154 javc c++ java nullpointerexception runtime-error drjava awt-eventqueue dsx math-pow ajquery nosuchelementexception appcompatactivity jsf jtextfield awt jpanel inputmismatchexception glassfish deque jupyter flask lvalue tensorflow gradle servlets netbeans simulation ibm factorial javax apache boot opengl virtualbox jvm margins 2147483647 mongodb cloud firebase plot plugins github processor automation crash card certificate pointers concatenation formatting debugging devices module testing color arraylist node sequence date caching expected build response ip directory algorithms release collections print figure casting url expression integer microsoft sorting sort read types replace js rest ajax exe excel dictionary message required database text variable time size dll system files table runtime static random code web map http error version 0 2
0 votes
10 views
write a program that asks the user for a number n and prints the sum of the numbers 1 to n
by (750 points)  

1 Answer

0 votes

SOLUTION:
import java.util.Scanner;
public class Num{

public static void main(String[] args){

int sum=0;

Scanner sc=new Scanner(System.in);

System.out.println("Enter the number:");

int n=sc.nextInt();

for(int i=1;i<=n;i++){

sum=sum+n;

}

System.out.println("The sum from 1 to"+n+"is:"+sum);

}

}

Example:

input:5
output: The sum from 1 to 5 is:15

by (140 points)  
...