Armstrong Number Command Line Programming

A number is called Armstrong number if the Sum of the cubes of its digits is equal to the number itself.

C program to find Armstrong number using Command line arguments

The following is a C program to check whether the given number is Armstrong number or not using command line arguments.

It is highly advisable to go through Command Line Arguments Post before even looking at the code. Please study this for TCS and come back to this post later

 
#include</span><span class="str"><stdio.h></span>
<span class="kwd">int</span><span class="pln"> main</span><span class="pun">(</span><span class="kwd">int</span><span class="pln"> argc</span><span class="pun">,</span> <span class="kwd">char</span> <span class="pun">*</span><span class="pln"> argv</span><span class="pun">[])</span>
<span class="pun">{</span>
<span class="kwd">int</span><span class="pln"> num</span><span class="pun">,</span><span class="pln">temp</span><span class="pun">,</span><span class="pln">arms</span><span class="pun">=</span><span class="lit">0</span><span class="pun">,</span><span class="pln">rem</span><span class="pun">;</span>
<span class="kwd">if</span> <span class="pun">(</span><span class="pln">argc</span><span class="pun">!=</span> <span class="lit">2</span><span class="pun">)</span>
<span class="pun">{</span>
<span class="pln"> printf</span><span class="pun">(</span><span class="str">"Enter the number:\n"</span><span class="pun">);</span>
<span class="pln"> scanf</span><span class="pun">(</span><span class="str">"%d"</span><span class="pun">,&</span><span class="pln">num</span><span class="pun">);</span>
<span class="pun">}</span>
<span class="kwd">else</span>
<span class="pun">{</span>
<span class="pln"> num </span><span class="pun">=</span><span class="pln"> atoi</span><span class="pun">(</span><span class="pln">argv</span><span class="pun">[</span><span class="lit">1</span><span class="pun">]);</span>
<span class="pun">}</span>
<span class="pln"> temp</span><span class="pun">=</span><span class="pln">num</span><span class="pun">;</span>
<span class="kwd">while</span><span class="pun">(</span><span class="pln">num</span><span class="pun">></span><span class="lit">0</span><span class="pun">)</span>
<span class="pun">{</span>
<span class="pln"> rem</span><span class="pun">=</span><span class="pln">num</span><span class="pun">%</span><span class="lit">10</span><span class="pun">;</span>
<span class="pln"> arms</span><span class="pun">=</span><span class="pln">arms</span><span class="pun">+</span><span class="pln">rem</span><span class="pun">*</span><span class="pln">rem</span><span class="pun">*</span><span class="pln">rem</span><span class="pun">;</span>
<span class="pln"> num</span><span class="pun">=</span><span class="pln">num</span><span class="pun">/</span><span class="lit">10</span><span class="pun">;</span>
<span class="pun">}</span>
<span class="kwd">if</span><span class="pun">(</span><span class="pln">temp</span><span class="pun">==</span><span class="pln">arms</span><span class="pun">)</span>
<span class="pun">{</span>
<span class="pln"> printf</span><span class="pun">(</span><span class="str">" \n%d is an Armstrong number"</span><span class="pun">,</span><span class="pln">temp</span><span class="pun">);</span>
<span class="pun">}</span>
<span class="kwd">else</span>
<span class="pun">{</span>
<span class="pln"> printf</span><span class="pun">(</span><span class="str">"\n%d is not an Armstrong number"</span><span class="pun">,</span><span class="pln">temp</span><span class="pun">);</span>
<span class="pun">}</span>
<span class="kwd">return</span> <span class="lit">0</span><span class="pun">;</span>
<span class="pun">}