Skip to content

Commit cef4b74

Browse files
committed
Conditionals & Loops
1 parent a6691ce commit cef4b74

File tree

5 files changed

+124
-0
lines changed

5 files changed

+124
-0
lines changed

Conditionals & Loops/Factors.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/*
2+
* Write a program to print all the factors of a number other than 1 and the number itself.
3+
* */
4+
import java.util.Scanner;
5+
public class Factors {
6+
public static void main(String[] args) {
7+
System.out.print("Enter the number ");
8+
Scanner s = new Scanner(System.in);
9+
int n = s.nextInt();
10+
11+
for(int i=2;i<=n/2;i++) {
12+
if(n%i==0) {
13+
System.out.print(i +" ");
14+
}
15+
}
16+
}
17+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*
2+
* Write a program to find x to the power n (i.e. x^n). Take x and n from the user. You need to print the answer.
3+
Note : For this question, you can assume that 0 raised to the power of 0 is 1
4+
*/
5+
import java.util.Scanner;
6+
public class FindPowerOfNumber {
7+
public static void main(String[] args) {
8+
Scanner s = new Scanner(System.in);
9+
System.out.print("Enter Number ");
10+
int x = s.nextInt();
11+
System.out.print("Enter power ");
12+
int n = s.nextInt();
13+
14+
int p = 1;
15+
while(n!=0) {
16+
p*=x;
17+
n--;
18+
}
19+
System.out.println("Power Of Number is "+ x +" = "+ p);
20+
21+
}
22+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/*
2+
* Write a program to print multiplication table of n
3+
*/
4+
import java.util.Scanner;
5+
public class MultiplicationTable {
6+
public static void main(String[] args) {
7+
System.out.print("Enter the number = ");
8+
Scanner s = new Scanner(System.in);
9+
int n = s.nextInt();
10+
System.out.println();
11+
System.out.println("Table of "+ n +" is:-");
12+
13+
for(int i=1;i<=10;i++) {
14+
int table = n*i;
15+
System.out.println(n +" * "+i +" = "+ table);
16+
}
17+
}
18+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* Write a program to input an integer N and print the sum of all its even digits and sum of all its odd digits separately.
3+
Digits mean numbers, not the places! That is, if the given integer is "13245", even digits are 2 & 4 and odd digits are 1, 3 & 5.
4+
*/
5+
import java.util.Scanner;
6+
public class SumofEvenAndOdd {
7+
public static void main(String[] args){
8+
System.out.print("Enter Number = ");
9+
Scanner s = new Scanner(System.in);
10+
int n = s.nextInt();
11+
12+
int SumOfEven = 0;
13+
int SumOfOdd = 0;
14+
15+
while(n!=0) {
16+
int temp = n%10;
17+
if(temp%2==0) {
18+
SumOfEven+=temp;
19+
}
20+
else {
21+
SumOfOdd+=temp;
22+
}
23+
n = (int)(n/10);
24+
}
25+
System.out.println("Sum of Even Number is: "+ SumOfEven +"\nSum of Odd Number is: "+ SumOfOdd);
26+
}
27+
}

Conditionals & Loops/TotalSalary.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Write a program to calculate the total salary of a person. The user has to enter the basic salary (an integer) and the grade (an uppercase character), and depending upon which the total salary is calculated as -
3+
totalSalary = basic + hra + da + allow – pf
4+
where :
5+
hra = 20% of basic
6+
da = 50% of basic
7+
allow = 1700 if grade = ‘A’
8+
allow = 1500 if grade = ‘B’
9+
allow = 1300 if grade = ‘C' or any other character
10+
pf = 11% of basic.
11+
*/
12+
import java.util.Scanner;
13+
import java.lang.Math;
14+
public class TotalSalary {
15+
public static void main(String[] args) {
16+
17+
Scanner s = new Scanner(System.in);
18+
System.out.println("Enter Basic Salary");
19+
int basicSalary = s.nextInt();
20+
System.out.println("Enter grade");
21+
char grade = s.next().charAt(0);
22+
23+
double hra = 0.2*basicSalary;
24+
double da = 0.5*basicSalary;
25+
double pf = 0.11*basicSalary;
26+
int allow;
27+
28+
if(grade=='A') {
29+
allow = 1700;
30+
}
31+
else if(grade=='B'){
32+
allow = 1500;
33+
}
34+
else {
35+
allow = 1300;
36+
}
37+
double totalSalary = basicSalary + hra + da + allow - pf;
38+
System.out.println(Math.round(totalSalary));
39+
}
40+
}

0 commit comments

Comments
 (0)