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

Anything that doesn't belong in the other forums.

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

Postby tvz » Sun Apr 06, 2014 10:32 am

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
I have not left.
User avatar
tvz
 
Posts: 98
Joined: Sun Mar 10, 2013 7:13 am
Location: Your PC screen
Score: 3 Give a positive score

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

Postby skydereign » Sun Apr 06, 2014 1:01 pm

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, "
User avatar
skydereign
 
Posts: 3510
Joined: Mon Jul 28, 2008 8:29 am
Score: 589 Give a positive score

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

Postby tvz » Mon Apr 07, 2014 8:45 am

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
I have not left.
User avatar
tvz
 
Posts: 98
Joined: Sun Mar 10, 2013 7:13 am
Location: Your PC screen
Score: 3 Give a positive score


Return to Other

Who is online

Users browsing this forum: No registered users and 1 guest