ICSE BlueJ Logical Programs 2

 

 
25. Write a program to print all the factors of a number.
class ques25
{
  public static void main(String args[])
  {
    int n  =  Integer.parseInt(args[0]);
    System.out.println ("The all Factors of given no. "+n+" is");
    for(int i = 1;i< = n;i++)
    {
      if(n%i =  = 0)
        System.out.println (i);
    }
  }
}
26. WAP to accept a number and check whether the number is perfect number or not. A number is called perfect number, if the sum of all factors (except number itself) of the number is equal to that number. (For e.g. 6 is a perfect number. Factors are 1, 2 & 3 and the sum is 1+2+3 = 6.)
class ques26
{
  public static void main(String args[])
  {
    int i,s = 0;
    int n  =  Integer.parseInt(args[0]);
    for(i = 1;i<n;i++)
    {
      if(n%i =  = 0)
        s = s+i;
    }
    if(n =  = s)
      System.out.println ("The given no. "+n+" is a Perfect no");
    else
      System.out.println ("The given no. "+n+" is not a Perfect no");
  }
}
27. WAP to generate all Perfect numbers up to 1000.
class ques27
{
  public static void main(String args[])
  {
    int i,s,n;
    System.out.println ("The Perfect nos between 1 to 1000 are ");
    for(n = 1;n< = 1000;n++)
    {
      s = 0;
      for(i = 1;i<n;i++)
      {
        if(n%i =  = 0)
          s = s+i;
      }
      if(n =  = s)
        System.out.println (n);
    }
  }
}
28. Write a program to accept a number, check whether the numbers is prime number or not. If number is prime, then display "PRIME NUMBER" or display "NOT A PRIME NUMBER".
class ques28
{
  public static void main(String args[])
  {
    int i,f = 0;
    int n  =  Integer.parseInt(args[0]);
    for(i = 2;i<n;i++)
    {
      if(n%i =  = 0)
      {
        f = 1;
        break;
      }
    }
    if(f =  = 0)
      System.out.print ("The given no. "+n+" is a prime number");
    else
      System.out.print ("The given no. "+n+" is not a prime number");
  }
}
29. Write a program to display all prime numbers between 100 to 200.
class ques29
{
  public static void main(String args[])
  {
    int i,f,n;
    System.out.print ("The Prime nos between 100 to 200 are : ");
    for(n = 100;n< = 200;n++)
    {
      f = 0;
      for(i = 2;i<n;i++)
      {
        if(n%i =  = 0)
        {
          f = 1;
          break;
        }
      }
      if(f =  = 0)
        System.out.print (n+" ");
    }
  }
}
30. The production (P) of crude oil of a country in millions of barrel may be estimated by the following set of equations, where t represent the times in years. P = 5+3t, for 0< = t< = 3 and P = 14+(T-5/2)^2, for t>3, Write a program to print the quantity of production for every year from t = 1 to 10.
 
31. Write a program to accept a number then print the sum of digits and number of digits present in it. (E.g. If the input number is 225, the sum of digits is 9 and number of digits is 3).
class ques31
{
  public static void main(int n)
  {
    int s = 0,c = 0,d;
    while(n>0)
    {
      d = n%10;
      s = s+d;
      c = c+1;
      n = n/10;
    }
    System.out.println ("Sum of digits : "+s);
    System.out.println ("no. of digits : "+c);
  }
}
32. Write a program to accept a number then print the number in reverse order. (E.g. If the input number is 245, the output will be 542)
class ques32
{
  public static void main(int number)
  {
    int reverseNumber = 0, digit;
    while(number>0)
    {
      digit = number%10;
      reverseNumber = reverseNumber*10+digit;
      number = number/10;
    }
    System.out.println ("Reverse Number is : " +reverseNumber);
  }
}
33. Write a program to check whether a number is palindrome number or not. (Palindrome means, If the reverse of the number is same of original number)
class ques33
{
  public static void main(int number)
  {
    int reverseNumber = 0, orgNumber = number, digit;
    while(number>0)
    {
      digit = number%10;
      reverseNumber = reverseNumber*10+digit;
      number = number/10;
    }
    if(orgNumber =  = reverseNumber)
      System.out.println ("Number is Palindrom");
    else
      System.out.println ("Number is not Palindrom");
  }
}
34. A number is called Armstrong number if the sum of cube of each digit of the number is equal to that number. Write a program to accept a number then check whether the given number is Armstrong number or not. (E.g. 153 is a Armstrong number because 153 =  13 + 53 +33).
class ques34
{
  public static void main(int number)
  {
    double sum = 0;
    int orgNumber = number, digit;
    while(number>0)
    {
      digit = number%10;
      sum = sum+Math.pow(digit,3);
      number = number/10;
    }
    if(orgNumber =  = sum)
      System.out.println ("Number is Armstrong");
    else
      System.out.println ("Number is not Armstrong");
  }
}
35. Write a program to display all three digits Armstrong number.
class ques35
{
  public static void main()
  {
    int sum;
    int n, digit;
    for(int number = 100;number< = 999;number++)
    {
      sum = 0;
      n = number;
      while(n>0)
      {
        digit = n%10;
        sum = sum+(digit*digit*digit);
        n = n/10;
      }
      if(number =  = sum)
        System.out.println ("Armstrong Number is "+number);
    }
  }
}
36. Write a program to check whether all digits of the given number are same type or not (i.e. all are odd, all even numbers or both present)
 
37. Write a program to accept a number and then add all digits until you found a single digit number. If that single digit number is 1, then that number is called lucky number. (e.g. if number is 2345 then sum of its digits becomes 14, further sum of this digits is 5, so the number is not a lucky number)
class ques37
{
  public static void main(int number)
  {
    int sum, digit;
    while(number>9)
    {
      sum = 0;
      while(number>0)
      {
        digit = number%10;
        sum = sum+digit;
        number = number/10;
      }
      number = sum;
    }
    if(number =  = 1)
      System.out.println ("Lucky Number");
    else
      System.out.println ("Not Lucky Number");
  }
}
 

Comments