Skip to content

Commit dd404b3

Browse files
authored
Add files via upload
1 parent 38a164d commit dd404b3

22 files changed

+495
-0
lines changed

Week3/A-Loops/AllPrimeNumbers.txt

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import java.util.Scanner;
2+
public class Solution {
3+
4+
public static void main(String[] args) {
5+
6+
/* Your class should be named Solution.
7+
* Read input as specified in the question.
8+
* Print output as specified in the question.
9+
*/
10+
Scanner sc = new Scanner(System.in);
11+
int n=sc.nextInt();
12+
for(int i=2;i<=n;i++)
13+
{
14+
boolean isPrime=true;
15+
for(int j=2;j<i;j++)
16+
{
17+
if(i%j==0)
18+
{
19+
isPrime=false;
20+
break;
21+
}
22+
}
23+
if(isPrime)
24+
{
25+
System.out.println(i);
26+
}
27+
}
28+
29+
}
30+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import java.util.Scanner;
2+
public class Main {
3+
4+
public static void main(String[] args) {
5+
// Write your code here
6+
Scanner sc=new Scanner(System.in);
7+
int n=sc.nextInt();
8+
int ref=-1;
9+
int num=0;
10+
while(n>0)
11+
{
12+
ref+=1;
13+
int r=n%10;
14+
num=num+((int)Math.pow(2,ref)) * r;
15+
n/=10;
16+
17+
}
18+
System.out.println(num);
19+
}
20+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import java.util.Scanner;
2+
public class Solution {
3+
public static void printFac(int n)
4+
{
5+
for(int i=2;i<n;i++)
6+
{
7+
if(n%i==0)
8+
{
9+
System.out.print(i+" ");
10+
}
11+
}
12+
}
13+
14+
public static boolean isPrime(int n)
15+
{
16+
boolean b=true;
17+
for(int i=2;i<n;i++)
18+
{
19+
if(n%i==0)
20+
{
21+
return false;
22+
}
23+
}
24+
return true;
25+
}
26+
27+
public static void main(String[] args) {
28+
// Write your code here
29+
Scanner sc=new Scanner(System.in);
30+
int n=sc.nextInt();
31+
boolean res=isPrime(n);
32+
if(res)
33+
{
34+
System.out.println("-1");
35+
}
36+
else{
37+
printFac(n);
38+
}
39+
}
40+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import java.util.Scanner;
2+
public class Solution {
3+
4+
5+
public static void main(String[] args) {
6+
7+
/* Your class should be named Solution.
8+
* Read input as specified in the question.
9+
* Print output as specified in the question.
10+
11+
*/
12+
Scanner sc=new Scanner(System.in);
13+
int start=sc.nextInt();
14+
int end=sc.nextInt();
15+
int step=sc.nextInt();
16+
for(int i=start;i<=end;i+=step)
17+
{
18+
int f=(int)( (i-32) * 5.0/9.0);
19+
System.out.println(i+" "+f);
20+
}
21+
22+
}
23+
24+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import java.util.Scanner;
2+
public class Solution {
3+
4+
public static void main(String[] args) {
5+
// Write your code here
6+
Scanner sc = new Scanner(System.in);
7+
int n=sc.nextInt();
8+
int p=sc.nextInt();
9+
int res=(int)Math.pow(n,p);
10+
System.out.println(res);
11+
12+
}
13+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import java.util.Scanner;
2+
public class Main {
3+
public static void sum(int n)
4+
{
5+
System.out.println((n * (n+1))/2);
6+
return;
7+
}
8+
public static void prod(int n)
9+
{
10+
int p=1;
11+
for(int i=1;i<=n;i++)
12+
{
13+
p*=i;
14+
}
15+
System.out.println(p);
16+
return;
17+
}
18+
19+
public static void main(String[] args)
20+
{
21+
// Write your code here
22+
Scanner sc = new Scanner(System.in);
23+
int n=sc.nextInt();
24+
int c=sc.nextInt();
25+
switch(c)
26+
{
27+
case 1: sum(n);break;
28+
case 2: prod(n);break;
29+
default:System.out.println("-1"); break;
30+
}
31+
}
32+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import java.util.Scanner;
2+
public class Main {
3+
4+
public static void main(String[] args) {
5+
// Write your code here
6+
Scanner sc= new Scanner(System.in);
7+
int n=sc.nextInt();
8+
int i=5;
9+
for(int j=1;j<=n;)
10+
{
11+
12+
if(i%4!=0)
13+
{
14+
System.out.print(i+" ");
15+
j++;
16+
}
17+
i+=3;
18+
}
19+
20+
}
21+
}

Week3/A-Loops/MultiplicationTable.txt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import java.util.Scanner;
2+
public class Main {
3+
4+
public static void main(String[] args) {
5+
// Write your code here
6+
Scanner sc=new Scanner(System.in);
7+
int a=sc.nextInt();
8+
for(int i=1;i<=10;i++)
9+
{
10+
System.out.println(a*i);
11+
}
12+
}
13+
}

Week3/A-Loops/ReverseOfaNumber.txt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import java.util.Scanner;
2+
public class Main {
3+
4+
public static void main(String[] args) {
5+
// Write your code here
6+
Scanner sc = new Scanner(System.in);
7+
int n=sc.nextInt();
8+
int sum=0;
9+
for(;n!=0;n/=10)
10+
{
11+
sum=sum*10 + n%10;
12+
}
13+
System.out.println(sum);
14+
}
15+
}

Week3/A-Loops/SquareRootIntegral.txt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import java.util.Scanner;
2+
public class Main {
3+
4+
public static void main(String[] args) {
5+
// Write your code here
6+
Scanner sc = new Scanner(System.in);
7+
int n=sc.nextInt();
8+
int res=(int)Math.sqrt(n);
9+
System.out.println(res);
10+
11+
}
12+
}

Week3/A-Loops/SumofEvenNumbers.txt

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
3+
Pre-requisite to solve this problem:
4+
In this question, we have to find sum of all even numbers till n. To take sum of all the even numbers, we have to take a variable and initialize it to zero. Let us name this variable as sum.
5+
Following code is used to add 2 to sum variable and update it to the same sum variable:
6+
sum = sum + 2
7+
8+
Hint to solve this problem: We will have to loop on even numbers only and add the even numbers to the same sum variable, described above.
9+
*/
10+
11+
import java.util.Scanner;
12+
public class Solution {
13+
14+
public static void main(String[] args) {
15+
16+
/* Your class should be named Solution.
17+
* Read input as specified in the question.
18+
* Print output as specified in the question.
19+
*/
20+
Scanner sc = new Scanner(System.in);
21+
int n=sc.nextInt();
22+
System.out.println((n/2) * ((n/2) + 1));
23+
}
24+
25+
}

Week3/A-Loops/SumofnNumbers.txt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import java.util.Scanner;
2+
public class Solution {
3+
4+
5+
public static void main(String[] args) {
6+
7+
/* Your class should be named Solution.
8+
* Read input as specified in the question.
9+
* Print output as specified in the question.
10+
*/
11+
Scanner sc= new Scanner(System.in);
12+
int a=sc.nextInt();
13+
System.out.println(( (a)*(a+1) )/2);
14+
}
15+
16+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import java.util.Scanner;
2+
public class Solution {
3+
public static void main(String[] args) {
4+
// write the logic here !!
5+
Scanner sc=new Scanner(System.in);
6+
byte b=sc.nextByte();
7+
for(byte i=0;i<b;i++)
8+
{
9+
for(byte j=0;j<=i-1;j++)
10+
{
11+
System.out.print(' ');
12+
}
13+
for(byte j=b;j>i;j--)
14+
{
15+
System.out.print('*');
16+
}
17+
System.out.println();
18+
}
19+
20+
}
21+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import java.util.Scanner;
2+
public class Solution {
3+
public static void Print1(int n)
4+
{
5+
for(int i=1;i<=n;i++)
6+
{
7+
System.out.print(i);
8+
}
9+
}
10+
public static void Print2(int n)
11+
{
12+
int a=1,b=2;
13+
System.out.print(a);
14+
for(int i=2;i<n;i++)
15+
{
16+
System.out.print(' ');
17+
}
18+
System.out.print(b);
19+
}
20+
21+
public static void main(String[] args)
22+
{
23+
// write your code here !!
24+
Scanner sc = new Scanner(System.in);
25+
int n=sc.nextInt();
26+
for(int i=1;i<=n;i++)
27+
{
28+
if(i==1 || i==n)
29+
{
30+
Print1(n);
31+
System.out.println();
32+
}
33+
else{
34+
Print2(n);
35+
System.out.println();
36+
}
37+
}
38+
}
39+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import java.util.Scanner;
2+
public class Solution {
3+
4+
5+
public static void main(String[] args) {
6+
7+
/* Your class should be named Solution.
8+
* Read input as specified in the question.
9+
* Print output as specified in the question.
10+
*/
11+
Scanner sc=new Scanner(System.in);
12+
byte b=sc.nextByte();
13+
for(byte i=1;i<=b;i++)
14+
{
15+
for(byte j=1;j<=i;j++)
16+
{
17+
System.out.print(i-j+1);
18+
}
19+
System.out.println();
20+
}
21+
22+
}
23+
24+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import java.util.Scanner;
2+
public class Solution {
3+
public static void main(String[] args) {
4+
// write the logic here !!
5+
Scanner sc=new Scanner(System.in);
6+
byte b=sc.nextByte();
7+
for(byte i=1;i<=b;i++)
8+
{
9+
for(byte j=1;j<=i-1;j++)
10+
{
11+
System.out.print(' ');
12+
}
13+
for(byte j=b;j>=i;j--)
14+
{
15+
System.out.print("* ");
16+
}
17+
System.out.println();
18+
}
19+
}
20+
}

0 commit comments

Comments
 (0)