Showing posts with label c answers. Show all posts
Showing posts with label c answers. Show all posts

Program to reverse a Number C Program

Program to reverse a Number C Program
Please friends to contribute questions of company interviews you attended mail us cinterviews.blogspot.com@gmail.com it will be useful for our job search community.www.cinterviews.com appreciates your contribution

In order to reverse a number you should first input the number and then extract the digits of number one by one. In order to extract digit you can use % operator which results in remainder of division operation. 13%10 is 3. Thus by doing mod 10 you can get rightmost digit of number. After extracting digit you can remove the same from number by storing result of /10 in integer variable. Eg 13/10 is 1.3 but on storing it in integer variable only 1 is stored.

In order to reverse a number using while loop in c language you must run the loop till the number is not 0 that is all the digits haven't been extracted. Assuming that you want reverse of number in variable called y (initial value of y should be zero), you should write y=y*10+extracted digit in while loop.

Consider test input of a=123,y=0

iteration 1
y=0*10+3=3, a=12
iteration 2
y=3*10+2=32, a=1
iteration 3
y=32*10+1=321 a=0

thus we get reverse of number in y.
Below is the code of the Program
 int main()
{
int a;
printf("Enter a number :");
scanf("%d",&a);
int y=0;
while(a)
{
y=y*10+a%10;
a=a/10;
}
printf("\nreverse= %d",y);
getch();
return 0;
}


Please friends to contribute questions of company interviews you attended mail us cinterviews.blogspot.com@gmail.com it will be useful for our job search community.www.cinterviews.com appreciates your contribution

far and near pointers in c

What is "far" and "near" pointers in "c" ?
A:“Near" and "far" pointers are actually non-standard qualifiers that you'll find only on x86 systems. They reflect the odd segmentation architecture of Intel processors. In short, a near pointer is an offset only,which refers to an address in a known segment. A far pointer is a compound value, containing both a segment number and an offset into that segment. Segmentation still exists on Intel processors, but it is not used in any of the mainstream 32-bit operating systems developed for them, so you'll generally only find the "near" and "far" keywords in source code developed for Windows 3.x,MS-DOS, Xenix/80286, etc.

c answers

C answers

C interview question1: What is a stack ?
Answer: The stack is a region of memory within which our programs temporarily store data as they execute. For example, when a program passes parameters to functions, C places the parameters on the stack. When the function completes, C removes the items from the stack. Similarly, when a function declares local variables, C stores the variable's values on the stack during the function's execution. Depending on the program's use of functions and parameters, the amount of stack space that a program requires will differ.

C interview question2:Allocating memory for a 3-D array
Answer: #include "alloc.h"
#define MAXX 3
#define MAXY 4
#define MAXZ 5
main( )
{
int ***p, i, j, k ;
p = ( int *** ) malloc ( MAXX * sizeof ( int ** ) ) ;
for ( i = 0 ; i < MAXX ; i++ )
{
p[i] = ( int ** ) malloc ( MAXY * sizeof ( int * ) ) ;
for ( j = 0 ; j < MAXY ; j++ )
p[i][j] = ( int * ) malloc ( MAXZ * sizeof ( int ) ) ;
}
for ( k = 0 ; k < MAXZ ; k++ )
{
for ( i = 0 ; i < MAXX ; i++ )
{
for ( j = 0 ; j < MAXY ; j++ )
{
p[i][j][k] = i + j + k ;
printf ( "%d ", p[i][j][k] ) ;
}
printf ( "\n" ) ;
}
printf ( "\n\n" ) ;
}
}

C interview question3:How to distinguish between a binary tree and a tree?
Answer: A node in a tree can have any number of branches. While a binary tree is a tree structure in which any node can have at most two branches. For binary trees we distinguish between the subtree on the left and subtree on the right, whereas for trees the order of the subtrees is irrelevant.

Consider two binary trees, but these binary trees are different. The first has an empty right subtree while the second has an empty left subtree. If the above are regarded as trees (not the binary trees), then they are same despite the fact that they are drawn differently. Also, an empty binary tree can exist, but there is no tree having zero nodes.

C interview question4:How do I use the function ldexp( ) in a program?
Answer: The math function ldexp( ) is used while solving the complex mathematical equations. This function takes two arguments, a double value and an int respectively. The order in which ldexp( ) function performs calculations is ( n * pow ( 2, exp ) ) where n is the double value and exp is the integer. The following program demonstrates the use of this function.
#include
void main( )
{
double ans ;
double n = 4 ;
ans = ldexp ( n, 2 ) ;
printf ( "\nThe ldexp value is : %lf\n", ans ) ;
}
Here, ldexp( ) function would get expanded as ( 4 * 2 * 2 ), and the output would be the ldexp value is : 16.000000

C interview question5:Can we get the mantissa and exponent form of a given number?
Answer:The function frexp( ) splits the given number into a mantissa and exponent form. The function takes two arguments, the number to be converted as a double value and an int to store the exponent form. The function returns the mantissa part as a double value. Following example demonstrates the use of this function.
#include
void main( )
{
double mantissa, number ;
int exponent ;
number = 8.0 ;
mantissa = frexp ( number, &exponent ) ;
printf ( "The number %lf is ", number ) ;
printf ( "%lf times two to the ", mantissa ) ;
printf ( "power of %d\n", exponent ) ;
return 0 ;
}
Keywords:
c interview questions
c++ faq
faq on pointers in c
faq on arrays in c
faq videos
c tutorial
faqs on strings in c
steve summit
faq on pointers in c
faq on arrays in c
c++ faq
programming in c textbook
c tutorial
c programming resources
c language reference
cinterviews.com