Page 1 of 1

C noob's Qn:Print prime numbers from 1 to 99

PostPosted: Sun Apr 06, 2014 10:32 am
by tvz
Hello
Please can anyone tell me how can i print prime numbers from 1 to 99 using basic C functions like "sprintf, for, if" in ge?













+1 for anyone who helps me

Re: C noob's Qn:Print prime numbers from 1 to 99

PostPosted: Sun Apr 06, 2014 1:01 pm
by skydereign
This is a pretty common question on the greater internet so you should be able to google for examples. For instance http://stackoverflow.com/questions/5200879/printing-prime-numbers-from-1-through-100
It's a c++ example, but the main logic can be used to get which values are primes. From there you just need to determine how you want to print it out and change it to work in gE. One way of doing this is to have a string buffer and use sprintf to put the found prime integer into the buffer. You can then strcat the buffer to your final string which you print using sprintf.
Code: Select all
// this is only an example of how to strcat a bunch of integers into one string
// needs to be run in a text actor
char final[255] = {0};
char buffer[10] = {0}; // to hold the numbers
int temp = 5;
sprintf(buffer, "%d, ", temp); // set buffer to be the number
strcat(final, buffer); // add buffer to the end of final

temp = 10;
sprintf(buffer, "%d, ", temp); // set buffer to be the number
strcat(final, buffer); // add buffer to the end of final

// and so on


sprintf(text, "%s", buffer); // set the text to show the result
// from this code it should display the string "5, 10, "

Re: C noob's Qn:Print prime numbers from 1 to 99

PostPosted: Mon Apr 07, 2014 8:45 am
by tvz
You are great sky. Thanky you. I did not know about the working of this function but your simple example helped me.
Sorry but, there was a mistake at the end. and i was again thinking what is problem but then i found it
Code: Select all
sprintf(text, "%s", buffer);

The code is printing buffer
I used this
Code: Select all
sprintf(text, "%s", final);

and it worked :D . again thank you
+1