楼主: ReneeBK
2252 19

Hari Mohan Pandey:Java Programming [推广有奖]

11
ReneeBK 发表于 2015-5-31 04:26:55
  1. /*PROG 4.10 MAXIMUM OF N ELEMENTS */
  2. import java.io.*;
  3. import java.util.*;
  4. class JPS13
  5. {
  6.    public static void main(String args[])
  7.    {
  8.           int n, max, t=1, m;
  9.           Scanner sc = new Scanner(System.in);
  10.           System.out.println("Enter how many numbers");
  11.           n = sc.nextInt();
  12.           System.out.println("Enter the number");
  13.           m = sc.nextInt();
  14.           max = m;
  15.           while (t<=n-1)
  16.           {
  17.                  System.out.println("Enter the number");
  18.                  m = sc.nextInt();
  19.                  if(max<m) ;
  20.                  max=m;
  21.                  t++;
  22.           }
  23.           System.out.println("Maximum element is " + max);
  24.    }
  25. }
复制代码
Explanation:
Initially the number of elements taken is n. The first number is taken outside the loop and assumed to be maximum; this number is stored in max. The remaining numbers are taken inside the loop. On each iteration, the number is compared with the max. If the max is less than the number taken, the number will be the maximum one. This is checked through if statement. In the end when control comes out from while loop, max is displayed.

12
ReneeBK 发表于 2015-5-31 04:29:23
  1. /*PROG 4.11 FINDING REVERSE OF A NUMBER */
  2. import java.io.*;
  3. import java.util.*;
  4. class JPS14
  5. {
  6.    public static void main(String args[])
  7.    {
  8.           int orig, rev = 0, r;
  9.           Scanner sc = new Scanner(System.in);
  10.           System.out.print("\nEnter any +ve number:= ");
  11.           orig = sc.nextInt();
  12.           while (orig != 0)
  13.           {
  14.                  r = orig % 10;
  15.                  rev = rev * 10 + r;
  16.                  orig = orig / 10;
  17.           }
  18.           System.out.print("\n\nReverse Number:= " + rev);
  19.    }
  20. }
复制代码
Explanation: The logic to reverse a number is quite simple. It can be understood step by step inside the loop:
Suppose orig = > original number entered by programmer.
   rev => reverse of the orig.
   r => remainder
While (orig! =0)
{
   r = orig%10;
   rev = rev *10 +r;
   orig= orig /10;
}

13
ReneeBK 发表于 2015-5-31 04:32:10
  1. /*PROG 4.12 TO CHECK NUMBER IS PRIME OR NOT */
  2. import java.io.*;
  3. import java.util.*;
  4. class JPS15
  5. {
  6.    public static void main(String args[])
  7.    {
  8.           int num, c = 2;
  9.           Scanner sc = new Scanner(System.in);
  10.           System.out.print("\n\nEnter any +ve number:= ");
  11.           num = sc.nextInt();
  12.           while (c <= num / 2)
  13.           {
  14.              if (num % c == 0)
  15.              {
  16.                 System.out.println("\nNumber is not prime");
  17.                 System.exit(0);
  18.              }
  19.              c++;
  20.           }
  21.           System.out.println("\nNumber is prime");
  22.    }
  23. }
复制代码
Explanation: A number is prime if it is completely divisible by 1 and itself, e.g., 1, 3, 5, 7, 11, 13, 17, 19, 23, etc. To check whether a number is prime or not, start from a counter c = 2 (every number divides by 1) and continue till c < = num/2 since no number is completely divisible by a number which is more than half of that number. For example, 12 is not divisible by 7, 8, 9, 10, 11 which are greater than 6. So it should be checked whether the number divisible by any number < =num/2 is true; simply print ' Number is not prime ' and exit from the program, using System.exit(0). If num%c = = 0 is never true during the loop then at the end when loop exits maturely print 'Number is prime'.

14
ReneeBK 发表于 2015-5-31 04:35:43
  1. /*PROG 4.13 CHECK NUMBER FOR PALINDROME */
  2. import java.io.*;
  3. import java.util.*;
  4. class JPS16
  5. {
  6.    public static void main(String args[])
  7.    {
  8.           int orig, rev = 0, r, save;
  9.           Scanner sc = new Scanner(System.in);
  10.           System.out.print("Enter any +ve number:=");
  11.           orig = sc.nextInt();
  12.           save = orig;
  13.           while (orig != 0)
  14.           {
  15.                  r = orig % 10;
  16.                  rev = rev * 10 + r;
  17.                  orig = orig / 10;
  18.           }
  19.           if (rev == save)
  20.                  System.out.println("Number is palindrome");
  21.           else
  22.                  System.out.println("Number is not palindrome");
  23.    }
  24. }
复制代码


Explanation: A number is called palindrome if on reversing it is equal to the original number, e.g., 121, 454 and 3443, etc. To check whether an entered number is palindrome or not, simply reverse a number; the original number becomes zero when controls come out from the loop. So, the original number is saved in a variable before processing; in the above program, it is in the save variable.

15
ReneeBK 发表于 2015-5-31 04:40:15
  1. /*PROG 4.14 CHECK NUMBER FOR ARMSTRONG */
  2. import java.io.*;
  3. import java.util.*;
  4. class JPS17
  5. {
  6.    public static void main(String args[])
  7.    {
  8.           int num, r, save, newnum = 0, count = 0;
  9.           Scanner sc = new Scanner(System.in);
  10.           System.out.print("\nEnter any +ve number:= ");
  11.           num = sc.nextInt();
  12.           save = num;
  13.           while (num != 0)
  14.           {
  15.                  num = num / 10;
  16.                  count++;
  17.           }
  18.           num = save;
  19.           while (num != 0)
  20.           {
  21.                  r = num % 10;
  22.                  newnum = newnum + (int)Math.pow(r, count);
  23.                  num = num / 10;
  24.           }
  25.           if (newnum == save)
  26.                  System.out.println("\nNumber " + save + " is
  27.                                         Armstrong");
  28.           else
  29.                  System.out.println("\nNumber " + save + " is
  30.                                         not Armstrong");
  31.    }
  32. }
复制代码

16
ReneeBK 发表于 2015-5-31 04:43:12
  1. /*PROG 4.15 SUM OF THE SERIES 1−2+3−4+......*/
  2. import java.io.*;
  3. import java.util.*;
  4. class JPS18
  5. {
  6.    public static void main(String args[])
  7.    {
  8.        int n, sum = 0, t, k = 1;
  9.        Scanner sc = new Scanner(System.in);
  10.        System.out.println("Enter the number of terms\n");
  11.        n = sc.nextInt();
  12.        for (t = 1; t <= n; t++)
  13.        {
  14.            if (t % 2 != 0)
  15.                System.out.print(t + "-");
  16.            else
  17.                System.out.print(t + "+");
  18.            sum = sum + (t * k);
  19.            k = k * (-1);
  20.        }
  21.    System.out.println("\nSum of series is " + sum);
  22.    }
  23. }
复制代码

Explanation:

In the series to be generated, odd numbers are +ve and even numbers are −ve. Take one variable k = 1. k is multiplied by −1 inside the loop in each iteration.

Initially sum = 0 + (1 * 1) gives sum = 1 then k becomes k = 1 * − 1= −1, which is used in the second iteration of the loop. In the second iteration, sumbecomes sum = 1 + (2 * −1) => sum = 1 − 2 = − 1 and value of k changes to 1 again (−1 * −1 = 1) and this continues for n times.


17
ReneeBK 发表于 2015-5-31 04:46:14
  1. /*PROG 4.16 TO PRINT AND FIND SUM OF SERIES 1^2+2^2+3^2+4^2+...........(^ STANDS FOR POWER) */
  2. import java.io.*;
  3. import java.util.*;
  4. class JPS19
  5. {
  6.        public static void main(String args[])
  7.        {
  8.           double sum = 0;
  9.           int n, t;
  10.           Scanner sc = new Scanner(System.in);
  11.           System.out.println("Enter the number of terms\n");
  12.           n = sc.nextInt();
  13.           for (t = 1; t <= n; t++)
  14.        {
  15.           System.out.print(t + "^2+");
  16.           sum = sum + Math.pow(t, 2);
  17.    }
  18.           System.out.println("\n Sum of series is " + sum);
  19.    }
  20. }
复制代码
Explanation:
pow is standard library function which returns power of first argument to second number, i.e., pow(2, 3) returns 2 to the power 3, i.e., 8. A number of terms are taken in n and loop is run from 1 to n. On each iteration, 2 was found to the power t and added to sum. The pattern is displayed within the for loop and sum of series is displayed outside the loop.

18
ReneeBK 发表于 2015-5-31 04:48:38
  1. /*PROG 4.17 TO CHECK A NUMBER IS PERFECT OR NOT */
  2. import java.io.*;
  3. import java.util.*;
  4. class JPS20
  5. {
  6.    public static void main(String args[])
  7.    {
  8.           int num, t, sum = 1;
  9.            Scanner sc = new Scanner(System.in);
  10.           System.out.println("Enter an integer");
  11.           num = sc.nextInt();
  12.           for (t = 2; t <= num / 2; t++)
  13.           {
  14.                  if (num % t == 0)
  15.                         sum = sum + t;
  16.           }
  17.           if (sum == num)
  18.                  System.out.println("The number is perfect");
  19.           else
  20.                  System.out.println("The number is not perfect");
  21.       }
  22. }
复制代码
Explanation:
A number is called perfect if sum of its factor is equal to the number itself, e.g., 6; its factors are 1, 2 and 3 and their sum is 6, which is equal to the number itself, so it is a perfect number. Similarly 28 is a perfect number. In the loop, the sum is initialized to 1 and the loop is run for num/2 as for any number, e.g., t which is more than num/2, num%t will not be zero (excluding num itself).

19
ReneeBK 发表于 2015-5-31 04:51:00
  1. /*PROG 4.18 PRINT THE FOLLOWING PATTERN, INPUT IS NUMBER OF LINES
  2. 1
  3. 2 3
  4. 4 5 6
  5. 7 8 9 10
  6. */
  7. import java.io.*;
  8. import java.util.*;
  9. class JPS21
  10. {
  11.    public static void main(String args[])
  12.    {
  13.           int line, row, col, value = 0;
  14.           Scanner sc = new Scanner(System.in);
  15.           System.out.print("\n\nEnter the number of
  16.                                lines:= ");
  17.           line = sc.nextInt();
  18.           System.out.println("\nThe patter is\n");
  19.           for (row = 1; row <= line; row++)
  20.           {
  21.                  for (col = 1; col <= row; col++)
  22.                  {
  23.                        value++;
  24.                        System.out.print(" " + value);
  25.                  }
  26.                  System.out.println();
  27.           }
  28.    }
  29. }
复制代码
Explanation: Two for loops have been used in the program. One is to control the number of rows and second is to control the number of columns. Initially assumeline = 5 so outer for loop runs five times. In the first run, row = 1 and inner loop runs only once. The value of value variable is incremented and printed. Control moves to the new line. For row = 2, inner loop runs twice and second row of output is printed. This continues till row < = line.

20
ReneeBK 发表于 2015-5-31 04:57:15
  1. /*PROG 4.19 TO DISPLAY THE PATTERN AS
  2. A
  3. B B
  4. C C C
  5. D D D D
  6. E E E E E
  7. ...........
  8. ...........*/
  9. import java.io.*;
  10. import java.util.*;
  11. class JPS22
  12. {
  13.     public static void main(String args[])
  14.     {
  15.            int line, row, col;
  16.            char ch = 'A';
  17.            Scanner sc = new Scanner(System.in);
  18.            System.out.print("\n\nEnter number of lines:=");
  19.            line = sc.nextInt();
  20.            System.out.println("\n\nThe pattern is\n");
  21.            for (row = 1; row <= line; row++)
  22.            {
  23.                   for (col = 1; col <= row; col++)
  24.                        System.out.print(" " + ch);
  25.                   System.out.println();
  26.                   ch++;
  27.            }
  28.     }
  29. }
复制代码
Explanation: For the first run of outer for loop, ch = 'A'. When first iteration of inner for loop finishes, value of ch is incremented by 1 and becomes B. As one A on first row, two B on second row and so on have to be printed, ch at the end of every iteration of outer for loop is incremented.

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
jg-xs1
拉您进交流群
GMT+8, 2026-1-1 18:37