GeekInterview.com
Series: Subject: Topic:
Question: 137 of 238

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
Asked by: Interview Candidate | Asked on: Nov 20th, 2006
Showing Answers 1 - 22 of 22 Answers
M.Sunil Kumar

Answered On : 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");


}

 

Yes  4 Users have rated as useful.
  
Login to rate this answer.
pradipc

Answered On : 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 }

Yes  1 User has rated as useful.
  
Login to rate this answer.
mekala

Answered On : 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");
}

Yes  1 User has rated as useful.
  
Login to rate this answer.
malaram

Answered On : Jan 31st, 2007

View all answers by malaram

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

  
Login to rate this answer.
G Siva Prakash Reddy

Answered On : 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 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

Yes  1 User has rated as useful.
  
Login to rate this answer.
G Siva Prakash Reddy

Answered On : 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.

Yes  1 User has rated as useful.
  
Login to rate this answer.
G Siva Prakash Reddy

Answered On : 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;
}

Yes  1 User has rated as useful.
  
Login to rate this answer.
emvusi

Answered On : May 4th, 2008

View all answers by emvusi

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");
    }
}

Yes  1 User has rated as useful.
  
Login to rate this answer.
banele

Answered On : May 11th, 2008

View all answers by banele

  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.   }

Yes  1 User has rated as useful.
  
Login to rate this answer.

 

#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 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

  
Login to rate this answer.
mastlogan

Answered On : Aug 21st, 2008

View all answers by mastlogan

#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();
}

  
Login to rate this answer.


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();
}

  
Login to rate this answer.
anishagupta22

Answered On : Oct 25th, 2008

View all answers by anishagupta22

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);  
        }
}      
           
      

  
Login to rate this answer.
anishagupta22

Answered On : Oct 25th, 2008

View all answers by anishagupta22

Answer 6

class abc
{
void main()
{
int a;
for(a=1;a>=100;a++)

{
System.out.println("how r  u");
}

  
Login to rate this answer.
raku_024

Answered On : Oct 29th, 2008

View all answers by raku_024

#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


  
Login to rate this answer.
arora_infosys

Answered On : Dec 9th, 2008

View all answers by arora_infosys

#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();
}

  
Login to rate this answer.

#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);
}

  
Login to rate this answer.

#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

  
Login to rate this answer.
saurabh4mnnit

Answered On : Jun 13th, 2010

View all answers by saurabh4mnnit

// 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;
}

  
Login to rate this answer.
dilkash

Answered On : Jan 12th, 2011

View all answers by dilkash

#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

  
Login to rate this answer.
Trinesh Baliga

Answered On : 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.     }

  
Login to rate this answer.
khadar.ali

Answered On : 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;
//

  
Login to rate this answer.

Give your answer:

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

Related Open Questions

Ads

Connect

twitter fb Linkedin GPlus RSS

Ads

Interview Question

 Ask Interview Question?

 

Latest Questions

Interview & Career Tips

Get invaluable Interview and Career Tips delivered directly to your inbox. Get your news alert set up today, Once you confirm your Email subscription, you will be able to download Job Inteview Questions Ebook . Please contact me if you there is any issue with the download.