TCS Coding Problem 7

25 comments on “TCS Coding Problem 7”


  • sai

    import math
    def isAmstrong(num):
    n = len(str(num))
    t=n
    temp = num
    total=0
    while t>0:
    digi = num % 10
    total += math.pow(digi,n)
    num = num//10
    t-=1
    if total == temp:
    return “yes”
    else:
    return “No”


  • 156

    package workspace;

    import java.util.Scanner;

    public class armstrong {

    public static void main(String[] args) {

    Scanner scn=new Scanner(System.in);
    int n=scn.nextInt();
    int temp=n;
    int node=0;
    int sum=0;
    while(temp>0) {
    node++;
    temp=temp/10;
    }

    temp=n;
    while(temp>0) {
    int val=1;
    int rem=temp%10;
    for(int i=1;i<=node;i++) {
    val=val*rem;
    }
    sum=sum+val;
    temp=temp/10;
    }
    if(sum==n) {
    System.out.println("YES");
    }else {
    System.out.println("NO");
    }

    }

    }


  • sagarbiswasbunny

    def armstrong_number(num):
    num_list=list(map(int,str(num)))
    n=len(num_list)
    sum_num=0
    for i in range(n):
    sum_num=sum_num+num_list[i]**n
    if sum_num==num:
    return True
    else:
    return False

    num=int(input())
    print(armstrong_number(num))


  • RAKESH

    //java
    import java.util.Scanner;

    public class ArmstrongNumber {

    public static void main(String[] args) {
    Scanner sc=new Scanner(System.in);
    int n=sc.nextInt();
    String s=String.valueOf(n);
    int x=0;

    for(int i=0;i<s.length();i++)
    {
    x=x+(int) Math.pow(Integer.valueOf(String.valueOf(s.charAt(i))),s.length() );
    }
    if(x==n)
    {
    System.out.print(n+" is Armstrong Number");
    }
    else
    {
    System.out.print(n+" is not Armstrong Number");
    }
    }

    }


  • dee.pansh18johri

    #include
    using namespace std;
    int main() {
    // your code goes here
    int n;
    cin>>n;
    int len=0;
    int m=n;
    while(m)
    {
    len++;
    m/=10;
    }
    double sum=0;int w=n;
    while(n)
    {
    sum+=pow(n%10,len);
    n/=10;
    }
    if(sum==w)
    cout<<"ARMSTRONG";
    else
    cout<<"NOT AN ARMSTRONG NUMBER";
    return 0;
    }
    // O(N)


  • Adarsh

    #include
    #include
    using namespace std;

    int main()
    {
    int n;
    cin>>n;
    int count=0;
    int temp=n;
    int t,temp1,tot=0;
    while(temp>0)
    {
    temp=temp/10;
    count++;
    }

    t=n;
    for(int i=0;i<count;i++)
    {
    temp1=t%10;

    tot=tot+pow(temp1,count);
    t=t/10;

    }

    if(tot==n)
    { cout<<n<<" is an armstrong number";}
    else { cout<<n<<" is not an armstrong number";}
    }


  • Gourav

    n=16345
    temp=n
    lng=0
    while(temp!=0):
    temp=temp//10
    lng+=1
    temp=n
    res=0
    while(temp!=0):
    res=res+pow(temp%10,lng)
    temp=temp//10
    if(res==n):
    print(‘YES’)
    else:
    print(‘NO’)


  • Deepak

    #include
    #include
    using namespace std;
    int main()
    {
    int n,sum=0,r;
    cout<>n;
    int size = trunc(log10(n)) + 1;
    cout<<size<0)
    {
    r=n%10;
    n=n/10;
    sum=sum+pow(r,size);
    }
    if(sum==temp)
    {
    cout<<temp<<" is armstrong number";
    }
    else
    cout<<temp<<" is not armstrong number";

    }


  • Rohan

    #include
    #include
    #include
    int main()
    {
    int n, i,d=0, num,c=0,k, an=0;
    printf(“Enter anumber”);
    scanf(“%d”,&n);
    num=n;
    k=n;
    printf(“%d\n”,k);
    while(n!=0)
    {

    n=n/10;
    c++;
    }

    while(k!=0)
    {
    d= k%10;

    an += pow(d,c) ;

    k=k/10;
    }
    printf(“%d”,an);
    if(num == an)
    printf(“It is armstrong number”);
    else
    printf(“It is not a armstrong number”);
    return 0;

    }


  • shushank

    n=int(input())
    a=n
    c=0
    d=len(str(n))
    while n!=0:
    c1=n%10
    c=c+c1**d
    n=n//10
    if a==c:
    print(“Yes”)
    else:
    print(“No”)


  • Debanjan

    import math
    def checkArmstrong(n) :
    l=len(n)
    print(l)
    a=int(n)
    print(a)
    b=a
    e=0
    while a>0:
    d=a%10
    e=e+d**l
    a=math.trunc(a/10)
    print(e)
    if e==b:
    return True
    else:
    return False
    # n=input(“Enter number to be checked”)
    print(checkArmstrong(“153”))