Program to Find the nth Armstrong number.

Write a C program to find the nth Armstrong number. or Write a program to find the nth Armstrong number in C.

Program in C

Code:

/*Write a C program to find the nth Armstrong number. or Write a program to find the nth Armstrong number Using C*/

#include <stdio.h>
#include<math.h>
int
main ()
{
  int i, rangenumber, num = 1, c = 0, letest = 0;
  printf ("Enter the nth value:");
  scanf ("%d", &rangenumber);

  while (c != rangenumber)
    {
      int num2 = num;
      int num1 = num;
      int sum = 0;
      while (num1 != 0)
{
  int rem = num1 % 10;
  num1 = num1 / 10;
  sum = sum + rem * rem * rem;
}
      if (sum == num2)
{
  c++;
  letest = num;
}
      num = num + 1;
    }
  printf ("%dth amstrong number is %d", rangenumber, letest);
  return 0;
}

Input/Output:
Enter the nth value:2
2th amstrong number is 153


Write a C++ program to find the nth Armstrong number. or Write a program to find the nth Armstrong number in C++.

Program in C++

Code:

/*Write a C++ program to find the nth Armstrong number. or Write a program to find the nth Armstrong number using C++*/

#include <iostream>
#include<cmath>
using namespace std;
int
main ()
{
  int i, rangenumber, num = 1, c = 0, letest = 0;
  cout<<"Enter the nth value:";
  cin>>rangenumber;

  while (c != rangenumber)
    {
      int num2 = num;
      int num1 = num;
      int sum = 0;
      while (num1 != 0)
{
  int rem = num1 % 10;
  num1 = num1 / 10;
  sum = sum + rem * rem * rem;
}
      if (sum == num2)
{
  c++;
  letest = num;
}
      num = num + 1;
    }

  cout<<rangenumber<<"th amstrong number is "<<letest;
  return 0;
}

Input/Output:
Enter the nth value:3
3th amstrong number is 370

Write a JAVA program to find the nth Armstrong number. or Write a program to find the nth Armstrong number in Java.

Program in Java

Code:

/*Write a JAVA program to find the nth Armstrong number. or Write a program to find the nth Armstrong number using Java*/

import java.util.Scanner;
public class NthArmstrongNumber {

public static void main(String[] args) {
Scanner cs=new Scanner(System.in);
int  rangenumber, num = 1, c = 0, letest = 0;
  System.out.println("Enter Nth number:");
  rangenumber=cs.nextInt();

  while (c != rangenumber)
    {
      int num2 = num;
      int num1 = num;
      int sum = 0;
      while (num1 != 0)
{
  int rem = num1 % 10;
  num1 = num1 / 10;
  sum = sum + rem * rem * rem;
}
      if (sum == num2)
{
  c++;
  letest = num;
}
      num = num + 1;
    }
System.out.println(rangenumber+"th amstrong number is "+letest);
cs.close();
}
}

Input/Output:
Enter Nth number:
4
4th amstrong number is 371

Write a PYTHON to find the nth Armstrong number. or Write a program to find the nth Armstrong number in Python.

Program in Python

Code:

'''Write a Python program to find the nth Armstrong number. or Write a program to find the nth Armstrong number using Python '''

print("Enter the range number:")
rangenumber=int(input())
num = 1
c = 0
letest = 0

while (c != rangenumber):
    num2 = num
    num1 = num
    sum = 0
    while (num1 != 0):
        rem = num1 % 10
        num1 = num1 // 10
        sum = sum + rem * rem * rem
    if (sum == num2):
        c+=1
        letest = num
    num = num + 1
print(rangenumber,"th amstrong number is ",letest)

Input/Output:
Enter the range number:
5
5 th amstrong number is  407

C/C++/java/Python practice questions with solution


SQL Queries   Android Project Ideas







Post a Comment

0 Comments