Java Compilation
I want to write a program that takes a positive integer from the keyboard. I want my program to sum all the integers from 1 up to and including the number entered. For example, if the user enters 5, the loop will sum 1+2+3+4+5.
I want the program to make the same calculation three times (using for loop, while loop, and do while loop). The loops should result in the same sum for all three calculations each output to the screen.
Example program run:
Please enter a positive integer: 5
Using a for loop, the sum is = 15
Using a while loop, the sum is = 15
Using a do while loop, the sum is = 15
Here is what i came up with:
import java.util.Scanner;
puplic class Project1
{
public static void main(String[] args)
{
Scanner key = new Scanner(System.in);
System.out.println("Please enter a positive integer: ");
int i = key.nextInt();
if(i < 0)
{
System.out.println("Please reenter a positive integer: ");
i++;
}
for(int b = 0; b >= i; b++)
{
System.out.println("Using the for loop, the sum is = " +i);
b++;
}
while(r>=0)
{
System.out.println("Using the while loop, the sum is =" +i)
r++;
}
do
{
System.out.println("Using the do while loop the sum is = " +i);
x++;
} while(x>=0)
}
}
View Answers
April 14, 2010 at 11:06 AM
Hi Friend,
Try the following code:
import java.util.Scanner;
public class Project1{
public static void main(String[] args){
Scanner key = new Scanner(System.in);
System.out.println("Please enter a positive integer: ");
int i = key.nextInt();
int j=1;
int sum=0;
if(i < 0)
{
System.out.println("Please reenter a positive integer: ");
}
else{
for(j = 1; j <= i; j++)
{
sum+=j;
}
System.out.println("Using the for loop, the sum is = " +sum);
sum=0;
j=1;
while(j<=i)
{
sum+=j;
++j;
}
System.out.println("Using the while loop, the sum is =" +sum);
sum=0;
j=1;
do
{
sum+=j;
j++;
} while(j<=i);
System.out.println("Using the do while loop the sum is = " +sum);
}
}
}
Thanks
Related Tutorials/Questions & Answers:
java Compilation java program
compilation time following error is occur in my system
Note...
java Compilation import
java .util.*;
class ArrayListDemo... in
Java 5 and later if you're using collections without type specifiers (e.g.
java Compilation java program
compilation time following error is occur in my system
Note...
java Compilation import
java .util.*;
class ArrayListDemo... in
Java 5 and later if you're using collections without type specifiers (e.g.
Advertisements
java Compilation java program
compilation time following error is occur in my system
Note...
java Compilation import
java .util.*;
class ArrayListDemo... in
Java 5 and later if you're using collections without type specifiers (e.g.
java compilation error - Appletjava compilation error I am getting
compilation error in the following code.:
public void paint(Graphics g)
{
switch(screen)
{
case 0:
g.drawString(line1,2,10);
break;
case 1
java compilation error - Hibernatejava compilation error hi
i am getting org.hibernate.exception.GenericJDBCException: Cannot open connection exception when runnig the code why this exception is coming
java compilation error - Appletjava compilation error hi friends
the following is the awt front design program error, give me the replay
E:\ramesh>javac...://www.roseindia.net/
java/example/
java/awt/
Thanks
java compilation error,Struts2 - Strutsjava compilation error,Struts2 hi,
From last 3 days i m trying for image uploading through struts2 by taking the reference of file upload sample code from roseindia.net site,But i m unable to insert it into database,its
Java Compilation error - SQLJava Compilation error ava.lang.NullPointerException
at beanUtils.SaleHeaderUtility.getFullRemainPatientList(SaleHeaderUtility.java:754)
at forms.ProcessDialog.updatePatientBalances(ProcessDialog.java:119
java compilation error - Antjava compilation error hi, i have a application in which i m reading from an xml file and then storing its values in database.but when i started using ant its main program is not running as it is unable to detect the jar file
java compilation error - Hibernatejava compilation error hi, i have made an registration page whosevalues should be inserted into database. i have used hibernate and eclipse it is working fine. but when i make a war file and deploy it using ant and then run
java compilation - Java Beginnersjava compilation compiling a program that execute name,surname...);
System.out.println("College/University " + college);
}
}
For
compilation :
c:\>javac Academic.java
c:\>
java Academic
Thanks
compilation error - Java Beginnerscompilation error class s
{
public static... is the reason? Hi Friend,
In
Java there are some specific literal... not in the Hexadecimal or Octal. Here is fraction of
Java code example which will help you
compilation error - Java Beginnerscompilation error sir what is the error inthis code
after... :
C:\>javac -d . Group.java
3)For execute the programm :
C:\>
java... not executing the
java file currectly,
1- put the Employee.java file in c
compilation error - Java Beginnerscompilation error Dear sir,
When i compile some pgms i am getting below error.what may be the reason?
"uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details." Hi Friend
java compilation error - JDBCjava compilation error
/*
*Project:employee.java
*Date:April 04,2007.
*Purpose:To All
Java Developers
**/
package employee;
import javax.swing.*;
import java.awt.*;
import javax.swing.border.
compilation error - Java Beginnerscompilation error i 'm not able to compile a program :
prblem is there is one package in which 2 class person & dog reside. both classes r public still i'm not able to make object of Dog clss in Person class!!
plezz help
compilation error - Java Beginnerscompilation error import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class BLayout implements ActionListener
{
JFrame f ;
JPanel jp;
JButton b[];
CardLayout cl;
BLayout(String s)
{
jp = new JPanel
java compilation error - Java Beginnersjava compilation error Hello madam/sir, i m a beginners in
java. My problem is that i want to connect a login form to a frame containing 4 panels. how could i do it?Please help
Java Compilation - Java BeginnersJava Compilation How do you design a
Java application that inputs a single letter and prints out the corresponding digit on a telephone?
It should be done in such a way that the letters and digits on a telephone are grouped
Java Compilation - Java BeginnersJava Compilation Dear Sir,
Thanks for giving the code for the question which i posted.
I went through the program
"Write a
Java program to read... it as simple
java code.
Thanks
java compilation - Java Beginnersjava compilation import java.awt.*;
import javax.swing.... for
java ActoinButtonsPanel Hi Friend,
Try the following code:
1)
java code:
import java.awt.*;
import javax.swing.*;
public class
Java Compilation - Java BeginnersJava Compilation Dear Sir,
Could you please send me the program code for the following
java program?
Q:Create a classname Math with its required variables and use add(),sub(),multiply(),divide(),modulo() methods?
Thank
Java Compilation - Java BeginnersJava Compilation How would i write a complete
java program which, using a do-while loop, reads integers from the keyboard and sums them until the sum hits or exceeds 30. Then print out the sum.
is it like this:
import
Java Compilation - Java BeginnersJava Compilation I want to write a complete
java program that catenates the file named "first" with the named "second" and produces a file named "third". If either input file "first" or "second" does not exist in the current
Java Compilation - Java BeginnersJava Compilation I want to write a program that takes a positive integer from the keyboard. I want my program to sum all the integers from 1 up to and including the number entered. For example, if the user enters 5, the loop
Java Compilation - Java BeginnersJava Compilation I want to write a
Java program that asks the user for the names of 3 files. I want the first file to be opened for reading and the other two files to be opened for writing.
I want the program to read
Java Compilation - Java BeginnersJava Compilation I want to write a program that takes a positive integer from the keyboard. I want my program to sum all the integers from 1 up to and including the number entered. For example, if the user enters 5, the loop
Java Compilation - Java BeginnersJava Compilation I tried what was given to me but im still having problems.
Here is what i want to do:
Write a program that calculates a customer's monthly bill. It should ask user to enter the letter of the package
java compilation - UMLjava compilation write
java methods to implement queues by the simple but slow method of keeping the front of the queue always in the first position of a linear array
java compilation - UMLjava compilation write
java methods to implement queues in a linear array with two indices front and rear, such that, when rear reaches the end of the array, all the entries are moved to the front of the array
java compilation - UMLjava compilation write a program that accepts a data sequence, generates a fcs (using a known divisor) and then ultimately check wether the recieved data is in error or not Hi Friend,
Please clarify it.
Thanks
JAVA COMPILATION - AjaxJAVA COMPILATION THE BLOCK DIAGRAM BELOW SUMMARITY DESCRIBE THE CRC METHOD.WRITE A PROGRAM THAT ACCEPTS A DATA SEQUENCE, GENERATES A FCS (USING A KNOWN DIVISOR) AND THEN ULTIMATELY CHECK WHETHER THE RECEIVED DATA IS IN ERROR
java compilation error - Java Beginnersjava compilation error can not find symbol
sysbol:method println(int,int,int)
location: class java.io.PrintStream)
System.out.println(+a,+b,+c); Hi friend,
Please, specify in detail what's your problem
Java compilation error - Java BeginnersJava compilation error hai,
now only i have started to work with java.I have been given a task as execute unix codes using eclipse.But i dont know the source codes.Please help me to start the program...
Regards,
R.Punitham
java compilation - UMLjava compilation Write a program that allows the user to enter students' names followed by their test scores and outputs the following information(assume that the maximum number of students in class is 50):
a.class average
JAVA COMPILATION - UMLJAVA COMPILATION A real estate office handles 50 apartment units.When the rent is $600 per month, all the units are occupied. However, for each $40 increase in rent, one unit becomes vacant. Each occupied unit requires
java compilation - Java3Djava compilation A point in the x-y plane is represented by its x-coordinate and y-coordinate.Design the class point that can store and process a point in the x-y plane. you should then perform operations on a point, printig
java compilation - UMLjava compilation Every circle has a center and a radiuus. Given the radius , we can determine the circles area and circumference. Given the center, we can determine its position in the x-y plane. The center of a circle
java compilation - UMLjava compilation In a gymnastics or diving competition, each constestants score is calculated by dropping the lowest and the highest scores and then putting the remaining scores. Write a program that allows the user to enter
java compilation - UMLjava compilation Design and implement the class Day that implements the day of the week in a program. The class Day should store the day, such as Sun for sunday. The program should be able to perform the following operations
java compilation error - Java Beginnersjava compilation error Sir Thanx for ur response for reading a doc file but when i tried to execute ur code
compilation error occurs,
it says --> org.apache.poi.hwpf package does not exist.
-->
java compilation error - Java Beginnersjava compilation error HELLO SIR,I HAVE ASKED QUESTION AND I GOT REPLY FOR THAT ALSO BUT I AM GETING
COMPILATION ERROR UNCHECKED OR UNSAFE OPERATION PLZ RECTIFY THIS. HOW TO READ ELEMENTS FROM TWO ARRAYLIST
AND PRINT
java compilation error - JSP-Servletjava compilation error How to compile a
java Program,If i am having jdk1.5 in webligic it support jdk1.4..When i put a .class file into WEB-INF/classes/Mybeans/Datacon.class it was given a error Hi friend
Java Compilation Error - Java BeginnersJava Compilation Error hi,
i'm chandrakanth.i got a dought regarding below program.
It generates
compilation error.but it executes and generates... for more information.
http://www.roseindia.net/
java/master-
java/index.shtml
java compilation error - Java Beginnersjava compilation error how to set path and class path for
java.. why... of the user defined classes and the packages in a
Java program for the
Java Virtual Machine. Setting the class path is mandatory to run a
java application.
Please
java compilation error - Java Beginnersjava compilation error i have downloaded
java software(version 6... from where i should download
java or what to do now? Have you set the
java_home and path in your environmental variables in "My Computer
Java Compilation error - Java BeginnersJava Compilation error how to put 2 classes on
java program?and what shall i do if symbol not found appear? Hi Friend,
Try the following code:
class Determine{
public int add(int a,int b){
return a+b
Java Compilation error - Java BeginnersJava Compilation error I got this error while convert
java to pdf...
Exception:java.lang.IndexOutOfBoundsException: reserve - incorrect column/size
can somebody told me how to handle this error... Hi Friend
Java Compilation error - Java BeginnersJava Compilation error
i wrote a simple hello world program i get this error every time please help
Exception in thread "main" java.lang.NullPointerException