GeekInterview.com
Answered Questions

Sum of two numbers without using arithmetic operators

Asked By: meda_reddy | Asked On: Jul 15th, 2008

Ex:int a=10;int b=10;int sum=a+b;without using "+" operator calculate sum

Star Read Best Answer

Editorial / Best Answer

Answered by: jintojos

View all answers by jintojos

Member Since May-2008 | Answered On : Jul 17th, 2008

void main()
 {
          int a=10,b=20;
          while(b--) a++;
           printf("Sum is :%d",a);  
 }

Answered by: And on: Aug 28th, 2012

For two positive numbers:

int main(){
int a = 10;
int b = 10;

printf("%d",a ^ b | ((a & b)<< 1));
return 0;
}

Answered by: mahamad on: Jun 14th, 2012

Code
  1. #include<stdio.h>
  2. #include<conio.h>
  3. main()
  4. {
  5.     int a=20,b=10,c;
  6.     clrscr();
  7.     c=a-~b-1;             //it will change the sign of operator truly magic
  8.     printf("sum is %d",c);
  9.     getch();
  10.     return 0;
  11. }

Find the binary form

Asked By: abhijeetsingh | Asked On: Apr 1st, 2008

Write a program to find the binary form for the given charcter input.Eg:- for 'a' ascii value is 65 and its binary form is 1000001.

Answered by: kbjarnason on: Jul 1st, 2010

This seems a rather pointless endeavour, as the binary representation of, say, 'a' will vary with the execution character set.    For example, in EBCDIC, 'a' has a value...

Answered by: elexpert_arka on: Aug 31st, 2009

Here is a simple code which will just get each bit of the char by AND operationint main (){    char a; int i,j;    printf("Enter a charn");    sc...

Difference b/w a[i] and i[a]

Asked By: rosuuuuu1010 | Asked On: Feb 3rd, 2008

Is there any relation between a[i] and i[a] while using arrays..Can anyone help me with this....

Answered by: jintojos on: Jun 9th, 2008

Bath a[i] and i[a] are same.....the variable name 'a'  contains the basic address of the  array.so a[i] means base address + index 'i' and also i[a] means base address + index 'i'

Answered by: paragmalshe on: May 29th, 2008

there is no difference in compilation perspective
both are internally converted as *(a+i) or *(i+a) which means the same thing

C program for pyramid

Asked By: vrijesh28 | Asked On: Dec 20th, 2007

What will be the code in C to get the following output?A b C d e f g f e d C b aa b C d e f f e d C b aa b C d e e d C b aa b C d d C b aa b C C b aa b b aa a

Answered by: Jimmy Jack on: Apr 25th, 2012

Code
  1. #include <iostream>
  2. #include <fstream>
  3.  
  4. void display_sequence2(int depth)
  5. {
  6.     int output_count = 0;
  7.     for (int count = depth - 1; count >= 0; --count) {
  8.         for (int index = 0; index <= count; ++index)
  9.             std::cout << (char) ((((output_count++ == 0) || (index == 2)) ? A : a) + index) << " ";
  10.         for (int index = (count == (depth - 1)) ? count - 1 : count; index >= 0; --index) {
  11.             std::cout << (char) ((((output_count++ == 0) || (index == 2)) ? A : a) + index);
  12.             if (index > 0)
  13.                 std::cout << " ";
  14.         }
  15.     }
  16. }
  17.  
  18. int main()
  19. {
  20.     display_sequence2(7);
  21. }
  22.  

Answered by: Gaurav Bhadauria on: Oct 13th, 2011

Code
  1. int i, j, k;
  2. char a[] = { 'A', 'B', 'C', 'D', 'E', 'F', 'G' };
  3.  
  4. for (i = 0; i < 7; i++) {
  5.     for (k = 0; k < 7 - i; k++)
  6.         printf("%c ", a[k]);
  7.     for (j = 7 - (i + 1); j >= 0; j--)
  8.         printf("%c ", a[j]);
  9.     printf("n");
  10. }
  11.  

Is it possible to write a C program without semicolons?

Asked By: kamba | Asked On: Nov 29th, 2007

Answered by: Manali on: Sep 18th, 2011

Code
  1. #include<stdio.h><br /><br />void main()<br /><br /> {<br /><br />   if(printf("no semicolon"))<br /><br /> {}<br /><br /> }


Answered by: rashmi.mohanty on: Sep 10th, 2008

Addition of 2 no without using semicolon:

void main(){  while( !printf("sum value=%d",10+20) ) { }  }

or,

void main(){  if( printf("sum value=%d",10+20) ) { }  }

or,

void main(){  switch( printf("sum value=%d",10+20) ) { }  }

What is the relation between array name and an element number.

Asked By: varun | Asked On: Nov 15th, 2007

Answered by: jintojos on: Jun 9th, 2008

Array name is the base address of the aaray. By using this base address we can refer to the array elements. that is a[0] means "base address + 0th" element. that is array element number is the distence from the base address to the specified data 

Answered by: Joe on: Nov 20th, 2007

The relationship between an array element and the elements index number is as follows.     1. The array name by itself is a constant pointer to the first element of the a...

What is the difference between structure & union?

Asked By: sathishkumar | Asked On: Oct 21st, 2007

How can we multiply this a*b without using "*" operator?

Answered by: sounak pandey on: May 21st, 2013

All the members of the structure can be accessed at once,where as in an union only one member can be used at a time. Another important difference is in the size allocated to a structure and an union. ...

Answered by: the_code on: Aug 24th, 2011

If the only thing is not to use '*' operator then I would prefer below method: A: mutiplier B: multilpicand mul: multiplication mul= A/(1/B); Sample C code: "c #include int mai...

C program to count numbers of positive and negative numbers

Asked By: Tai | Asked On: Oct 12th, 2007

Write a C program using arrays to count the numbers of positive and negative numbers which accepts the inputs as "size of the array" & "elements of the array" and outputs as "number of negative numbers in an array are" & "numbers of positive numbers in an array are" ?

Answered by: kesineni on: Jul 12th, 2009

#include <stdio.h>main(){   int a[] = { 1,-3,-5,-4,-9,2,8};   int len,i,val;   int npos=0, nneg=0;   len = sizeof(a)/sizeof(a[0]);   printf(&quo...

Answered by: jintojos on: Jun 18th, 2008

#include<conio.h>#include<stdio.h>void main()  {      int *arr,num,i=0,pos=0,neg=0;      clrscr();      prin...

What will be the output of the code?#includemain(){int a=10;int b;b=a++ + 20;printf("a= %d b= %d", a, b);}

Asked By: vrijesh28 | Asked On: Oct 9th, 2007

The o/p will be 11 30. Can somebody tell me how?

Answered by: jintojos on: Jul 2nd, 2008

The variable a is incremented by one only after completing
the statement b=a++ + 20;
So after executing the statement b=a++ + 20 the variable b will have the value 30.
Then the value of variable a is incremented that is why the out put is 11 30.

Answered by: amma_mans80 on: Nov 7th, 2007

a = 11 b=30

What is far pointer and where should we use it?

Asked By: Shahed Parvez | Asked On: Sep 11th, 2007

Answered by: kbjarnason on: Jul 1st, 2010

C has no "far" pointers, it only has pointers.Some (generally ancient) compilers, notably for DOS, did offer near, far, and huge pointers.  This was due to how DOS managed memory.Basica...

Answered by: jintojos on: Jun 18th, 2008

Use Of Far Pointer..........Each program has its own data and code segments which is allocated by the compiler.The normal pointer or near pointer can address locations which are in its ...

#includevoid fun(int);void main(){ inta; a=3; fun(a); }void fun(int){ if(n>0) { fun(--n); printf("%d",n); fun(--n); } } what is the output of...

Asked By: sudamadhuri | Asked On: Aug 7th, 2007

0 1 2 0

Answered by: coolquasar on: Sep 15th, 2008

It proceeds like thisfun(3)fun(2)fun(1)fun(0)fun(-1)printf("%d",n)(Here n is 0)fun(-1)printf("%d",n)(Here n is 1)fun(0)printf("%d",n)(Here n is 2)fun(1)fun(0)printf("...

Answered by: jintojos on: Jul 2nd, 2008

The out will be just like 3 2 1 0 2 1 0 1 0 0... not sureThis is a recursive function with 2 recursive calls one at line 13 and other in at line 15.The variable 'a' initial...

Break statement can be simulated by using 1. Go to 2. Return 3. Exit 4. Both return and exit

Asked By: Rujul | Asked On: Mar 9th, 2007

Answered by: jintojos on: Jun 10th, 2008

The Break statement can be simulated by using the "goto" statements.The exit statement is used for exiting the program and the return statement is for returning to the calling function. So b...

Answered by: pbchaudhari on: Aug 11th, 2007

The ans is goto

The fields in a structure of a C program are by default 1. Protected 2. Public 3. PriVATe 4. Can't say

Asked By: Rujul | Asked On: Mar 9th, 2007

Answered by: jintojos on: Jun 10th, 2008

In c language there is no modifiers like protected,public,private......these modifiers are in c++ language.......
In c language the structure fields are public by default that is they can used by any structre instance variabes....

Answered by: div_han on: Mar 13th, 2007

By defalt the members of a structure are public

Write a C program to find biggest of 4 numbers without using relational operators?

Asked By: Arun Reddy.Nare | Asked On: Dec 28th, 2006

Answered by: jintojos on: May 27th, 2008

// Find The Largest Of 4 numbers (Only For Positive Numbers) #include #include void main() { int arr[4],arr2[4],i,count; clrscr(); printf("Enter The Numbers :"); for(i=0;i<4;i++) { scanf("%d",&arr[i]); arr2[i]=arr[i]; } do{ if(arr[0]!=0) { count=0; arr[0]--; } if(arr[1]!=0) { count=1; arr[1]--; } if(arr[2]!=0) { count=2; arr[2]--; } if(arr[3]!=0) { count=3; arr[3]--; } }while(arr[0]!=0 || arr[1]!=0 || arr[2]!=0 || arr[3]!=0); printf("The Largest No Is :%d",arr2[count]); getch(); }

Answered by: Praveen K on: Jan 19th, 2007

n1,n2,n3,n4

if(n1-n2) && (n1-n3) && (n1-n4)

How we print alphabets using while loop?

Asked By: geetasanikop | Asked On: Oct 12th, 2006

Answered by: sivaraju.sateesh on: Jul 20th, 2010

#include<stdio.h>
main()
{
int i=65;
while(i<123)
{
if(i<91||i>96)
printf("%c",i);
i++;
}

}

Answered by: jintojos on: Jun 15th, 2008

#include<stdio.h> #include<conio.h> void main()   {         int alphabet=65;       ...

Printing numbers from 1 to 100 without using condition checking.

Asked By: muhammad nayeemuddin | Asked On: Sep 13th, 2006

Answered by: jintojos on: Jun 16th, 2008

void main() { int count=100,i=1; while(count--) printf(" %d",i++); }

Answered by: jose on: Oct 19th, 2006

Just came up with another solution. It is a 1-liner:cout << "1 2 3 4 5 6 7 ...........................99 100";very simple.

How to reverse a string using array?

Asked By: vineet | Asked On: Sep 10th, 2006

Answered by: Nikhil Saini on: Aug 14th, 2011

simple code ...

Code
  1. #include<stdio.h>
  2. #include<string.h>
  3. #include<conio.h>
  4. main()
  5. {
  6. int i;
  7. char str[100];
  8.  
  9. printf("enter string:");
  10. gets(str);
  11. for(i=strlen(str)-1;i>=0;i--)
  12. printf("%c",str[i]);
  13.  
  14. }

Answered by: Sooraj Kumar on: Aug 3rd, 2011

Code
  1. #include<stdio.h>
  2. #include<string.h>
  3. #include<conio.h>
  4. main()
  5. {
  6. int i;
  7. char str[100];
  8. clrscr();
  9. printf("enter string:");
  10. scanf("%s",str);
  11. for(i=strlen(str)-1;i>=0;i++)
  12. printf("%c",str[i]);
  13. getch();
  14. }

Why do we use header file

Asked By: tanuja | Asked On: Aug 31st, 2006

Answered by: jintojos on: Jun 16th, 2008

Header files contains pre-compiled functions.The similar functions are grouped into same header files. These functions can be used by our programs using the icludederivative. So we can use these heade...

Answered by: uddin2000 on: Sep 13th, 2006

Hi,

   To include the functions which are not changed and which are useful for other functions For ex: I/O functions are included in the header file as they are not a part of your complier. So, we use header files to make our program easier to write

Fibbonaci series program

Asked By: devaraj | Asked On: Aug 11th, 2006

Answered by: jintojos on: May 24th, 2008

 // Fibnocci Series Using Recursion  #include<stdio.h>  #include<conio.h>  void fun(int,int);  int num;  void main()    { clrscr(); p...

Answered by: jintojos on: May 24th, 2008

// Fibnocci Series Using Recursion #include #include void fun(int,int); int num; void main() { clrscr(); printf("Enter The Limit :"); scanf("%d",&num); printf("The Fibnocci Series Up To %d Is...

How to find a given number is armstrong number or not in "c".?

Asked By: sudheer kumar | Asked On: Jul 24th, 2006

Answered by: jintojos on: Jun 16th, 2008

void main() { int num1,num2,sum=0,rem; printf("Enter The Number :"); scanf("%d",&num1); num2=num1; while(num2>0) { rem=num2%10; sum+=rem*rem*rem; num...

Answered by: Deepak kumar Prasad on: Aug 7th, 2006

The check should should be

while ( n>=1) and not (n>=0)
The cotrol enters into an infinite while loop.

How to type a string without using printf function?

Asked By: mohamed arafath | Asked On: Jul 16th, 2006

Answered by: jintojos on: Jun 16th, 2008

Using Video Memory We can write a string into the screen.....that is the string is directly placed into the Video memory...ExampleVideo Memory Address : 0xb0008000 pgm: void main() { int i=0; ...

Answered by: shaanxxx on: Aug 20th, 2006

use write system call
write (1 , "XXXXXXXXXXXXXXX" , strlen("XXXXXXXXXXXXXXX" ));

Between a long pointer and a char pointer , which one consumes more memory? Explain

Asked By: Raji | Asked On: Jun 20th, 2006

Answered by: kbjarnason on: Jul 1st, 2010

As a general rule of thumb, object pointers (as opposed to function pointers) will typically be the same size, as the relevant information - memory address - requires the same size regardless of type....

Answered by: abhimanipal on: Jul 20th, 2009

I am sorry the 2 part of the above answer is wrong.....

I confused long pointer with far pointer

SIze of long pointer and char pointer will be the same

When function say abc() calls another function say xyz(), what happens in stack?

Asked By: gopi_ra | Asked On: Mar 3rd, 2006

Answered by: jintojos on: Jul 7th, 2008

before the control transfered into the called fucntion(ie. xyz()), the values in some of the registers copied into the stack(registers are : BP,SP,CS,IP,falg).also the values of the local variabl...

Answered by: Sunil Soni on: May 5th, 2006

when the function 'abc()' calls another function 'xyz()' then first of all, current status of execution is stored in the STACK i.e. the address of current statement (Which lies in Code segment), then ...

How to find entered number is even or odd without using conditional statement(not using if.. Else,if.. , else if..,while, do... While...., for....)

Asked By: kapilmandge | Asked On: Feb 16th, 2006

Answered by: cageordie on: Jun 17th, 2011

    Assuming i contains the number we wish to evaluate...

    char *oddeven[] = {"even", "odd"};
    printf("number is %sn",oddeven[i%2]);

Answered by: Gitabalakrishnan on: Jun 6th, 2011

modulus operator can be used.

if (i%2)
  printf("Even");
else
 printf("Odd");

Regards,
Geetha B

What is far pointer?

Asked By: ash_k6 | Asked On: Feb 6th, 2006

Answered by: jintojos on: Jul 7th, 2008

Far pointers are mainly used for 32 bit addressing that is the segment offset addressing. Normal pointers can onLy access the memory locations which is allocated by the compiler for that program. Far...

Answered by: mahendra on: Apr 11th, 2006

When the function is present out side the segment, it requires base address and offsetaddress. To access the function, the base and offset addersses are required to specify. This is like the far jum...

What is the difference between #include <file> and #include “file”?

Asked By: Interview Candidate | Asked On: Mar 6th, 2005

When writing your C program, you can include files in two ways. The first way is to surround the file you want to include with the angled brackets < and >. This method of inclusion tells the preprocessor to look for the file in the predefined default location. This predefined default location...

Answered by: jintojos on: Jun 9th, 2008

if you used #include" filename.h" for including header files then the compiler will check only in the current directory.but if you used #include<filename.h> for including header files then the compiler will checks all directories specified in the directory options of turbo c

Answered by: supriya ahire on: Mar 19th, 2006

hi,

     #include searches the file in the specified list of directories while,

    #include"file" searches the file in the current working directory as wel as in the specified list of directories.

How do you redirect a standard stream?

Asked By: Interview Candidate | Asked On: Mar 6th, 2005

Most operating systems, including dos, provide a means to redirect program input and output to and from different devices. This means that rather than your program output (stdout) going to the screen; it can be redirected to a file or printer port. Similarly, your program’s input (stdin) can come from...

Answered by: abhimanipal on: Jan 30th, 2010

Another way to do this is to use the dup system call.

Answered by: jintojos on: Jun 9th, 2008

By using the operators "<" and ">" we can redirect the standard input and out streams.

example : d:> jinto.exe > outputredirected

                d:> jinto.exe < inputredirected

Interview Question

 Ask Interview Question?

 

Career Counselling

 Have Career Question?

 Ask Chandra

 Ask Only Career questions.

Follow us: