Guest User

Untitled

a guest
Apr 19th, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.70 KB | None | 0 0
  1. package practical04;
  2. public class Question01 {
  3.     public static void main(String[] args) {
  4.         int a=2;
  5.         while(a<=16){
  6.             System.out.print(a + "    ");
  7.             a+=2;
  8.         }
  9.         System.out.println();
  10.        
  11.         int b = 16;
  12.         do{
  13.             System.out.print(b + "  ");
  14.             b/=2;
  15.             }while(b >=1 );
  16.         System.out.println();
  17.        
  18.         int c = 0;
  19.        
  20.         for(c = 0; c >-10; c--){
  21.             System.out.print(c + "  ");
  22.         }
  23.         System.out.println();
  24.  
  25.     }
  26. }
  27.  
  28.  
  29.  
  30. package practical04;
  31. public class Question02 {
  32.     public static void main(String[] args) {
  33.        
  34.         int multa=0, multb=0, multc=0;
  35.              
  36.         while(multa <= 1000){
  37.             System.out.println(multa);
  38.             multa+=5;
  39.         }
  40.        
  41.         System.out.println("*************************************************");
  42.        
  43.         do{
  44.             System.out.println(multb);
  45.             multb+=5;
  46.         }while(multb <= 1000);
  47.        
  48.         System.out.println("*************************************************");
  49.        
  50.        
  51.         for(multc=0; multc<=1000; multc+=5){
  52.             System.out.println(multc);
  53.         }
  54.        
  55.         System.out.println("*************************************************");
  56.     }
  57. }
  58.  
  59.  
  60.  
  61. package practical04;
  62. import java.util.Scanner;
  63.  
  64. public class Question03 {
  65.     public static void main(String[] args) {
  66.         Scanner input = new Scanner(System.in);
  67.         int a=1, sum = 0;
  68.        
  69.         while(a<=5){
  70.             System.out.print("number " + a + " : ");
  71.             int b = input.nextInt();
  72.             a++;
  73.             sum+=b;
  74.         }
  75.         System.out.println("Sum of numbers: " + sum);
  76.        
  77.         do{
  78.             System.out.print("number " + a + " : ");
  79.             int b = input.nextInt();
  80.             a++;
  81.             sum+=b;  
  82.         }while(a<=5);
  83.         System.out.println("Sum of numbers: " + sum);
  84.        
  85.           for (a=1; a<=5; a+=1){
  86.             System.out.print("number " + a + " : ");
  87.             int b = input.nextInt();
  88.             sum+=b;  
  89.           }
  90.           System.out.println("Sum of numbers: " + sum);
  91.        
  92.        
  93.        
  94.     }    
  95. }
  96.  
  97.  
  98. package practical04;
  99.  
  100. public class Question04 {
  101.  
  102.     public static void main(String[] args) {
  103.         int n = 0, n2 = 1;
  104.         while (n2 <= 15000) {
  105.             n2 = n * n;
  106.             System.out.println(n);
  107.             n++;
  108.         }
  109.         n-=1;
  110.         System.out.println("The smallest integer is " + n);
  111.        
  112.     }
  113. }
  114.  
  115.  
  116. package practical04;
  117.  
  118. import javax.swing.JOptionPane;
  119.  
  120. public class Question05 {
  121.  
  122.     public static void main(String[] args) {
  123.         int num;
  124.         do {
  125.             num = Integer.parseInt(JOptionPane.showInputDialog("Enter a number"));
  126.             if (num <= 100 ||num >= 200) {
  127.                 JOptionPane.showMessageDialog(null, "Pls enter a number between 100 and 200!", "Error", JOptionPane.ERROR_MESSAGE);
  128.             }
  129.         }while(num <= 100 || num >= 200);
  130.        
  131.         JOptionPane.showMessageDialog(null, "End of program!");
  132.        
  133.     }
  134. }
  135.  
  136.  
  137. package practical04;
  138.  
  139. import javax.swing.JOptionPane;
  140.  
  141. public class Question06 {
  142. //      Practical 04 (Unmodified)
  143. //    public static void main(String[] args) {
  144. //        int num, mult;
  145. //        String output = "";
  146. //        String output2 = "";
  147. //        
  148. //        for (num = 1; num <= 12; num++) {
  149. //            mult = num*5;
  150. //            output = num + " x 5 = " + mult + "\n";
  151. //            output2 += output;            
  152. //        }
  153. //        
  154. //        JOptionPane.showMessageDialog(null, output2);
  155. //    }
  156.    
  157.     // below = modified for practical 05 //
  158.     public static void printTable(){
  159.         int num, mult;
  160.         String output = "";
  161.         String output2 = "";
  162.        
  163.         for (num = 1; num <= 12; num ++){
  164.             mult = num*5;
  165.             output = num + " x 5 = " + mult + "\n";
  166.             output2 += output;
  167.         }
  168.         JOptionPane.showMessageDialog(null, output2);
  169.     }
  170.    
  171.     public static void main(String[] args) {
  172.         printTable();
  173.     }
  174.    
  175.    
  176.    
  177.    
  178.    
  179.    
  180.    
  181.    
  182. }
  183.  
  184.  
  185.  
  186. package practical04;
  187.  
  188. import javax.swing.JOptionPane;
  189.  
  190. public class Question07 {
  191.  
  192.     public static void main(String[] args) {
  193.         int fnum = Integer.parseInt(JOptionPane.showInputDialog(null, "Enter 1st number", "Example", JOptionPane.INFORMATION_MESSAGE));
  194.         int snum = Integer.parseInt(JOptionPane.showInputDialog(null, "Enter 2nd number", "Example", JOptionPane.INFORMATION_MESSAGE));
  195.         int oddNum;
  196.         String print=" ";
  197.         String print2=" ";
  198.        
  199.         for (oddNum = fnum; oddNum < snum; oddNum += 1) {
  200.             if (oddNum % 2 == 0) {
  201.             } else {
  202.             print = oddNum + " \n";
  203.             print2 += print;
  204.             }
  205.         }
  206.  
  207.         JOptionPane.showMessageDialog(null,"Odd numbers between " + fnum + " & " + snum + "\n"
  208.                 +print2);
  209.    
  210.    
  211.    
  212.     }
  213. }
  214.  
  215.  
  216.  
  217. package practical04;
  218.  
  219. import javax.swing.JOptionPane;
  220.  
  221. public class Question08 {
  222.  
  223.     public static void main(String[] args) {
  224.         int a;
  225.         do {
  226.             a = Integer.parseInt(JOptionPane.showInputDialog("Enter any number or \n (0) to quit."));
  227.             if (a == 0) {
  228.             } else {
  229.                 {
  230.                     if (a % 5 == 0) {
  231.                         JOptionPane.showMessageDialog(null, a + " is divisble by 3 but not 5? false");
  232.                     } else {
  233.                         JOptionPane.showMessageDialog(null, a + " is divisble by 3 but not 5? true");
  234.                     }
  235.                 }
  236.             }
  237.         } while (a > 0);
  238.  
  239.         JOptionPane.showMessageDialog(null, "Program terminated");
  240.  
  241.  
  242.  
  243.  
  244.     }
  245. }
  246.  
  247.  
  248.  
  249. package practical04;
  250.  
  251. public class Question10 {
  252.  
  253.     public static void main(String[] args) {
  254.         int total = 0; // initialize total
  255.         int count = 1;
  256.  
  257.         while (count < 11) { // repeat for 10 times
  258.  
  259.             int randomInt;
  260.  
  261.             do {
  262.                 // generate a random number from 0 to 8
  263.                 randomInt = (int) (Math.random() * 9);
  264.  
  265.             } while (randomInt >= 0^randomInt % 2 == 0 ); // repeat if random number is not even
  266.  
  267.  
  268.             System.out.println("Number " + count + " : " + randomInt); // print out random even number
  269.  
  270.  
  271.             total += randomInt;    // increase total
  272.             count++;
  273.         }
  274.  
  275.  
  276.         double average = total / (count-1);     // compute average
  277.  
  278.  
  279.         System.out.println("Average is " + average);  // print out average
  280.  
  281.     } // end main
  282. } // end class
Add Comment
Please, Sign In to add comment