Remedials Assignments Sandip Das
Remedials Assignments Sandip Das
import java.util.*;
public class Main
{
public static void main(String[] args) {
int num,sum=0;
Scanner sc=new Scanner(System.in);
num=sc.nextInt();
while(num>0)
{
sum=sum+num%10;
num=num/10;
}
System.out.println(sum);
}
}
Logic for this solution - while num>0 the loop will be run.
Assume sum=0 then sum=0*234%10=4
num=234/10=23
int num,factorial=1;
Scanner sc=new Scanner(System.in);
num=sc.nextInt();
for(int i = 1; i <= num; ++i)
{
factorial=factorial * i;
}
System.out.println(factorial);
}
}
3. Program to find the palindrome of a number.
import java.util.*;
public class Main {
int num,temp,rem,sum=0;
Scanner sc=new Scanner(System.in);
num=sc.nextInt();
temp=num;
while(num>0)
{
rem=num%10;
sum=(sum*10)+rem;
num=num/10;
}
if(temp==sum)
{
System.out.println("Palindrome");
}
else
{
System.out.println("Not Palindrome");
}
}
}
4. Write a program to input n numbers using array and find the greatest number of them.
Import java.util.*;
public class Main
{
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int[] arr=new int[n];
for (int i=0;i<n;i++){
arr[i]=sc.nextInt();
}
int max=arr[0];
for (int i=1;i<n;i++){
if(arr[i]>max){
max=arr[i];
}
}
System.out.println(max);
}
}
5. Write a program to print the following pattern given n as argument:
[for input 3]
1
22
333
Import java.util.*;
public class Main
{
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
for(int i=0;i<n+1;i++){
for(int j=0;j<i;j++){
System.out.print(i);
}
System.out.println();
}
}
}
6. Input temperature for a week. Calculate the average temperature and display the highest
and lowest temperature using functions.
import java.util.*;
import java.util.*;
9. Calculate grade and Display the Remarks of a student. Consider score of 5 subjects.
Calculate the percentage and grade as per the criteria below: [Hint: use Switch case]
Percentage Grade Remark
>95% A Excellent
>75% B Good
>50% C Well done
>40% D Passed
<40% E Failed
import java.util.*;
public class Main {
Double total=sub1+sub2+sub3+sub4+sub5;
Double percentage = (total/5)*100;
char grade;
String remark;
if (percentage > 95) {
grade = 'A';
remark = "Excellent";
} else if (percentage > 75) {
grade = 'B';
remark = "Good";
} else if (percentage > 50) {
grade = 'C';
remark = "Well done";
} else if (percentage > 40) {
grade = 'D';
remark = "Passed";
} else {
grade = 'E';
remark = "Failed";
}
10. Program to create a calculator which does basic mathematic operations (addition,
subtraction, multiplication and division) using function call for 2 input values.
import java.util.*;
class Main {
public static void main(String[] args) {
char operator;
Double number1, number2, result;
Scanner input = new Scanner(System.in);
// ask users to enter operator
System.out.println("Choose an operator: +, -, *, or /");
operator = input.next().charAt(0);
switch (operator) {
case '+':
result = number1 + number2;
System.out.println(number1 + " + " + number2 + " = " + result);
break;
case '-':
result = number1 - number2;
System.out.println(number1 + " - " + number2 + " = " + result);
break;
case '*':
result = number1 * number2;
System.out.println(number1 + " * " + number2 + " = " + result);
break;
case '/':
result = number1 / number2;
System.out.println(number1 + " / " + number2 + " = " + result);
break;
default:
System.out.println("Invalid operator!");
break;
}
input.close();
}
}