C Armstrong Number and multiple coding questions

Q1. write a program to find a given number is armstrong number or not ?

Q2write a program which accepts a filename as a command line argument and reverse the contents of the file(i.e first character becomes the last character of the file and so on ?

Q3 how can i call a function given its name as a string ?

Q4 How to swap low-order byte and high order byte in an integer without using temporary variable?

Q5 If we develop a project in C, then how can we create an .exe file of it?

Q6 how to print 1 to 100 numbers without using any condition checkings ?


please let me know the answers to my email address as i having the interview in TCS so please please please please let me know

Showing Answers 1 - 64 of 64 Answers

M.Sunil Kumar

  • Dec 5th, 2006
 

main()

{

int amm=0,temp=0,n,x;

printf("enter thealue of n");

scanf("%d",&n);

x=n;

for(;n>0;)

{

temp=n%10;

amm=amm+(temp*temp*temp);

n=n/10;

}

if(x==n)

{

printf("THE GIVEN NUMBER IS AMSTRONG NUMBER");

}

else

printf("NOT AMSTRONG NUMBER");


}

 

pradipc

  • Dec 6th, 2006
 

q6

 show(int i)
      3 {
      4 i&&show(i-1);
      5 printf("%d", i);
      6 }
      7  main()
      8 {
      9 show (10);
     10 }

mekala

  • Jan 8th, 2007
 

    #include<stdio.h>
int main()
{
int i,j,k,l,m;
printf("give the number to find Amswrong (three digit)");
scanf("%d",&i);
j=i/100;
k=i%100;
l=k/10;
m=k%10;
j=j*j*j;
l=l*l*l;
m=m*m*m;
if(i==j+l+m)
{
printf("yes %d is an Amswrong numbern",i);
}
else
printf("not an Amswrong number");
}

malaram

  • Jan 31st, 2007
 

Armstrong number is a number that in a given base is the sum of its own digits to the power of the number of digits.For example:1? + 5? + 3? = 153 To put it algebraically, let be an integer with representation dkdk − 1...d1 in base-b notation. If for some m it happens that then n is a narcissistic number (or an m-narcissistic number).In "A Mathematician's Apology", G. H. Hardy wrote:"There are just four numbers, after unity, which are the sums of the cubes of their digits:153 = 13 + 53 + 33370 = 33 + 73 + 03371 = 33 + 73 + 13and 407 = 43 + 03 + 73. These are odd facts, very suitable for puzzle columns and likely to amuse amateurs, but there is nothing in them which appeals to the mathematician."However, it is not known if the only base 10 numbers equal to the sum of the cubes of their digits are 1, 153, 370, 371, and 407.Some "base ten" Armstrong numbers are: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 153, 370, 371, ... (sequence A005188 in OEIS)Some "base three" Armstrong numbers are: 0,1,2,12,122Some "base four" Armstrong numbers are: 0,1,2,3,313

  Was this answer useful?  Yes

G Siva Prakash Reddy

  • Oct 18th, 2007
 

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

static void reverse_file(char *, unsigned int);

int main(int argc, char **argv)
{
        char *ptr;
        unsigned int size, count = 0;
        FILE *fp;

        if(argc < 2)
        {
                printf("%s <file>n", argv[0]);
                return EXIT_FAILURE;
        }


        if(!(fp = fopen(argv[1], "r")))
                return EXIT_FAILURE;

        fseek( fp, 0L, SEEK_END );
        count = ftell( fp );
        rewind(fp);

        if(!(ptr = malloc(count+1)))
                return EXIT_FAILURE;

        ptr[count] = '';

        fread(ptr, count, sizeof(char), fp);

        fclose(fp);

        reverse_file(ptr, count);

return EXIT_SUCCESS;
}

static void reverse_file(char *base, unsigned int size)
{
        char *end = &base[size-1];
        if(*end == 'n')
        {
                *end = '';
                --end;
        }

        for( ; end >= base; --end)
                if(*end == 'n')
                {
                        *end = ''; 
                        printf("%sn", end+1); /* if you want read into file, using fputs u can */
                }
                printf("%sn", base);
                free(base);
}


     regards,
G. Siva Prakash Reddy

G Siva Prakash Reddy

  • Oct 18th, 2007
 

Keep a table of names and their function pointers:

int myfunc1(), myfunc2();

struct
{
  char *name;
  int (*func_ptr)();
} func_table[] = {"myfunc1", myfunc1,
                  "myfunc2", myfunc2,};


Search the table for the name, and call via the associated function pointer.
         
       regards,
G. Siva Prakash Reddy.

G Siva Prakash Reddy

  • Oct 18th, 2007
 


this program reverse the content of file that is first character becomes the last character of the file so on.. previous posted answer  for the same question, it just reverse the file not the content...


#include<stdio.h>
#include<string.h>

int main( int argc, char **argv )
{
char *fileName;
FILE *fp;
int pos,i = 0, switchCase = 0;

        if( argc == 1 )
        {
                printf("n INCORRECT USAGE n");
                return 0;
        }
fileName = argv[1];

fp = fopen( fileName, "r" );
        if( fp )
        {
                pos = ftell(fp);
                fseek(fp, 0L, SEEK_END );
                pos = ftell(fp);
                char buffer[pos];
                memset( buffer, 0, pos );
                pos = pos - 1;
                        fseek( fp, pos, SEEK_SET );
                while ( pos >= 0 )
                {
                        fseek( fp, pos, SEEK_SET );
                        buffer[i] = fgetc( fp );
                        pos--;
                        i++;
                }
                printf("buffer :: %s n", buffer );

        }

        else
        {
                printf("n give file name doesn't exist n");
        }
return 0;
}

emvusi

  • May 4th, 2008
 

import java.util.*;

public class ArmstrongNos
{
    public astatic void main (String [] args)
    {
       int no = 0;
       int noOfDigits = 0;
       int tmp = 0;
      
       System.out.println ("Enter number");
       Scanner kb = new Scanner (System.in);
       no = kb.nextInt();
       
       tmp = no;
       do
          {
             if (tmp > o)
             tmp = tmp / 10;
             noOfDigits = noOfDigiis + 1;
           }while (tmp > 0);
         
          int quotient = 0;
          int sum = 0;
          int digit = 0;

          do
             {
                digit = quotient % 10;
                sum = sum + digit^(noOfDigits);
          quotient = quotient / 10;
              }while (quotient >0);

          if (sum == no)
             System.out.println(no+ " is an armstrong no");
          else
                System.out.println (no+" is not an armstrong no");
    }
}

banele

  • May 11th, 2008
 

  1. import java.util*.;
  2. public class armstrong number
  3.     {
  4.        public static void main(strings args);
  5.        {
  6.         keyboard scanner = new key board scanner;
  7.         screen wirter = new screen writer;
  8.         int kb writer.next=new kb writer
  9.          {
  10.          int counter =0;
  11.          int sum;
  12.          }       
  13.          system.out.println(please Enter a number);
  14.          }
  15.           whilecounter(i<=o;i++);
  16.         {
  17.          for (i=no;&&no^;i++)
  18.            {
  19.            if (no^base++sum)
  20.             system.out.println(sum+"number is an  armstrong number");
  21.             else
  22.             system.out.println(sum+"number is not an armtrong number");
  23.           }
  24.       }
  25.   }

 

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

static void rev_file(char *, unsigned int);

int main(int argc, char **argv)
{
        char *ptr;
        unsigned int size, count = 0;
        FILE *fp;

        if(argc < 2)
        {
                printf("%s <file>n", argv[0]);
                return EXIT_FAILURE;
        }


        if(!(fp = fopen(argv[1], "r")))
                return EXIT_FAILURE;

        fseek( fp, 0L, SEEK_END );
        count = ftell( fp );  rewind(fp);

        if(!(ptr = malloc(count+1)))
                return EXIT_FAILURE;

        ptr[count] = '';fread(ptr, count, sizeof(char), fp);

        fclose(fp); reverse_file(ptr, count);

return EXIT_SUCCESS;
}

static void rev_file(char *base, unsigned int size)
{
        char *end = &base[size-1];
        if(*end == 'n')
        {
                *end = '';
                --end;
        }

        for( ; end >= base; --end)
                if(*end == 'n')
                {
                        *end = '';printf("%sn", end+1);
                }
                printf("%sn", base);
                free(base);
}

with regards,
 
P.Nirmal

  Was this answer useful?  Yes

mastlogan

  • Aug 21st, 2008
 

#include<conio.h>
#include<math.h>
#include<stdio.h>
main()
{
int a,b,c,d,i;
for(i=500;i>=1;i--)
{
a=(i-(i%100))/100;
b=((i%100)-(i%10))/10;
c=(i%10);
d= (a*a*a)+(b*b*b)+(c*c*c);
if(d==i)
printf("%dn",i);
}
getch();
}

  Was this answer useful?  Yes


void main()
{
int n,r,k,s=0;
clrscr();
printf("n enter the value=n");
scanf("%d",&n);
k=n;
while(n>0)
{
r=n%10;
s=s+r*r*r;
n=n/10;
}
if(k==s)
printf("n the number is armstrong=%d",k);
else
printf("n the number is not armstrong=%d",k);
getch();
}

  Was this answer useful?  Yes

answer 1.
class anisha
{
    void main(int n)
    {
       int a,r,s;
           for(a=n;a>0;a=a/10)      
           {         
                b=a%10;
                s=s=(b*b*b);     
           }
           if(s==n)
            System.out.println(No. is armstrong no);
            else
            System.out.println(No. is not Armstrong);  
        }
}      
           
      

  Was this answer useful?  Yes

raku_024

  • Oct 29th, 2008
 

#include<stdio.h>
#include<conio.h>
void main()
{
int a;
clrscr();
while (printf ("%dt",a++)-4);
getch();
}


posted by:
Rakesh Kumar
KIIT University,
Bhubaneswar,
Orissa,
India
ph: 09776131845


  Was this answer useful?  Yes

#include<stdio.h>
#include<conio.h>
void main()
{
 long int a,b=0,c,d;
 clrscr();
 printf("Enter the value");
 scanf("%ld",&a);
 d=a;
  while(a/10!=0)
  {
  printf("%ld",a%10);
  c=a%10;
  b=b+(c*c*c);
  a=a/10;
  }
  printf("%ld",a);
  b=b+(a*a*a);
  if(b==d)
  printf("nnnumber is armstrong %ld",b);
  else
  printf("nnnumber is not armstrong %ld",b);
 getch();
}

  Was this answer useful?  Yes

#include<stdio.h>
int armstrong(int);
main()
{
int i ,num;
printf("Enetr the
number to which you want to checkn");
scanf("%d", &i);
num=armstrong(i);
if( i
== num)
printf(" the number is armstrongn");
else
printf("The number is not an
armstrong numbern");
}
int armstrong(int i)
{
int r,sum=0;
while(i > 0)
{
r=i %
10;
sum = sum + r * r * r;
i = i / 10;
}
return(sum);
}

  Was this answer useful?  Yes

#include<stdio.h>
#include<conio.h>
void main()
{ int x,a,n,s=0;
clrscr();
printf(“Enter a number”);
scan f(“%d”,&n);
x=n;
while(n>0)
{a=n%10;
S=s+(a*a*a);
n=n/10;
}
If (s==x)
printf(“number is Armstrong”);
Else
printf(“number is not Armstrong”);
getch();
}
Output
Enter a number
1 5 3
Number is Armstrong

  Was this answer useful?  Yes

// C program to check whether a given number is Armstrong or not

#include<stdio.h>
#include<math.h>
int
main ()
{
  int i, n, m, k, cnt = 0, sum = 0, rem;
  printf ("Enter the numbert");
  scanf ("%d", &n);
  m = k = n;
  while (n > 0)
    {
      n = n / 10;
      cnt++;
    }
//printf("number of digits in the number are %d ",cnt);
  while (m > 0)
    {
      rem = m % 10;
      sum = sum + pow (rem, cnt);
      m /= 10;
    }
  if (k == sum)
    printf (" %d is a armstrong number n", sum);

  return 0;
}

  Was this answer useful?  Yes

dilkash

  • Jan 12th, 2011
 

#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
long int rem,sum=0,a,len=0,num,num1,rem1;
clrscr();
printf("Enter Any Numbern");
scanf("%ld",&a);
num=a;
num1=num;
while(a!=0)
{
rem=a%10;
len=len+1;
a=a/10;

}
printf("length=%ld",len);
while(num!=0)
{
rem1=num%10;
sum=sum+(pow(rem1,len));
num=num/10;
}
if(num1==sum)
printf("n%ld is ARMSTRONGn",num1);
else
printf("n%ld is Not ARMSTRONGn",num1);
getch();
}

by Dilkash
from  MANUU

  Was this answer useful?  Yes

Trinesh Baliga

  • Jul 19th, 2011
 

This Program will Print the Armstrong Numbers between 1 to 500

Code
  1.  class Program

  2.     {

  3.        

  4.         static void Main(string[] args)

  5.         {

  6.             int[] number=new int[3];

  7.             int digit1, digit2, digit3,temp;

  8.             string a=null,b=null,c=null;

  9.             int i;

  10.             for (i = 1; i <= 500; i++)

  11.             {

  12.                 string[] k = new string[3];

  13.                 string value= Convert.ToString(i);

  14.  

  15.                 for (int j = 0; j < value.Length; j++)

  16.                 {

  17.                     k[j] = value[j].ToString();

  18.                 }

  19.  

  20.                        if (k[0] != null)

  21.                         {

  22.                             a = k[0];

  23.                         }

  24.                    

  25.                         if (k[1] != null)

  26.                         {

  27.                             b = k[1];

  28.                         }

  29.                    

  30.                         if (k[2] != null)

  31.                         {

  32.                             c = k[2];

  33.                         }

  34.                  

  35.            

  36.                 number[0] = Convert.ToInt32(a);

  37.                 number[1] = Convert.ToInt32(b);

  38.                 number[2] = Convert.ToInt32(c);

  39.                

  40.                 digit1 = number[0];

  41.                 digit2 = number[1];

  42.                 digit3 = number[2];

  43.  

  44.                 temp = (digit1 * digit1 * digit1) + (digit2 * digit2 * digit2) + (digit3 * digit3 * digit3);

  45.                 if (temp == i)

  46.                 {

  47.                     Console.WriteLine("The Arm Strong Numbers are : "+i);

  48.                 }

  49.  

  50.             }

  51.         }

  52.     }

  Was this answer useful?  Yes

khadar.ali

  • Sep 7th, 2011
 


a program to accept a number and find how many digits it contain

//
declare
a number:=&a;
v_le number;
begin
select length (a) into v_le from dual;
dbms_output.put_line('the no of digits is '||v_le);
end;
//

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions