I am doing following code to get the output upto 5 decimal characters of any number inputted by user when divided by 1, I have to use typecast it with (float) , can any one tell me how this can be done without typecasting or using float constant..

``````int main() {
int n;
scanf("%d",&n);
printf("%.5 ", 1/(float)n);
return 0;
}``````

Hello every one
Can I have a better algorithm to find the nth prime number, where 1<= n <=5000000.

for e.g.,
1st prime number is 2.
10th prime number is 29.
100th prime number is 541.
1000th prime number is 7919.
10000th prime number is 104729.
100000th prime number is 1299709.
1000000th prime number is 15485863.

Here is my code:--
[CODE]

include
include

long long arr[100000000]={0};
long long arr1[100000000];
int main() {
long long i,j,num,inc=2,m=0,root,q,k;
start=clock();
num=86028130;
root=sqrt(num)+1;
for(i=0;i<num;i++)arr[i]=i;
for(i=2;i<=root;i++){
if(arr[i]>0) {
for(j=inc*i;j<=num;j=j+i)
arr[j]=-1;
inc++;
}
}
for(i=0;i<num;i++) if(arr[i]>1) arr1[m++]=i;
printf("Enter value of q-");
scanf("%lld",&q);
while(q--){
printf("enter the nth prime number to be found:--");
scanf("%lld",&k);
printf("%lld\n",arr1[k-1]);
}
return 0;
}
[/CODE]

range for q is <= 50000 and k inclusive 1 and 5000000.
time limit for this problem must be 10s.

but time limit for my program is exceeding for q=10.
help me with more efficient algo or techniques.

n=4.
2^4= 2 2 2 * 2;
2 is multiplied four times.
You can use a loop from 1 to n to multiply 2 to n times.

now % is modulas operator which gives the remainder part i.e,
a % b = R as a=bd + R
for ex.
21 % 2 = 1 as 21=2
10 + 1

Use this concept to get the digits.
and in C 16/10 !=1.6 its 1 decimal part is not considered.

so use both the concepts and you will get the solution

All the best... :)

What I am analyzing(as Narue told not to do that,but due to curiosity i did) is that,

for the parenthesis part a's value is assigned to b, but b outside a is not assigned i.e., it's not containing the value that is done in parenthesis.
so it's (b) containing the garbage value and final calculation is printing garbage number
if suppose the code is:--
[CODE]
int main(void) {
int a=10,b=20;
a=a+b-(b=a);
printf("%d",a);
}
[/CODE]

the code is interpreted as a=10+20-(10)=20
i.e, 'b' inside the parenthesis is assigned the value of a but not the 'b' which is outside the parenthesis, i.e, what is the value of 'b' is assigned is the output as the expression is evaluating as:--
a=a+b-(b=a)
=>a=a+b-a
=>a=b

for any value of a and b within the range of int it will give the output..

I hope you have understood the point..:)

The two statements are confusing :--

If the sum is 7 or 11 on the first throw, the player wins.

and

The player loses by rolling a 7 before making the points.

the code is working as per your logic
[CODE]

include
include
include
include

int main ()
{
int i,d1,d2,sumd;
int winf = 0, lostf = 0, winp = 0, lostp = 0;
// printf("This program will simulate the game of craps for 100 times.\n");

for (i=0; i<10; i++) {
d1 = rand()%6+1;
d2 = rand()%6+1;
sumd = d1 + d2;
printf("%d %d sum:-%d\n",d1,d2,sumd);
if (sumd==7 || sumd==11) {
printf("You rolled a 7 or an 11, you win.\n");
winf++;
}
if (sumd==2 || sumd==3 || sumd==12) {
printf("You rolled a 12, a 3, or a 2, you lose.\n");
lostf++;
}
if (sumd==4 || sumd==5 || sumd==6 || sumd==8 || sumd==9 || sumd==10) {
d1 = rand()%6+1;
d2 = rand()%6+1;
sumd = d1 + d2;
if (sumd==7) {
printf("You rolled a 7, you lose.\n");
lostp++;
}
if (sumd==4 || sumd==5 || sumd==6 || sumd==8 || sumd==9 || sumd==10) {
printf("You rolled your points, you win.\n");
winp++;
}
}
}

``printf("First roll wins: %d, First roll loses: %d, Second roll wins: %d, Second roll loses: %d. ", winf, lostf, winp, lostp);``

}

[/CODE]

[QUOTE]if (sumd=7,11)[/QUOTE]

line 18,22,26,34, for or operation the condition will be like this:-
[CODE]if(sumd==7 || sumd==11)[/CODE]

You can do it like this:--;)
[CODE]

include

int getline(char[],int);
main()
{
char a[100],a1,b[100],b1,a2;
int c=0;
getline(a,100);
a1=a2=&a;
b1=&b;
printf("%s\n",a);
while(
a1!=NULL)
{
if(a1>='A' && a1 <='Z' || a1>='a' && a1<='z')
b1=a1+1;
else
b1=a1;
a1++;
b1++;
}
*b1=NULL;
printf("%s",b);
return 0;
}
int getline(char s[], int lim)
{
int c,i;
for(i=0;i<lim-1 && (c=getchar())!=EOF && c!='\n';++i)
s[i]=c;
if(c=='\n') {
s[i]=c;
++i;
}
s[i]='\0';
return i;
}

[/CODE]

We can make a generalized functions with the help of void pointer like this:-
[CODE]

include
include

void abc(void a, int b) {
char
format[] = {"%d ", "%c "};
printf(format[b-1], a);
}
int main()
{
abc(1,1);
abc('A',2);
return 0;
}
[/CODE]

its kind of big endian and little endian thing , means-

These are d terms dat describe the order in which a sequence of bytes are stored in computer memory.

Little Endian means that the lower order byte of the number is stored in memory at the lowest address, and the higher order byte is stored at the highest address. That is, the little end comes first.

For example, a 4 byte, 32-bit integer
s

Byte3 Byte2 Byte1 Byte0

will be arranged in memory as follows:

Example :Intel processors use "Little Endian" byte order.

"Big Endian" means that the higher order byte of the number is stored in memory at the lowest address, and the lower order byte at the highest address. The big end comes first.

Motorola, Solaris processors use "Big Endian" byte order.

Now whats happening here iPtr is a pointer storing integer address.
iPtr will represent the same address as represented by casting it with char i.e, (char)iPtr.
but here we are limiting the value at that address to 1 byte instead of sizeof(int) byte by casting it, and we are returning the value as an int not as a char.
so 1 byte= 8 bits. and that binary conversion , the little endium things (as intel processor) are taken into consideration for value at address of (char
)iPtr and next bit for (char*)iptr+1.

:icon_idea: [U][B][COLOR="red"]MUST GO ...

char* generally stores the address of char type,here it stores the address of int type.
since char occupy 1 byte memory space while int occupy 2 byte or 4 byte(machine & compiler dependent) space in memory. so on increment in p it will refer to next byte incremented by 1 while the next value is 2 or 4 byte from that address of arr . what I mean to say is that.

Suppose arr has an address of 2000 so p=arr means p has an address of arr.
but arr is of type int so arr[1] will be stored in address 2004.
and on increasing the pointer with value 1 it will point to 2001(as it is of char type) which doesn't contain the value arr[1]. so it will result out 0.

you can refer to arr[1] by :-
[CODE]
p=(int)(p+2);//if int take 2 bytes
p=(int
)(p+4);//if int take 4 bytes
[/CODE]

Ummmm self refrential structure is basically nothing but referring to a structure of its own type.

[CODE]
struct node {
int data;
struct node *next;
};
[/CODE]

In the code above , a pointer is used . Pointer is used for storing either a valid address or NULL.
here next is a pointer which will store some address which is of struct node type .
for eg.
in the code:-
[CODE]
int a=5;
int b;
b=&a;
[/CODE]
b is a pointer which will store the address of any integer data.
similarly with
next , it will store the adress of that element whose memory block have an integer space and a space for storing of address of struct node type.

Compiler treats number starting with 0 as octal number and 01234 is an octal number whose decimal value is 668 as you are printing it with %d specifier so it outputs 668.

[CODE]printf("%o",x);[/CODE]

Carrier in C programming is good, but you can go for C++, it supports all features of C along with the features of Object oriented Programming and widely used in software development and technologies.
For C and C++ development check the site.
[URL="http://www.cprogramming.com/"]http://www.cprogramming.com/[/URL]

ProgrammingGeek commented: You currently have 69 posts! I am so immature xD +1
asitmahato commented: good suggestion +1

Segmentation fault is due to an attempt of access of an area of memory that is not allowed to be accessed.
Common causes are:-
Improper format control string in printf or scanf statements.
Forgetting to use & on the arguments to scanf.
Accessing beyond the boundaries of an array.
Failure to initialize a pointer before accessing it.
Incorrect use of the "&" (address of) and "*" (dereferencing) operators.

No dear ,it was not my logic I was correcting his logic and making him understand about the increment.

my code is:
[CODE]

include

int main()
{
int n;
scanf("%d",&n);
printf("sum of 1st odd n terms:-%d",n*n);
return 0;
}
[/CODE]

You need to type cast
[CODE]c=(int*)&b[1];[/CODE]
in b you are storing ascii values.
if you are printing it with %c, you will get the the ascii character in that value.
but when it is to be printed with %d, that ascii character doesn't prints garbage value as you are storing the values in integer pointer.
but if you change it to character pointer, it will work ;)

[CODE]#include
main()
{
char c; char b[10]={1,2,3,4,5,6,7,8,9,0 };
c=&b[1];
printf("\n%d",
c);
}[/CODE]
[CODE]#include
main()
{
char c; char b[10]={1,2,3,4,5,6,7,8,9,0 };
c=&b[1];
printf("\n%c",
c);
}[/CODE]
and you dont need typecasting tooo... ;)

Segmentation fault is due to an attempt of access of an area of memory that is not allowed to be accessed.
Common causes are:-
Improper format control string in printf or scanf statements.
Forgetting to use & on the arguments to scanf.
Accessing beyond the boundaries of an array.
Failure to initialize a pointer before accessing it.
Incorrect use of the "&" (address of) and "*" (dereferencing) operators.

``            merge(a,p,p+q/2,q);``