Skip to content

Ahmet Faruk Bişkinler


CENG102Homework2Q1.java PDF Yazdır e-Posta
(0 votes, average 0 out of 5)
Lessons
Administrator tarafından yazıldı.   
Pazar, 03 Aralık 2006 22:17
Etiketler:

CENG 102 Assignment #2

Assigned: 19/10/05, Wednesday

Due: 26/10/05, Wednesday at 14:00

Homeworks must be delivered at the deadline date and hour.

  1. (5 points)

A number is said to be a perfect number if its factors, including 1 (but not the number itself), sum to the number. For example, 6 is a perfect number, because 6 = 1+2+3. Write a program that lists the perfect numbers between a given boundary. The boundary should be limited the same way as in assignment#1, question 1.

CENG102Homework2Q1.java
  1.  
  2. /*************************************/
  3. /* */
  4. /* AHMET FARUK BİŞKİNLER */
  5. /* */
  6. /* CENG 102 JAVA PROGRAMMING */
  7. /* */
  8. /* LESSON */
  9. /* */
  10. /* COMPUTER ENGINEERING 07010441 */
  11. /* */
  12. /* COPYRIGHT © 2003-2005 */
  13. /* by */
  14. /* AHMET FARUK BİŞKİNLER */
  15. /* */
  16. /* */
  17. /* http://ahbis.blogspot.com */
  18. /* http://afb.picom.com.tr */
  19. /* */
  20. /* All Rights Reserved */
  21. /* */
  22. /*************************************/
  23. import java.util.Scanner;
  24. public class CENG102Homework2Q1 {
  25. public static void main(String args[]) {
  26.  
  27. Scanner gir = new Scanner(System.in);
  28.  
  29. int x1, x2, sum = 1;
  30.  
  31. System.out.println("Enter The First Interger");
  32. x1 = gir.nextInt();
  33.  
  34. while (x1 > 65535 || x1 < 2) {
  35. System.out.printf("You Entered Invalid Number.\n",
  36. "Please Enter x1 < 65535 or x1 > 1 \n\n");
  37.  
  38. System.out.println("Enter The First Interger: ");
  39. x1 = gir.nextInt();
  40. }
  41.  
  42. System.out.println("Enter The Second Interger");
  43. x2 = gir.nextInt();
  44.  
  45. while (x2 < x1 || x2 < 2) {
  46. System.out.printf("You Entered Invalid Number.\n",
  47. "Please Enter x2 < 65535 or x2 > 1 or x2 > x1 \n\n");
  48.  
  49. System.out.println("Enter The Second Interger: ");
  50. x2 = gir.nextInt();
  51. }
  52.  
  53. for (; x1 <= x2; x1++) {
  54. sum = 1;
  55. for (int j = 2; j < x1; j++) {
  56. if (x1 % j == 0) {
  57. sum += j;
  58. }
  59. }
  60. if (sum == x1) {
  61. System.out.println("The Number : " + x1 + " is a"
  62. + " Perfect Number");
  63. }
  64. }
  65. }
  66. }
  67.  
  68.  
  69.  
©
Etiketler:
Son Güncelleme: Cumartesi, 09 Aralık 2006 01:06
 

Yorum ekle


Güvenlik kodu
Yenile