Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

Vel Tech Rangarajan Dr Sagunthala R&D Institute of Science and Technology, Avadi, Chennai

Office of Campus to Corporate

S4P1) Write a function to check whether the given number is an Armstrong Number or not. If the
number n is an Armstrong number, then print 1 otherwise print 0.
###################################################
Sample Input-1:
123
Sample Output-1:
0
Sample Input-1:
153
Sample Output-1:
1
################################################## Solution in Python:

def isArmstrong(n):
s = str(n)
length = len(s)
total = 0
for i in s:
total += int(i)**length
if(total==n):
return ("1")
else:
return ("0")
n = int(input())
ans= isArmstrong (n)
print(ans)
#######################
//////////////////////////////////////////////////
// Solution in Java:
import java.util.Scanner;
class Main {
static int isArmStrong( int n)
{
Vel Tech Rangarajan Dr Sagunthala R&D Institute of Science and Technology, Avadi, Chennai
Office of Campus to Corporate

String s=Integer.toString(n);
int len=s.length();
int sum=0;
int m=n;
int d;
while(m>0){
d=m%10;
int pow=1;
for(int i=1;i<=len;i++)
pow=pow*d;
sum=sum+pow;
m=m/10;
}
if(sum==n)
return (1);
else
return (0);
}
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int ans = isArmStrong( n);
System.out.println(ans);

}
}//////////////////////////////////////////////
//////////////////////////////////////////////
Vel Tech Rangarajan Dr Sagunthala R&D Institute of Science and Technology, Avadi, Chennai
Office of Campus to Corporate

S4P2) An integer that is the square of another integer is referred to as a perfect square. For instance, 3
and 11 are not perfect squares. 1, 4, 9 and 16 are perfect squares. If the given number is not a perfect
square, then print that number, otherwise print the least divisor for the given number.
Sample Input-1:
16
Sample Output-1:
2
Sample Input-2:
11
Sample Output-2:
11

###############################################Solution in Python:

n=int(input())
flag=0
for i in range(2,n):
if(n%i==0):
flag=1
break
if flag==1:
print(i)
else:
print(n)

#####################################################
Vel Tech Rangarajan Dr Sagunthala R&D Institute of Science and Technology, Avadi, Chennai
Office of Campus to Corporate

///////////////////////////////////////////////////////Solution in JAVA:
import java.util.Scanner;
class PerfectSquare {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
boolean flag=false;
int i;
for (i=2; i<=n/2; i++) {
if(n%i==0) {
flag=true;
break;
}
}
if(flag==true)
System.out.println(i);
else
System.out.println(n);
}
}
Vel Tech Rangarajan Dr Sagunthala R&D Institute of Science and Technology, Avadi, Chennai
Office of Campus to Corporate

S4P3) Given an integer n, print the “reverse triangle” using * as shown below
Sample Input-1:
5
Sample Output-1:
*****
****
***
**
*
Sample Input-2:
7
Sample Output-2:
*******
******
*****
****
***
**
*
Vel Tech Rangarajan Dr Sagunthala R&D Institute of Science and Technology, Avadi, Chennai
Office of Campus to Corporate

#########Solution in Python:
n = int(input())
k=n
m=0
for i in range(n,0,-1):
for k in range(n-i):
print(" ", end="")
for j in range(i):
print("*", end=" ")
print()
#########/////////////////////////////////////////////////// Solution in Java
import java.util.Scanner;
class PrintTriangle {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int m=n;
for(int i=n; i>=0;i--)
{ for(int k=0;k<n-i;k++)
System.out.print(" ");
for(int j=1;j<=i;j++)
System.out.print("* ");
System.out.println();
}
}
}
////////////////////////////////////////////////////

You might also like