Ask your own question, for FREE!
Computer Science 17 Online
OpenStudy (lgbasallote):

i need help with a C code i'll write the specifications in the comments

OpenStudy (lgbasallote):

1) i'm only allowed to use int, printf, scanf, getch, if/else, and % 2) the user inputs a number and the code will say if it's prime or not prime if you will post full codes please explain them simply for me :D

ganeshie8 (ganeshie8):

hmmm for/while loop not allowed ?

OpenStudy (anonymous):

OK That's easy, writing your code right now !

OpenStudy (lgbasallote):

@ganeshie8 for/while not allowed @Jeet.in pls explain later :)

OpenStudy (anonymous):

sure, @lgbasallote is goto statement allowed?

OpenStudy (lgbasallote):

depends...how will it be used??

OpenStudy (anonymous):

OK i won't be needing it

OpenStudy (lgbasallote):

ahh...so how?

OpenStudy (lgbasallote):

what are these codes :O

OpenStudy (lgbasallote):

you used other codes :(

ganeshie8 (ganeshie8):

recursion !!

OpenStudy (lgbasallote):

recursion?

OpenStudy (anonymous):

Well do you know what's recursion?

OpenStudy (lgbasallote):

no

OpenStudy (anonymous):

Or should I give you a primer?

OpenStudy (lgbasallote):

i'm only allowed to use int, printf, scanf, getch, if/else, and %

OpenStudy (anonymous):

Recursion is a programming technique that allows the programmer to express operations in terms of themselves. In C, this takes the form of a function that calls itself. A useful way to think of recursive functions is to imagine them as a process being performed where one of the instructions is to "repeat the process". This makes it sound very similar to a loop because it repeats the same code, and in some ways it is similar to looping. On the other hand, recursion makes it easier to express ideas in which the result of the recursive call is necessary to complete the task. Of course, it must be possible for the "process" to sometimes be completed without the recursive call. One simple example is the idea of building a wall that is ten feet high; if I want to build a ten foot high wall, then I will first build a 9 foot high wall, and then add an extra foot of bricks. Conceptually, this is like saying the "build wall" function takes a height and if that height is greater than one, first calls itself to build a lower wall, and then adds one a foot of bricks.

OpenStudy (lgbasallote):

we're not allowed to loop!! :(

OpenStudy (anonymous):

@lgbasallote I only used int, printf, scanf, getch, if/else, and %. recursion is a technique, not a syntax !

OpenStudy (lgbasallote):

like i said..loop is not allowed...

OpenStudy (lgbasallote):

also what is this soln=checkPrime(num) and if((num%i)==0 && i <num) { i++; checkPrime(num); return 0;

OpenStudy (anonymous):

No looping Used :) do you see WHILE DO-WHILE or FOR, GOTO-LABEL in my code? It strictly adheres to your question !

OpenStudy (lgbasallote):

uhmm?

OpenStudy (anonymous):

yup. and that's a function i.e. breaking the code into smaller parts !

OpenStudy (anonymous):

Read this: http://www.cprogramming.com/tutorial/lesson4.html

OpenStudy (anonymous):

Well don't I deserve some medals? Clicking the top right button with a zero on it helps !

ganeshie8 (ganeshie8):

@lgbasallote try this : #include<stdio.h> main() { int n, c = 2; printf("Enter a number to check if it is prime\n"); scanf("%d",&n); check_factor: if ( n%c == 0 ) { printf("%d is not prime. %d\n", n, c); goto last; } c++; if (c <= n-1) { goto check_factor; } if ( c == n ) printf("%d is prime.\n", n); last: return 0; }

OpenStudy (anonymous):

Thanks @ganeshie8 !

OpenStudy (lgbasallote):

what does check_factor mean?

ganeshie8 (ganeshie8):

it is a label name. we can label any part of the code. il try to comment the whole code.. wait ;)

OpenStudy (lgbasallote):

yay thanks

OpenStudy (anonymous):

@lgbasallote make sure you understand @ganeshie8 's method and don't just copy ! You need to learn how to code correctly, submitting assignments won't lead you anywhere ;)

OpenStudy (lgbasallote):

hah lol of course..im not interested in the code...im just interested to know how it applies

OpenStudy (lgbasallote):

what i really need is that fibonacci code which i still dont get :/ im hoping to get some clue from here

OpenStudy (lgbasallote):

hmm speaking of which maybe i can ask for an explanation of that code later hmm

ganeshie8 (ganeshie8):

#include<stdio.h> main() { int n, c; /* input number, counter checking factors */ printf("Enter a number to check if it is prime\n"); scanf("%d",&n); check_factor: /* just a label. section of code to check if c is a factor of n */ if ( n%c == 0 ) { printf("%d is not prime. %d\n", n, c); goto last; } c++; /*increment factor counter c */ if (c <= n-1) { /*jump to check_factor until c is less than n*/ goto check_factor; } if ( c == n ) /*we are here still. we didnt jump to last after checking factor for every number < n. means its a prime ! */ printf("%d is prime.\n", n); last: return 0; }

OpenStudy (lgbasallote):

hmm..correct me if im wrong but you used loop?

ganeshie8 (ganeshie8):

@lgba ping @shubhamsrg he is good in fibonacci :)

OpenStudy (lgbasallote):

wait...im gonna post a code here (it's not the fibonacci)

ganeshie8 (ganeshie8):

i used a jump statement : "goto" * to jump to top if c < n

OpenStudy (lgbasallote):

okay here's a code we did for testing odd or even...this prime or not prime is a seatwork together with this one #include <stdio.h> #include <stdlib.h> #include<conio.h> main() { int a, b; printf("Enter a number: \t"); scanf("%d", &a); b = a%2; if (b==0) { printf("The number is even"); } else { printf("The number is odd"); } getch(); } so maybe the prime or not prime might look like that?

OpenStudy (lgbasallote):

btw this was what i meant by the specification..a real simple one :)

OpenStudy (anonymous):

Nope, prime is a much more complex process !

OpenStudy (lgbasallote):

the hint my teacher gave us was using modulo

OpenStudy (anonymous):

yup were using modulo arithmetic that's the % symbol

OpenStudy (lgbasallote):

hmm i dont think this is possible using those simple codes only eh?

OpenStudy (anonymous):

yup ! true your teacher wanted to test your intelligence !

OpenStudy (lgbasallote):

lol that tricky !@#$% haha jkjk

OpenStudy (anonymous):

yup ! actually it's not your fault. Your teacher should have taught you the basics first, he/she seems to be skipping topics !

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!