Computer Notes
Square pattern
For (int i=1; i<=5;i++)
{
For (int j=1;j<=5:j++)
{
[Link](*);
}
[Link]();
}
Increasing triangle
For (int i=1; i<=5;i++)
{
For (int j=1;j<=i:j++)
{
[Link](*);
}
[Link]();
}
Decreasing Triangle
For (int i=1; i<=5;i++)
{
For (int j=i;j<=5:j++)
{
[Link](*);
}
[Link]();
}
Right Sided Triangle
For (int i=1; i<=5;i++)
{
For (int j=i;j<=5:j++)
{
[Link](“ “);
}
For(int j=1;j<=i:j++)
{
[Link](*);
}
[Link]();
}
Right Sided Inverted
For (int i=1; i<=5;i++)
{
For (int j=1;j<=i:j++)
{
[Link](“ “);
}
For(int j=i;j<=5:j++)
{
[Link](*);
}
[Link]();
}
Hill Pattern
For (int i=1; i<=5;i++)
{
For (int j=i;j<=5:j++)
{
[Link](“ “);
}
For(int j=1;j<i:j++)
{
[Link](*);
}
For(int j=1;j<=i:j++)
{
[Link](*);
}
[Link]();
}
Reverse Hill
For (int i=1; i<=5;i++)
{
For (int j=1;j<=i:j++)
{
[Link](“ “);
}
For(int j=i;j<5:j++)
{
[Link](*);
}
For(int j=i;j<=5:j++)
{
[Link](*);
}
[Link]();
}
Diamond Pattern
For (int i=1; i<5;i++)
{
For (int j=i;j<=5:j++)
{
[Link](“ “);
}
For(int j=1;j<i:j++)
{
[Link](*);
}
For(int j=1;j<=i:j++)
{
[Link](*);}
[Link]();}
For (int i=1; i<=5;i++)
{
For (int j=1;j<=i:j++)
{
[Link](“ “);
}
For(int j=i;j<5:j++)
{
[Link](*);
}
For(int j=i;j<=5:j++)
{
[Link](*);}
[Link]();}
NUMBER PATTERNS
Increasing Number
int n=5;
for(int i=1, p=1; i<=n; i++, p++){
for(int j=1; j<=i; j++){
[Link](p+" ");
}
[Link]();
}
Decreasing Number
int n=5;
for(int i=1, p=5; i<=n; i++, p++){
for(int j=1; j<=i; j++){
[Link](p+" ");
}
[Link]();
}
Increasing Even Numbers
int n=5;
for(int i=1, p=5; i<=n; i++, p+=2){
for(int j=1; j<=i; j++){
[Link](p+" ");
}
[Link]();
}
Alternating numbers
int n=5;
for(int i=1; i<=n; i++){
for(int j=1; j<=i; j++){
if(i%2==0)
[Link]("2 ");
else
[Link]("1 ");
}
[Link]();
}
Increasing Diamond Pattern
int n= 5,p=1;
for(int i=1; i<n; i++, p++){
for(int j=i; j<=n; j++){
[Link](" ");
}
for(int j=1; j<i; j++){
[Link](p +" ");
}
for(int j=1; j<=i; j++){
[Link](p +" ");
}
[Link]();
}
for(int i=1; i<=n; i++, p++){
for(int j=1; j<=i; j++){
[Link](" ");
}
for(int j=i; j<n; j++){
[Link](p +" ");
}
for(int j=i; j<=n; j++){
[Link](p +" ");
}
[Link]();
}
Decreasing Diamond Pattern
int n= 5;
for(int i=1, p= 1; i<=n; i++, p++){
for(int j=i; j<=n; j++){
[Link](" ");
}
for(int j=1; j<i; j++){
[Link](p +" ");
}
for(int j=1; j<=i; j++){
[Link](p +" ");
}
[Link]();
}
for(int i=1, p=5; i<=n; i++, p--){
for(int j=1; j<=i; j++){
[Link](" ");
}
for(int j=i; j<n; j++){
[Link](p +" ");
}
for(int j=i; j<=n; j++){
[Link](p +" ");
}
[Link]();
}
Increasing Row Numbers
int n=5;
for(int i=1; i<=n; i++){
int p=1;
for(int j=1; j<=i; j++){
[Link](p++ +" ");
}
[Link]();
}
Decreasing Row Numbers
int n= 5;
for(int i=1; i<=n; i++){
int p =1;
for(int j=1; j<=i; j++){
[Link](" ");
}
for(int j=i; j<=n; j++){
[Link](p++ +" ");
}
[Link]();
}
Hill Pattern
int n= 5;
for(int i=1; i<=n; i++){
int p =1;
for(int j=i; j<=n; j++){
[Link](" ");
}
for(int j=1; j<i; j++){
[Link](p++ +" ");
}
for(int j=1; j<=i; j++){
[Link](p++ +" ");
}
[Link]();
}
Reverse Pattern
int n=5;
for(int i=1; i<=n; i++){
int p=5;
for(int j=1; j<=i; j++){
[Link](p-- +" ");
}
[Link]();
}
1 Reverse
int n=5;
for(int i=1,k=n; i<=n;i++,k--){
int p=k;
for(int j=1; j<=i; j++){
[Link](" ");
}
for(int j=i; j<=n; j++){
[Link](p-- +" ");
}
[Link]();
}
1 Hill
int n= 5;
for(int i=1; i<=n; i++){
int p =1;
for(int j=i; j<=n; j++){
[Link](" ");
}
for(int j=1; j<i; j++){
[Link](p++ +" ");
}
for(int j=1; j<=i; j++){
[Link](p-- +" ");
}
[Link]();
}
Character Patterns
A Pattern
int n=5;
for(int i=1; i<=n; i++){
for(int j=1; j<=i; j++){
[Link]("A ");
}
[Link]();
}
Increasing Pattern
int n=5;
for(int i=1, p='A'; i<=n; i++, p++){
for(int j=1; j<=i; j++){
[Link]((char)p+"");
}
[Link]();
}
Odd Pattern
int n=5;
for(int i=1, p='E'; i<=n; i++, p+=2){
for(int j=1; j<=i; j++){
[Link]((char)p+"");
}
[Link]();
}
Reverse Indian
int n=5;
for(int i=1, p='E'; i<=n; i++, p--){
for(int j=1; j<=i; j++){
[Link]((char)p+"");
}
[Link]();
}
1 . Find if a number is prime number. prime number is a number
divisible by 1 and itself e.g 5 is a primt number as it has factor only
1,5
import [Link].*;
public class PrimeNum
{
public static void main(String args[])
{
Scanner sc =new Scanner([Link]);
int count=0;
[Link]("enter a number");
int n= [Link]( );
for(int i=1;i<=n;i++)
{
if(n%i==0)
count++;
}
if(count==2)
[Link]("Yes");
else
[Link]("No");
}
}
2 . Find if a number is a composite number. Composite number is a
number which has more than one factor(excluding 1 and itself) e.g
8=2, 4=2 factors
import [Link].*;
public class compositeNo
{
public static void main(String args[])
{
Scanner sc =new Scanner([Link]);
int count=0;
[Link]("enter a number");
int n= [Link]( );
for(int i=1;i<=n;i++) { if(n%i==0) count++; } if(count>3)
[Link]("Yes");
else
[Link]("No");
}
}
3 . Find if a number is a perfect number. A perfect number is number
which is equal to sum of its divisor or factor except itself e.g.
6=1+2+3
import [Link].*;
public class perfectNo
{
public static void main(String args[])
{
Scanner sc =new Scanner([Link]);
int sum=0;
[Link]("enter a number");
int n= [Link]( );
for(int i=1;i<n;i++)
{
if(n%i==0)
sum= sum+i;
}
if(sum==n)
[Link]("Yes");
else
[Link]("No");
}
}
4 . Find if a number is an Abundant number. Here sum of factor is
greater then the number. itself e.g. 12 factor 1,2,3,4,6=16>12
import [Link].*;
public class AbundantNo
{
public static void main(String args[])
{
Scanner sc =new Scanner([Link]);
int sum=0;
[Link]("enter a number");
int n= [Link]( );
for(int i=1;i<n;i++)
{
if(n%i==0)
sum= sum+i;
}
if(sum>n)
[Link]("Yes");
else
[Link]("No");
}
}
5 . Find if number is a Deficient number. Here sum of factor is less
than the no itself. e.g. 21 factor 1,3,7=11<21
import [Link].*;
public class DeficientNo
{
public static void main(String args[])
{
Scanner sc =new Scanner([Link]);
int sum=0;
[Link]("enter a number");
int n= [Link]( );
for(int i=1;i<n;i++)
{
if(n%i==0)
sum= sum+i;
}
if(sum<n)
[Link]("Yes");
else
[Link]("No");
}
}
6 . Find if number is a Pronic number. Pronic No is the product of
two consecutive integers, n(n+1). e.g. 56=7×8
import [Link].*;
public class PronicNo
{
public static void main(String args[])
{
Scanner sc =new Scanner([Link]);
int fact=0;
[Link]("enter a number");
int n= [Link]( );
for(int i=1;i<n;i++)
{
if(i*(i+1)==n)
fact=i;
}
if(fact!=0)
[Link]("Yes");
else
[Link]("No");
}
}
1. Write a java program to print sum of following series:
Code:
import [Link];
public class NetedLoopSeries
{
public static void main(String[] args)
{
double sum=0;
Scanner sc = new Scanner([Link]);
[Link]("Enter Number:");
int n = [Link]();
int x = [Link]();
for(int i=1, a=1, d=9; i<=n; i++, a+=2, d+=4){
int fact=1;
for(int j=1; j<=d; j++)
fact = fact*j;
sum=sum + [Link](x,a)/fact;
}
[Link]("Sum: "+sum);
}
}
}
2. Write a java program to print following sum of series:
(9! + 13 ! + 17! … N)
Code:
import [Link];
public class SumOfSeries
{
public static void main(String[] args)
{
int sum=0;
Scanner sc = new Scanner([Link]);
[Link]("Enter Number:");
int n = [Link]();
for(int i=1, a=9; i<=n; i++, a+=4{
int fact=1;
for(int j=1; j<=a; j++)
fact = fact*j;
sum = sum+fact;
}
[Link]("Sum: "+sum);
}
}
3. Write a java program to print following sum of series:
Code:
import [Link];
public class GeometricSeries {
public static void main(String[] args) {
int sum=0;
Scanner sc = new Scanner([Link]);
[Link]("Enter Number :" );
int n= [Link]();
for(int i=1, k=2; i<=n; i++, k*=2){
sum= sum + k ;
}
[Link]("Sum : "+sum);
}
}
4. Write a java program to print following sum of series:
(2! + 4! + 8! + 16! +… N)
Code:
import [Link];
public class SumOfSeries:
{
public static void main(String[] args)
{
int sum = 0;
Scanner sc = new Scanner([Link]);
[Link]("Enter Number:");
int n = [Link]();
for(int i=1, a=2; i<=n; i++, a*=2{
int fact=1;
for(int j=1; j<=a; j++)
fact = fact*j;
sum = sum+fact;
}
[Link]("Sum: "+sum);
}
}
5. Write a java program to print following sum of series:
(1! + 2 ! + 3! + 4! … N)
Code:
import [Link];
public class SumOfSeries
{
public static void main(String[] args)
{
int sum=0;
Scanner sc = new Scanner([Link]);
[Link]("Enter Number:");
int n = [Link]();
for(int i=1, a=1; i<=n; i++,a++){
int fact=1;
for(int j=1; j<=a; j++)
fact = fact*j;
sum = sum + fact;
}
[Link]("Sum: "+sum);
}
}
6. Write a java program to print following sum of series:
(10! + 9! + 8! + … 1!)
Code:
public class SumOfSeries
{
public static void main(String[] args)
{
int sum=0;
int n = 10;
for(int i=1, a=10; i<=n; i++, a--{
int fact=1;
for(int j=1; j<=a; j++)
fact = fact*j;
sum=sum+fact;
}
[Link]("Sum: "+sum);
}
}
7. Write a java program to print following sum of series:
Code:
import [Link];
public class SumOfSeries
{
public static void main(String[] args)
{
double sum=0;
Scanner sc = new Scanner([Link]);
[Link]("Enter Number:");
int n = [Link]();
for(int i=1, a=2; i<=n; i++, a++){
int fact=1;
for(int j=1; j<=a; j++)
fact = fact*j;
sum= sum + double(i+a)/fact;
}
[Link]("Sum: "+sum);
}
}
8. Write a java program to print following sum of series:
(1×2 + 1x2x3 + 1x2x3x4 + … N )
Code:
import [Link];
public class NetedLoopSeries
{
public static void main(String[] args)
{
int sum=0;
Scanner sc = new Scanner([Link]);
[Link]("Enter Number:");
int n = [Link]();
for(int i = 1, a=2; i<=n; i++,a++{
int fact=1;
for(int j=1; j<=a; j++)
fact = fact*j;
sum=sum+fact;
}
[Link]("Sum: "+sum);
}
}
9. Write a java program to print following sum of series:
Code:
import [Link];
public class NetedLoopSeries
{
public static void main(String[] args)
{
double sum=0;
Scanner sc = new Scanner([Link]);
[Link]("Enter Number:");
int n = [Link]();
for(int i=1, a=2; i<=n; i++,a++){
int fact=1;
for(int j=1; j<=a; j++)
fact = fact*j;
sum= sum + (double) (i+a)/fact;
}
[Link]("Sum: "+sum);
}
}
10. Write a java program to print following sum of series:
Code:
import [Link];
public class SumOfSeries
{
public static void main(String[] args)
{
double sum=0;
Scanner sc = new Scanner([Link]);
[Link]("Enter Number:");
int n = [Link]();
int x = [Link]();
for(int i=1, a=1, d=2; i<=n; i++, a+=10, d*=2){
int fact = 1;
for(int j=1; j<=d; j++)
fact = fact*j;
sum = sum + double(a+x)/fact;
}
[Link]("Sum: "+sum);
}
}
11. Write a java program to print following sum of series:
Code:
import [Link];
public class NetedLoopSeries
{
public static void main(String[] args)
{
double sum=0;
Scanner sc = new Scanner([Link]);
[Link]("Enter Number:");
int n = [Link]();
for(int i=1, a=2; i<=n; i++,a*=2){
int fact=1;
for(int j=1; j<=i+x; j++)
fact = fact*j;
int fact1 = 1;
for(int j=1; j<=a; j++)
fact = fact*j;
sum= sum + (double) fact/fact1;
}
[Link]("Sum: "+sum);
}
}
12. Write a java program to print following sum of series:
Code:
import [Link];
public class NetedLoopSeries
{
public static void main(String[] args)
{
double sum=0;
Scanner sc = new Scanner([Link]);
[Link]("Enter Number:");
int n = [Link]();
int x = [Link]();
for(int i = 1, a=1; i<=n; i++,a++{
int fact=1;
for(int j=1; j<=a; j++)
fact = fact*j;
sum= sum + [Link](x,a)/fact;
}
[Link]("Sum: "+sum);
}
}
1. Find if number is Deficient Number or not.
Deficient Number: Sum of factors is less than the number itself.
e.g. 21. Factors: 1,3,7 = 11<21.
Code:
import [Link];
public class DeficientNum
{
public static void main(String[] args)
{
[Link]("Enter the number:");
Scanner sc = new Scanner([Link]);
int n = [Link]();
int sum = 0;
for(int i = 1; i < n; i++)
{
if (n % i == 0)
sum = sum + i;
}
if (sum < n)
[Link]("Yes");
else
[Link]("No");
}
}
2. Find if number is a Composite number. its is a number which has more that
one factor(excl. 1,n).
e.g. 8=2,4=2 factors.
Code:
import [Link];
public class CompositeNum
{
public static void main(String[] args)
{
[Link]("Enter the number:");
Scanner sc = new Scanner([Link]);
int n = [Link]();
int count = 0;
for(int i = 1; i <= n; i++) { if (n % i == 0) count++; } if (count >3)
[Link]("Yes");
else
[Link]("No");
}
}
3. Find if given number is a Abundant number or not.
Abundant number: sum of factor is greater then the number itself.
e.g. 12. Factors: 1, 2, 3, 4, 6 = 16 >12
Code:
public class AbundantNum
{
public static void main(String[] args)
{
[Link]("Enter the number:");
Scanner sc = new Scanner([Link]);
int n = [Link]();
int sum = 0;
for(int i = 1; i < n; i++)
{
if (n % i == 0)
sum = sum + i;
}
if (sum > n)
[Link]("Yes");
else
[Link]("No");
}
}
4. Find if a given number is Pronic number or not.
Pronic Number: it is the product of two consecutive integers, n(n+1).
e.g 56= 7 x 8 .
Code:
import [Link];
public class PronicNum
{
public static void main(String[] args)
{
[Link]("Enter the number:");
Scanner sc = new Scanner([Link]);
int n = [Link]();
int fact = 0;
for(int i = 1; i < n; i++)
{
if (n % i == 0)
if (i * (i + 1) == n)
fact = i;
}
if (fact != 0)
[Link]("Yes");
else
[Link]("No");
}
5. Accept a number from user and print if given number is Automorphic
number or not.
Automorphic number: A Automorphic number is a number which is contained in
the last digit(s) of its square.
For example: 25 in 625
Code:
import [Link];
public class AutomorphicNumber
{
public static void main(){
Scanner sc = new Scanner([Link]);
int n;
[Link]("Enter any number:");
n= [Link]();
int m = n;
int flag = 0, q = n * n;
while (n != 0) {
int d = n%10;
int d1 = q%10;
if (d != d1)
flag = 1;
n = n / 10;
q = q / 10;
}
if ( flag == 0)
[Link] ("yes");
else
[Link] ("no");
}
}
6. Write Accept a number from user and print if given number is
Krishnamurthy number or special number or not.
KN Special number: A special number is a number whose sum of factorial of digits is
equal to the number
For example 145 = 1! + 4! + 5!
Code:
import [Link];
public class KNSpecialNumber {
public static void main()
{
Scanner sc = new Scanner([Link]);
int n;
[Link]("Enter any number:");
n= [Link]();
int m = n;
int sum = 0;
while (n != 0) {
int d = n%10;
int fact = 1;
for (int i= 1; i<= d; i++)
fact = fact * i;
sum = sum + fact;
n = n / 10;
}
if ( sum == m)
[Link] ("yes");
else
[Link] ("no");
}
}
7. Accept a number from user and print if given number is Neon number or
not.
Neon number: Sum of digits of square of the number is equal to the number .
For example: 9: 9*9 = 81, 9= 8+1
Code:
import [Link];
public class NeonNumber
{
public static void main()
{
Scanner sc = new Scanner([Link]);
int n;
[Link]("Enter any number:");
n= [Link]();
int m = n;
int sum = 0;
n = n * n;
while (n != 0) {
int d = n%10;
sum = sum + d;
n = n / 10;
}
if (sum == m)
[Link] ("yes");
else
[Link] ("no");
}
}
8. Accept a number from user and print if given number is duck number or
not.
Duck number: A Duck number is a number which has zeroes present in it. e.g 402,
280.
Code:
import [Link];
public class DuckNumber
{
public static void main(){
Scanner sc = new Scanner([Link]);
int n;
[Link]("Enter any number:");
n= [Link]();
int m = n;
int count = 0;
while (n != 0) {
int d = n%10;
if ( d == 0)
count++;
n = n / 10;
}
if ( count > 0)
[Link] ("yes");
else
[Link] ("no");
}
}
9. Accept a number from user and print if given number is spy number or not.
spy number: A spy number is a number which has sum its digits equals the product of
the digits.
Example: 123. 1+2+3=1*2*3
Code:
import [Link];
public class SpyNumber
{
public static void main(){
Scanner sc = new Scanner([Link]);
int n;
[Link]("Enter any number:");
n= [Link]();
int m = n;
int sum = 0, prod = 1;
while (n != 0) {
int d = n%10;
sum = sum + d;
prod = prod * d;
n = n / 10;
}
if (sum == prod)
[Link] ("yes");
else
[Link] ("no");
}
}
10. Accept a number from user and print if given number is special number or
not.
Special number: A special number is a number whose Sum of digits plus product of
digits it is equal to original number. e.g
For example 59 = 5 + 9 + (5×9)
=59.
Code:
import [Link];
public class SpecialNumber
{
public static void main()
{
Scanner sc = new Scanner([Link]);
int n;
[Link]("Enter any number:");
n= [Link]();
int m = n;
int sum = 0, prod = 1;
while (n != 0) {
int d = n%10;
sum = sum + d;
prod = prod * d;
n = n / 10;
}
if ( sum + prod == m)
[Link] ("yes");
else
[Link] ("no");
}
}
11. Accept a number from user and print if given number is prime number or
not?
Code:
import [Link];
public class primeNumber
{
public static void main(String[] args)
{
[Link]("Enter the number:");
Scanner sc = new Scanner([Link]);
int n = [Link]();
int count=0;
for(int i=1; i<=n; i++)
{
if (n % i == 0)
count++;
}
if (count==2)
[Link]("Yes");
else
[Link]("No");
}
}
12. Accept a number from user and print if given number is Perfect number or
not.
Perfect numbers : the sum of its positive divisors excluding the number itself is equal to
that number.
For example, 28 is a perfect number because, 28 is divisible by 1, 2, 4, 7, 14 and
28 ,and the sum of these values is 1 + 2 + 4 + 7 + 14 = 28.
Code:
import [Link];
public class PerfectNumber
{
public static void main(String[] args) {
int n, Sum = 0 ;
Scanner sc = new Scanner([Link]);
[Link]("\n Please Enter any Number: ");
n = [Link]();
for(int i = 1; i < n; i++) {
if (n % i == 0)
sum = sum + i;
}
if (sum == n)
[Link]("Yes");
else
[Link]("No");
}
}
[Link] whether a given number is palindrome number or not.
Palindrome means, If the reverse of the number is same of original number
Code:
import [Link];
public class palindrome
{
public static void main()
{
Scanner scan = new Scanner([Link]);
[Link]("Enter any number: ");
int n = [Link]();
int m = n;
int sum = 0;
while (n != 0) {
int d = n%10;
sum = sum * 10 + d;
n = n / 10;
}
if ( sum == m)
[Link] ("yes");
else
[Link] ("no");
}
}
[Link] a Program in Java to print GCD of two numbers.
The GCD (Greatest Common Divisor) of two numbers is the largest positive integer
number that divides both the numbers without leaving any remainder.
For example. GCD of 30 and 45 is 15.
Code:
import [Link];
public class GCD
{
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
int num1, num2, gcd = 1;
[Link]("Enter first number:");
num1= [Link]();
[Link]("Enter second number:");
num2= [Link]();
for(int i = 1; i <= num1 && i <= num2; i++)
{
if(num1%i==0 && num2%i==0)
gcd = i;
}
[Link]("GCD of "+num1+" and "+num2+" is= "+gcd);
}
}
15. Accept a number from user and print if given number is Harshad number or
not.
A number is said to be the Harshad number if it is divisible by the sum of its digit.
For example, 156= 1 + 5 + 6 = 12.
Since 156 is divisible by 12. So, 156 is a Harshad number.
Code:
import [Link];
public class HarshadNumber
{
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter any number:");
int n= [Link]();
int m = n;
int sum = 0;
while (n != 0) {
int d = n%10;
sum = sum + d;
n = n / 10;
}
if ( m%sum == 0)
[Link] ("yes");
else
[Link] ("no");
}
}