C program to find euler's number e?
#include <stdio.h>
long factorial(int n) {
long result = 1;
for (int i = 1; i <= n; ++i)
result *= i;
return result;
}
int main ()
{
double n=0;
int i;
for (i=0; i<=32; i++) {
n=(1.0/factorial(i))+n;
}
printf("%.32f\n", n);
}
//2.71828182845904553488480814849027
How do you enter a number as octal in c?
Not sure is what respect, but when you want a variable to be read as an octal put a 0 in front of the number.
a = 01, b = 02; Lets the compiler know that 1 and 2 are to be stored as octals.
To print an unsigned number as an octal use %o.
In c a pointer is a variable that points to or references a memory location in which data is stored. Each memory cell in the computer has an address that can be used to access that location so a pointer variable points to a memory location we can access and change the contents of this memory location via the pointer.
Pointer declaration
A pointer is a variable that contains the memory location of another variable. The syntax is as shown below. You start by specifying the type of data stored in the location identified by the pointer. The asterisk tells the compiler that you are creating a pointer variable. Finally you give the name of the variable. type * variable name Example: int *ptr; float *string;
import java.io.* ;
public class Roman
{
public static void main(String[] args)throws IOException
{
InputStreamReader reader = new InputStreamReader(System.in) ;
BufferedReader br = new BufferedReader (reader) ;
System.out.print("Enter any decimal number between 1 and 59 : ") ;
int n = Integer.parseInt(br.readLine()) ;
String c[] = {"I","II","III","IV","V","VI","VII","VIII","IX","X","L"} ;
String s = "" ;
int t ;
if(n <= 10)
System.out.println("The number '"+n+"' in ROMAN notation is '"+(c[n-1])+"'") ;
else if(n > 10 && n <= 59)
{
int y = n/10 ;
int r = n%10 ;
if(y 0)
s = s+c[10] ;
}
else
{
if(r!=0)
s = s+c[r-1] ;
}
System.out.println("The number '"+n+"' in ROMAN notation is '"+s+"'") ;
}
else
System.out.println("!! FATAL ERROR !! [ No. Greater than 59 ] ") ;
}
}
Program designed to swap the values of two variables?
You'll need 3 variables for this, here's a pseudo code for swapping values of 2 variables.
ALGORITHM SWAP
var1 = 10
var2 = 20
temp = 0
temp = var1 "temp = 10"
var1 = var2 "var1 = 20, originally 10"
var2 = temp "var2 = 10, originally 20"
END SWAP
Do one what has a value of eight A 512 B 32 C 4096 D 83?
If you add up all the digits, A has the value 8.
How do you convert cents to dollars in C without losing decimal places?
d= c/100;
c= c-d*100;
c=245 --> d=2, c=45
What is the smallest number of formal parameters that can be included in a function definition in C?
You can have a function with no parameters.
How do you put battiries in a lazer pointer?
Usually At The Back Or Front Of A Laser Pointer You Can Screw It Off, Just Slot The Batteries In And BAM!!! It SHould Work, Be Carful that you have the right batteries though, they should be the type you get in a watch XD
What is the difference between forward slash and backward slash in c?
forward slash - division operator
backward slash - special character (e.g. \n - newline) in C strings
What are the Advantages of in line function in c plus plus?
Inline functions effectively remove the function call altogether. However, just because a function is declared inline is no guarantee it will be inline expanded. it is merely a hint to the compiler that the function should considered for inline expansion, nothing more.
To understand how inline expansion helps, consider the following code:
void print(int x) {
std::cout << x << std::endl; }
int main()
{
int x=5;
print(x);
x*=3;
print(x);
print(++x);
return(0);
}
If the print() function could be inline expanded, it would be the same as if you'd written the following:
int main()
{
int x=5;
std::cout << x << std::endl;
x*=3;
std::cout << x << std::endl;
std::cout << ++x << std::endl;
return(0);
}
Our code may be a little larger, but it will execute that much faster because we've completely eliminated all three function calls. But while we could expand functions inline manually, as shown above, it makes no sense to do so. In a larger program where the function might be called several dozen times, it becomes that much harder to manage the inline code because there are so many instances of it. By keeping the function and declaring it inline, we can easily modify the function's behaviour because the code is all in one place. More fundamentally, however, if we manually expand functions inline, our code becomes that much larger, and performance is directly related to code size.
By giving the compiler hints as to which functions should be inline expanded, we allow the compiler to decide which is better in each case. The compiler uses complex algorithms to determine the best compromise between code size and performance. A function that is called many times but has little actual code is an ideal candidate for inline expansion, but then so is any function, regardless of complexity, that is called just the once. In the latter case the function may not be absolutely necessary, but functions can help make the code that uses them that little bit more readable and easier to understand, with much less need for commentary.
Even recursive functions can be inline expanded, but usually there is a limit to the depth of recursion (16 being a common limit). This can greatly increase the performance of the function at the expense of code size, which may affect other areas of the program. However, it is not something that should concern the programmer. The compiler is a better judge of what should be expanded and what should not. Even so, some compilers also include a non-standard force_inline flag that can override the compiler's decision, but this is best used sparingly, and only when the programmer is absolutely sure the performance gained will not cause problems elsewhere due to the increased code size.
A backward slash is a slash that tips backwards (\), as opposed to a forward slash that tips forwards (/).
Write a program to find the sum of even numbers from 2 to50?
In Java:
sum = 0;
for (i = 2; i <= 50; i += 2)
sum += i;
Similar (perhaps identical?) in C.
Write a basic program to print even and odd numbers in between 1 to 10?
# include<stdio.h>
void main()
{
int i = 1;
while(i<11)
{
if(i%2==1)
{
printf("\n%d",i);
}
i++;
}
}
Is the main function in C a built-in function or user-defined function?
The main function in C is user-defined. Built-in functions are simply those that do not require a library to be included, but every program must provide a user-defined point of entry; it cannot be built-in. Indeed, most functions in C are user-defined; the built-in functions are mostly operators rather than functions although most do behave like functions. The standard library functions are not built-in either; they all require the inclusion of the appropriate standard library header.
Write a c program to reverse string using static implementation of stack?
#include<stdio.h>
#include<conio.h>
#include<string.h>
int main()
{
char str[100];
int i,temp;
printf("Enter any string : ");
gets(str);
for(i=0; str[i]!=NULL; i++)
{
if(str[i+1]==' ' str[i+1]==NULL)
{
for(temp=i; temp>=0 && str[temp]!=' '; temp--)
printf("%c", str[temp]);
}
printf(" ");
}
getch();
return 0;
}
Clearly, (2272 - 875) = 1397, is exactly divisible by N.
Now, 1397 = 11 x 127
The required 3-digit number is 127, the sum of whose digits is 10.
How do you find the nth number?
Basically the nth number is any possible number in the world!! Generally if you come across a question like this there will be more to it.
For example:
In the sequence 2, 4, 6, 8, 10.... what is the nth term?
This means that they want a general formula for each term. So if x is the term number, the nth term would be 2x.
That's all the information I can give you, I'm sure someone else might be able to help a little more :)
Calloc allocates a block of memory for an array of elements of a certain size?
No. The calloc function allocates a block of memory for a count of a specific type. The size of the type is already known to the compiler so does not need to be specified, it will automatically multiply the type's size by the count. With malloc, you have to allocate memory in bytes, therefore you need to calculate exactly how many bytes you will need for a given type and the number of elements of that type.
Examples (allocate 100 integers):
int* p = (int*) malloc (sizeof (int) * 100);
int* q = (int*) calloc (int, 100);
Note also that malloc does not initialise the memory whereas calloc does (the allocated memory is initialised with the value zero). As such, malloc is more efficient when you want to initialise the memory by copying from other memory. That is, there's no point initialising memory you're going to initialise manually, so long as you don't access that memory before it is initialised.