i got this exercise where it wants me to create a function to check if a number is "Prime", and than create another function to print how many smaller prime numbers are there from the one i checked. The thing is i need to create a recursive function to check the number of smaller prime numbers using the first function(the one that checks if a number is prime or not). This is what i got so far, and i'm stuck here. The recursive function is confusing for me.
#include <stdio.h>
int main() {
int a;
scanf("%d", &a);
checkPrime(a);
smallerPrime(a);
}
int checkPrime (int number) {
if(number % 2 == 0) {
return 1;
} else {
return 0;
}
}
int smallerPrime (int number) {
if(checkPrime(number) % 2 != 0){
return ;
} else {
return ;
}
}
I saw from the comments,that you actually want to check if a number is even,and if so,you want to know how many smaller even numbers there are using recursion,not prime numbers as mentioned in the title,so my answer will refer that.
You could use something like this:
int smallerPrime (int number) {
static int count = -1; // so that the number itself will not be counted
if(number <1) //excluding 0 or negative numbers
return 0;
if(number !=2) {//we know the number has to be even so no check is needed
++count;
smallerPrime(number - 2);
return count+1; // the +1 is in order to count 2 as well
}
else {
return 0;
}
So for example:
Input 10 would give output 4 (8,6,4,2)
As mentioned by paxdiablo,using recursion here is not the best idea.
In case of very big numbers,your program will probably crash.
Furthermore,note that this code works for positive numbers only,as i am not sure if you want to count anything but them(Negative numbers (like -2,-4 and so on) and 0 are also considered even).
I excluded them here.
In main,you need to put the return value of checkPrime in some variable,and use it to determine if you need to use smallerPrime function.
So you should correct that in your code.
Of course,you can do it all in one function with some small changes.
Related
I am trying to check whether a given number is prime but I've run into an issue. This is the code:
#include <stdio.h>
#include <math.h>
#include <stdbool.h>
bool isPrime(int input)
{
for (int i = sqrt(input); i >= 2; i--)
{
if (input % i == 0)
{
return false;
}
return true;
}
}
int main()
{
int input;
scanf("%d", &input);
if (isPrime(input))
{
printf("Is prime number");
} else
{
printf("Is not prime number");
}
return 0;
}
In the code block of my isPrime function, if I put return true; in the for loop like above, this will be wrong in some cases (for example, when input is 10, it will declare that 10 is a prime number). But if I put return true; outside the for loop, it works fine. So what is the difference?
Let's walk through your loop:
for (int i = sqrt(input); i >= 2; i--)
{
If the input is 10, then i starts out as 3 (remember that when assigning a floating-point value to an int, the fractional portion is discarded). 3 is greater than or equal to 2, so the loop body executes.
if (input % i == 0)
{
The remainder of 10 divided by 3 is not zero, so we do not enter the body of the if statement.
return false;
}
return true;
}
And then we immediately return true.
Because of this, no matter what input you provide, your loop will only iterate 1 time. If input is evenly divisible by the integer value of its square root (such as 9, 16, or 25), then the body of the if statement is executed and it returns false, otherwise it unconditionally returns true.
For your function to work properly, you must move the return true; statement outside the body of the loop - you should only return true when you've exhausted all values of i between sqrt(input) and 2.
The function returns true if the first divisor in the for loop does not divide the target number.
because this return statement
return true;
is inside the for loop.
bool isPrime(int input)
{
for (int i = sqrt(input); i >= 2; i--)
{
if (input % i == 0)
{
return false;
}
return true;
}
}
Moreover the function has undefined behavior if it is called for example for prime numbers 2 or 3 or if a non-positive number is passed to the function.
Placing the return statement
return true;
outside the loop does not make your function correct.
Pay attention to that there is no sense to check even numbers except the number 2 whether they are prime or not prime.
The function can be written the following way
bool isPrime( unsigned long long n )
{
bool prime = n % 2 == 0 ? n == 2 : n != 1;
for ( unsigned long long int i = 3; prime && i <= n / i; i += 2 )
{
prime = n % i != 0;
}
return prime;
}
The function can be called like
unsigned int input;
scanf("%u", &input);
if (isPrime(input))
{
printf("Is prime number");
} else
{
printf("Is not prime number");
}
When you put return true; inside of the loop, that statement will be executed whenever the preceding if condition is false. You only want to return true once you've completed your for loop and have found no divisors. That's why the return statement needs to be outside the loop.
This happens because your for loop isn't over with the complete traverse of i, but the return true statement tells the code to execute it the very first time your if condition goes false, and then exit the loop. So, imagine I give you 10 chocolate bars and tell you to take a bite from each, and only collect the dark ones. You take a bite on the first, it's sweet, you put it away. You take a bite on the second and it's dark. You collect it and you tell me you are done, although you actually aren't (You didn't check the rest of the chocolate bars to see if there are more dark ones there or not). That's what you are doing in your code. Hope the example is clear and helpful :)
my task is to make a sum of digits of a given number (without knowing its size). All I know is that the number is natural positive (including 0), so 0,1,2,3,4....10^x. The number will be given on stdin, so probably scanf is a solution. I know how I would do the sum, but I dont know how to store (some people suggested not to store) this number because even long long might not be enough if the number is too big.
Please answer more accurate, I'm C beginner. Thanks
If the input number can be arbitrarily large, it is indeed better not to try and convert it as a number but to operate on the digits typed. Note that this method always works anyway:
#include <stdio.h>
int main() {
long sum = 0;
int has_number = 0;
for (;;) {
int c = getchar(); // read the next byte from input
if (c >= '0' && c <= '9') {
sum += c - '0'; // add the digit value
has_number = 1;
} else {
if (has_number) {
printf("sum=%ld\n", sum); // output the current sum and
has_number = 0;
sum = 0; // reset the sum to zero
}
if (c == EOF)
break; // stop at end of file
}
}
return 0;
}
The above program can handle very large numbers, up to at least 200 million digits.
I'm a college student in my first year of software engineering. I am in the fourth week of my semester and am having trouble in my programming class. Currently, I was given this assignment in which I was given a function called "getNum()" and I had to use it in another function where the program user, would input a number and the function that I program (must be named isOdd()) would determine if the number is odd or even. Then the main function would print whether the number is odd or even. This is the way that my professor worded it:
" Write a program that uses the getNum() function provided to you in Assignment 2 to get anumber from the user (prompting them first, as always). Create and use a function called isOddwith parameters (the number) and return values (1 ifthe number is odd, 0 if the number is evenOR use a bool or boolean data type, your choice) to determine if thenumber is odd. In main(), tell the user (by displaying using printf()or cout) whether the number is evenor odd."
Now, the problem I am having is understanding programming as I am fairly new to it and some words confuse me, such as parameter and return value. To give you and idea of what I have written so far,
#include <stdio.h>
int isOdd(int numInput);
int getNum(void);
int main(void)
{
int number = 0;
while (number > -1)
{
if (isOdd(0))
{
printf("The number is even.\n");
}
else if (isOdd(1))
{
printf("The number is odd.\n");
}
}
return 0;
}
int isOdd(int numInput)
{
int myNumber = 0;
printf("Please enter a number: ", numInput);
myNumber = getNum();
if (myNumber % 2 == 0)
{
myNumber == 0;
}
else
{
myNumber == 1;
}
return myNumber;
}
#pragma warning(disable: 4996)
int getNum(void)
{
/* the array is 121 bytes in size; we'll see in a later lecture how we can improve this code */
char record[121] = { 0 }; /* record stores the string */
int number = 0;
/* NOTE to student: indent and brace this function consistent with your others */
/* use fgets() to get a string from the keyboard */
fgets(record, 121, stdin);
/* extract the number from the string; sscanf() returns a number
* corresponding with the number of items it found in the string */
if (sscanf(record, "%d", &number) != 1)
{
/* if the user did not enter a number recognizable by
* the system, set number to -1 */
number = -1;
}
return number;
}
This is what I have written, trying to do things accordingly to my professor's instructions, as I do not yet know how to properly use booleans. As you can see, at the bottom is the getNum() function that my professor has said is mandatory for this assignment. As of now, everything I input, I am told is even. I am not asking for you guys to solve and do everything for me but I want to understand what I am doing wrong, what my thinking is doing wrong and to overall better my understanding for future programming. Thank you
It's hard to help you without knowing why you did what you did. A lot of the code is just baffling:
if (isOdd(0))
Why are you passing a zero to isOdd?
printf("Please enter a number: ", numInput);
myNumber = getNum();
Is numInput supposed to be the number they enter or is myNumber supposed to be?
if (myNumber % 2 == 0)
{
myNumber == 0;
}
The statement myNumber == 0 compares myNumber to zero to see if they're equal. It does nothing useful here since you ignore the result of the comparison.
The function:
int getNum(void)
Takes no parameters (void), and returns an integer (int) value. The function itself accepts input from the standard input (stdin) stream - this is normally from the keyboard.
To complete your assignment you should write a function:
int isOdd( int value ) ;
Where value is an integer parameter and the return value is 1 if value is odd and 0 if it is even. Alternatively you are allowed to use the Boolean type:
#include "bool.h"
bool isOdd( int value ) ;
In which case you would return true for odd, and false for even.
Your isOdd() includes the getNumber() call and user prompt code. Not only is that not specified in the assignment, it is poor design making isOdd() difficult to use in more general situations. The assignment explicitly requires you to pass the value to be tested as a parameter.
The assignment does not require you to iterate the test (the while loop is not needed). The user input prompt and acceptance should be in main as follows:
int main(void)
{
printf( "Please enter a number: " ) ;
fflush( stdin ) ; // you'll may this on some platforms
int myNumber = getNum();
if( isOdd( myNumber ) )
{
printf("The number is odd.\n");
}
else
{
printf("The number is even.\n");
}
}
return 0;
}
Note that there are only two possible outcomes, so you do not need an else if( !isOdd( myNumber ) ... - if it is not odd it is implicitly even.
Your isOdd() function will work, but is over complicated. You are required to return a Boolean (a type with two possible values true/false - or an integer 1/0 which can be implicitly converted to a Boolean), and (myNumber % 2 == 0) is a Boolean expression (an expression with two possible results true or false). Anywhere you see the pattern:
if( <boolean expression> )
{
b = true ;
}
else
{
b = false ;
}
you can simply write:
b = <boolean expression> ;
In your case the Boolean determination of odd-ness is simply value % 2 != 0. You can return that directly:
bool isOdd( int value )
{
return value % 2 != 0 ;
}
Natural numbers are the set of positive integers, which ranges from 1 to infinity excluding fractional part. Natural numbers are whole numbers excluding zero. Zero is the only whole number which is not a natural number. An array is special if all the elements are natural numbers. Find whether the given array is special or not.
I've tried to use numbers without using scanf it worked and functioned as its supposed to function.
Ihis is the code I tried to write:
#include <stdio.h>
int main(){
int N,special[N] ;
scanf("%d",&N);
for(N;N>0;N++){
if(special[N]>0){
printf("yes/n");
}else{
printf("no/n");
}
}
return 0;
}
I expect the outout to be yes or no. But the actual output is nothing. There is no output to my code.
There are several issues with the code as pointed out in the comments.
One way of what you want to achieve is:
#include <stdio.h>
int main(){
int N;
int ret = scanf("%d",&N);
if(1 != ret || N < 0)
return 1;
int special[N] ;
for(int i=0;i<N;i++){
ret = scanf("%d", &special[i]);
if(1 != ret)
return 1;
if(special[i] == 0){
printf("array not special\n");
return 1;
}
}
printf("special array\n");
return 0;
}
I am a coding-beginner and would like to hear your advice relating to following solution of this exercise:
Write a program that loops prompting for positive or zero integers of data type long. Then the number of digits the integer consists of (in decimal representation) should be printed to stdout. Entering a negative number immediately stops the program.
Output examples: 0 has 1 digit. 999 has 3 digits. etc.
I've written the code below and according to the tests I did, the program fulfills all given tasks. But what do you think about it? How can I improve it?
(And I also think that I am not allowed to use any finished helpful function in any c-library. It is just 'plain' C coding or so. Idk how to describe it.)
(The programming language is C)
#include <stdio.h>
int main(void)
{
long number;
int n=0;
do
{
printf("Enter a number: ");
scanf_s("%ld", &number);
if (number > 0)
{
while (number != 0)
{
number /= 10;
n++;
}
}
else if(number == 0){
n = 1;
}
else {
exit();
}
printf("The number you've entered has %d digits.\n\n",n);
n = 0;
} while (getchar() != 'EOF');
return 0;
}
That getchar it's useless because EOF it's to say that a file it's over and you're not reading an file. Change that to while(number >=0).