Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

What is wrong with my code...?

0 votes
asked Sep 22, 2021 by Stereeta Gilbert (120 points)
#include<stdio.h>
#include<stdio.h>
void armstrong(int);
void armstrong(int x)
{
    int a,t,sum=0,org;
    while(x>0)
    t=x%10;
    sum=sum+t*t*t;
    x=x/10;
    
        if (org==sum)
        printf("Armstrong");
        else
        printf("not armstrong");
    
}
void main()
{
    int a;
    printf("Enter the numbers");
    scanf("%d",&a);
}

2 Answers

0 votes
answered Sep 22, 2021 by Peter Minarik (84,720 points)

C or C++?

If you compile it as a C code it runs.

If you compile it as a C++ code, it does not run as the return type of main() should be int.

Format Your Code

Your code is not properly formatted. Indentations are neglected. Would you use proper indentation you'd see right away that the while loop is wrong as the update on the sum and x variables is not part of the loop, but it should be.

Call Your Function

Since you never call

arsmotrong(a);

from your main() function, your program only asks for a number then exits.

Set Your Variables

The variable org never takes its value, so later when you compare it to sum it will not give you the right answer. You should set it to the original value before changing it:
int org = x;

Also note that the variable a is unused, so you can remove it.

0 votes
answered Sep 22, 2021 by sireesha B (540 points)
#include<stdio.h>
#include<stdio.h>
void armstrong(int);
void armstrong(int x)
{
    int t,sum=0,org;
    org=x;
    while(x>0)
{
    t=x%10;
    sum=sum+t*t*t;
    x=x/10;
}
        if (org==sum)
        printf("Armstrong");
        else
        printf("not armstrong");
    
}
void main()
{
    int a;
    printf("Enter the numbers");
    scanf("%d",&a);
    armstrong(a);
}
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...