You are on page 1of 3

Assignment – 1

Enrollment No. – 0801IT221017 Name -Aman Sharma


Submitted to – Prof Vivek Menon sir

Q1) Write a program to display 'Hello World'. Sol 1) Code – public


class Q1_HelloWorld{ public static void main(String[] args) {
System.out.println("Hello World!!");

}
}

Output –

Q2) Write a program to calculate the sum of digits of a number.

Sol 2) Code – public class Q2_SumOfDigits { static int sd(int n){ int sum = 0;

while(n != 0){ sum += n%10; n /= 10; } return sum;

public static void main(String[] args){ int n = 7562;

System.out.println("The sum of digits of number " + n + " is "+ sd(n));

Output –

Q3) Write a program to reverse a number.


Sol 3) Code –

public class Q3_ReverseNumber {

static int sd(int n){ int rev = 0; while(n != 0){ rev *= 10; rev +=
n%10; n /= 10;

} return rev;

} public static void main(String[] args){ int n =

1238;

System.out.println("The sum of digits of number " + n + " is "+ sd(n));

Output –

Q4) Write a program to display the following structure. *****


*** * ***
*****

Sol 4) Code –

public class Q4_StarPattern { static void star(int n){

//upperTriangle for(int i = 1;i <= n/2 + 1 ; i++ ){ for(int j = 1; j<i ; j++){


System.out.print(" ");
}

for(int k = 2*i-1;k<=n;k++){
System.out.print("*");
}
System.out.println();
}

//lowerTriangle for(int i =
1;i<=n/2;i++){
for(int j = i; j<=n/2-1 ; j++){
System.out.print(" ");
}

for(int k = 1;k<=2*i+1;k++){
System.out.print("*");
}
System.out.println();
}
}
public static void main(String args[]){ star(5);
}
}
Output –

You might also like