GeekInterview.com
   Home |  Tech FAQ  |   Interview Questions |  Placement Papers |  Tech Articles |  Learn |  Freelance Projects |  Online Testing |  Geeks Talk |  Job Postings |  Knowledge Base | Site Search |  Add/Ask Question

GeekInterview.com  >  Tech FAQs  >  Programming  >  C
Go To First  |  Previous Question  |  Next Question 
 C  |  Question 8 of 92    Print  
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



  
Total Answers and Comments: 10 Last Update: June 11, 2008     Asked by: M.srilatha 
  
 Sponsored Links

 
 Best Rated Answer
Submitted by: pradipc
 

q6

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



Above answer was rated as good by the following members:
nirmaLmani, Anusha N
December 05, 2006 02:16:29   #1  
M.Sunil Kumar        

RE: Q1. write a program to find a given number is arms...

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


}

 


 
Is this answer useful? Yes | NoAnswer is useful 1   Answer is not useful 1Overall Rating: -N/A-    
December 06, 2006 08:08:16   #2  
pradipc        

RE: Q1. write a program to find a given number is arms...

q6

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


 
Is this answer useful? Yes | NoAnswer is useful 1   Answer is not useful 0Overall Rating: +1    
January 08, 2007 04:23:14   #3  
mekala        

RE: Q1. write a program to find a given number is arms...
    #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");
}


 
Is this answer useful? Yes | NoAnswer is useful 1   Answer is not useful 0Overall Rating: +1    
January 31, 2007 02:43:28   #4  
malaram Member Since: January 2007   Contribution: 6    

RE: Q1. write a program to find a given number is arms...
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
 
Is this answer useful? Yes | NoAnswer is useful 0   Answer is not useful 1Overall Rating: -1    
October 18, 2007 00:40:40   #5  
G Siva Prakash Reddy        

RE: Q2. Write 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 ) ?

#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


 
Is this answer useful? Yes | NoAnswer is useful 1   Answer is not useful 0Overall Rating: +1    
October 18, 2007 01:07:14   #6  
G Siva Prakash Reddy        

RE: Q3. How can i call a function given its name as string?
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.

 
Is this answer useful? Yes | NoAnswer is useful 1   Answer is not useful 0Overall Rating: +1    
October 18, 2007 01:34:15   #7  
G Siva Prakash Reddy        

RE: Q2. write 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 ).


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


 
Is this answer useful? Yes | NoAnswer is useful 1   Answer is not useful 1Overall Rating: -N/A-    
May 04, 2008 08:56:05   #8  
emvusi Member Since: May 2008   Contribution: 1    

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

 
Is this answer useful? Yes | NoAnswer is useful 1   Answer is not useful 0Overall Rating: +1    
May 11, 2008 11:37:05   #9  
banele Member Since: May 2008   Contribution: 1    

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

 
Is this answer useful? Yes | NoAnswer is useful 1   Answer is not useful 0Overall Rating: +1    
June 11, 2008 05:24:48   #10  
nirmaLmani Member Since: May 2008   Contribution: 13    

RE: 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
 

#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


 
Is this answer useful? Yes | No


 
Go To Top


 Sponsored Links

 




About Us  |   Privacy Policy  |   Terms and Conditions  |   Contact  |   Site Map  |   Add Question  |   Propose Category  |   RSS Feeds  |   Articles Sitemap  |   Site Updates  |   Add Resource

Copyright © 2005 - 2008 GeekInterview.com. All Rights Reserved
Page copy protected against web site content infringement by Copyscape