Armstrong number program

Armstrong number c program

#include<stdio.h>
#include<conio.h>
#include<math.h>
double Multiply(double number, int n);
void CheckArmstrong(int number);
void main()
{
	int number;
	printf("Enter your number \n");
	scanf("%d",&number);
	CheckArmstrong(number);
	
	getch();
}
double Multiply(double number, int n)
{
    return  pow(number, n);
}
void CheckArmstrong(int number)
{
    int r = 0, count = 0;
    double s = 0;
    int originalnumber = number;
    for (int i = originalnumber; i != 0; i /= 10)
    {
        count++;
    }

    while (number > 0)
    {
        r = number % 10;
        s += Multiply(r, count);
        number /= 10;
    }
    if (originalnumber == s)
        printf("Yes, it is an armstrong number");
    else
        printf("No, it is not an armstrong number");
           
}

Output

Enter your number
153
Yes, it is an armstrong number

Armstrong number program in c++

#include<iostream>
#include<conio.h>
#include<math.h>
using namespace std;
class Example{
public: double Multiply(double number, int n)
{
    return  pow(number, n);
}
public: void CheckArmstrong(int number)
{
    int r = 0, count = 0;
    double s = 0;
    int originalnumber = number;
    for (int i = originalnumber; i != 0; i /= 10)
    {
        count++;
    }

    while (number > 0)
    {
        r = number % 10;
        s += Multiply(r, count);
        number /= 10;
    }
    if (originalnumber == s)
        cout<<"Yes, it is an armstrong number";
    else
        cout<<"No, it is not an armstrong number";
           
}
};
void main()
{
	int input;
	cout<<"Enter your number \n";
	cin>>input;
	Example ex;
	ex.CheckArmstrong(input);
	getch();
}

Output

Enter your number
371
Yes, it is an armstrong number

Armstrong number program in c#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CheckArmStrongNumber
{
class Program
{
public double Multiply(int number, int n)
{
    return  Math.Pow(number, n);
}
public void CheckArmstrong(int number)
{
    int r = 0, count = 0;
    double s = 0;
    int originalnumber = number;
    for (int i = originalnumber; i != 0; i /= 10)
    {
        count++;
    }

    while (number > 0)
    {
        r = number % 10;
        s += Multiply(r, count);
        number /= 10;
    }
    if (originalnumber == s)
        Console.Write("Yes, it is an armstrong number");
    else
        Console.Write("No, it is not an armstrong number");
           
}
static void Main(string[] args)
{
    Program p1 = new Program();
    Console.WriteLine("Enter  your number");
    int number = Convert.ToInt32(Console.ReadLine());
    p1.CheckArmstrong(number);
    Console.ReadLine();
}
}
}

Output

Enter  your number
1634
Yes, it is an armstrong number

Armstrong number program in java

import java.util.*;
class AB
{
	public double Multiply(int number, int n)
	{
	    return  Math.pow(number, n);
	}
	public void CheckArmstrong(int number)
	{
	    int r = 0, count = 0;
	    double s = 0;
	    int originalnumber = number;
	    for (int i = originalnumber; i != 0; i /= 10)
	    {
	        count++;
	    }

	    while (number > 0)
	    {
	        r = number % 10;
	        s += Multiply(r, count);
	        number /= 10;
	    }
	    if (originalnumber == s)
	        System.out.println("Yes, it is an armstrong number");
	    else
	    	System.out.println("No, it is not an armstrong number");
	           
	}
	
}
class examples
{
public static void main(String[] args)
	{
     int input;
     Scanner obj= new Scanner(System.in);
     System.out.println("Enter your number");
     input=obj.nextInt();
     
     AB ab=new AB();
     ab.CheckArmstrong(input);
     
	}
}

Output

Enter your number
156
No, it is not an armstrong number

Write a comment


Name Email Message