I Know How This Program works this is the 1st one.
#include <stdio.h>
void main()
{
int n;
printf("Enter any number\n");
scanf("%d", &n);
if(n&1==1)
{
printf("%d is an odd number\n", n);
}
else
{
printf("%d is an even number\n", n);
}
}
Program below to 1st program is the 2nd program, in that program, it has been written n&1 instead of n&1==1, this works same as 1st program but the problem is that in this program I have written n&1 instead of n&1==1 still the programs works fine (ho2) and it has same outputs compared to 1st program i was learning that how to use bitwise operator to find whether a given number is odd or even from youtube and i saw this program(2nd program below to 1st program) on youtube.
#include <stdio.h>
void main()
{
int n;
printf("Enter any number\n");
scanf("%d", &n);
if(n&1)
{
printf("%d is an odd number\n", n);
}
else
{
printf("%d is an even number\n", n);
}
}
In C, any non-zero number is considered to be true.
To understand, let's take two small numbers, say 5 and 6. The binary representations are 101 and 110. 1 in three bits is 001.
Now, when we check the second program's condition for 5, we have:
if (101 & 001)
=> if (001)
=> if (true) // Since 001 = 1 != 0
Similarly, for 6:
if (110 & 001)
=> if (000)
=> if (false) // Since 000 = 0
The output should print the statement "Case #(N): I will become a good boy." a certain number of times depending on user input. It should only print prime numbers of N. This is the latest attempt.
#include<stdio.h>
int main(){
int primeNum;
int primeCount;
int primeCheck;
int caseCount=1;
int caseCheck;
scanf("%d", &caseCheck);//gets number of cases
do {
scanf("%d", &primeNum);
primeCheck = 0;
if (primeNum<=1)
{
caseCount++;
}
for (primeCount=2 ; primeCount<=primeNum/2 ; primeCount++)//checks if number is prime
{
if ((primeNum%primeCount) == 0)//checks if number is not prime
{
primeCheck=1;
}
}
if (primeCheck==0)
printf("Case #%d: I will become a good boy.\n", caseCount);//prints if number is prime
} while (caseCount=caseCheck);//while case counter does not exceed number of cases
return(0);}`
The result of this piece of code when the output is 2 [enter] 4 [enter] 2 is "Case #2: I will become a good boy." Why does it not print more than one time and start from number 2?
Just change while (caseCount=caseCheck); to while (caseCount==caseCheck);
The problem with your code is you're not checking the condition. You're just reassigning a value to a variable.
I want to make a code were you find all the even numbers between N number of cases(y). The next y lines will contain a n number (1<=n1<=100). For each line I want to find the even numbers between these. So for example:
input:
2 (number of cases; 1<=y<=10)
1
7
Output:
No even numbers
2 4 6
If there is no even numbers between them then print "No even numbers" for example:
So what I've made up until now is this:
#include <stdio.h>
int main()
{
int n1, n2, i, j, p, y;
printf("number of intervals: ");
scanf("%d", &y);
for(j=1; j<=y; j++)
{
scanf("%d", &n1);
for(i=1;i<=n1; i++)
{
p=i%2;
if(p==0)
printf(" %d", i);
}
return 0;
}
}
The thing is that I dont know how to implement the number of intervals to the code, it only works with two intervals.
Its always a good idea to make a function if you want to do similar thing with different values.You can make a function and call that function as many time as you need.For example
void printInterval(int n1, int n2){ //function will print all even
for(i=n1;i<=n2; i++)//number between n1 and n2 (both inclusive)
{
p=i%2;
if(p==0)
printf(" %d", i);
}
}
Hey guys so I need to make a program which asks the user to enter a number as a argument and then let them know if it is a prime number or 0 otherwise. So the code I have so far is as follows but I am a little confused on how to make it run through all the possible values of the and make sure that it isn't a non-prime number. Right now what happens is that the program opens, I enter a value and nothing happens. Note: I have math in the header as I am unsure if it is needed or not at this stage.
EDIT: SO I MADE THE CHANGES SUGGESTED AND ALSO ADDED A FOR LOOP HOWEVER WHEN I GO TO COMPILE MY PROGRAM I GET AN WARNING SOMETHING ALONG THE LINES OF 'CONTROL MAY REACH END OF NON-VOID FUNCTION'. HOWEVER THE PROGRAM DOES COMPILE WHEN I GO TO ENTER A NUMBER AND HIT ENTER IRRELEVANT OT WHETHER OR NOT IT IS A PRIME NUMBER I GET AN ERROR BACK SAYING 'FLOATING POINT EXCEPTION: 8'.
EDIT 2: THE FLOATING POINT ERROR HAS BEEN FIXED HOWEVER NOW THE PROGRAM SEEMS TO THINK THAT EVERY NUMBER IS NON - PRIME AND OUTPUTS IT THIS WAY. I CAN'T SEEM TO SEE WHY IT WOULD DO THIS. I AM ALSO STILL GETTING THE 'CONTROL MAY REACH END OF NON-VOID FUNCTION' WARNING
#include <stdio.h>
#include <math.h>
int prime(int a){
int b;
for(b=1; b<=a; b++){
if (a%b==0)
return(0);
}
if(b==a){
return(1);
}
}
int main(void){
int c, answer;
printf("Please enter the number you would like to find is prime or not= ");
scanf("%d",&c);
answer = prime(c);
if(answer==1){
printf("%d is a prime number \n",c);
}
else
printf("%d is not a prime number\n",c);
}
1. You never initialized i (it has indeterminate value - local variable).
2. You never call function is_prime.
And using a loop will be good idea .Comparing to what you have right now.
I just modified your function a little. Here is the code
#include <stdio.h>
#include <math.h>
int prime(int a)
{
int b=2,n=0;
for(b=2; b<a; b++)
{
if (a%b==0)
{
n++;
break;
}
}
return(n);
}
int main(void)
{
int c, answer;
printf("Please enter the number you would like to find is prime or not= ");
scanf("%d",&c);
answer = prime(c);
if(answer==1)
{
printf("%d is not a prime number \n",c);
}
else
{
printf("%d is a prime number\n",c);
}
return 0;
}
Explanation-
In the for loop, I am starting from 2 because, I want to see if the given number is divisible by 2 or the number higher than 2. And I have used break, because once the number is divisible, I don't want to check anymore. So, it will exit the loop.
In your main function, you had not assigned properly for the printf() statement. If answer==1, it is not a prime number. (Because this implies that a number is divisible by some other number). You had written, it is a prime number(which was wrong).
If you have any doubts, let me hear them.
I suggest you start with trial division. What is the minimal set of numbers you need to divide by to decide whether a is prime? When can you prove that, if a has a factor q, it must have a smaller factor p? (Hint: it has a prime decomposition.)
Some errors your program had in your prime finding algorithm:
You start the loop with number 1 - this will make all numbers you test to be not prime, because when you test if the modulo of a division by 1 is zero, it's true (all numbers are divisible by 1).
You go through the loop until a, which modulo will also be zero (all number are divisible by themselves).
The condition for a number to be prime is that it must be divisible by 1 and itself. That's it. So you must not test that in that loop.
On main, the error you're getting (control reaches end of non-void function) is because you declare main to return an int.
int main(void)
And to solve that, you should put a return 0; statement on the end of your main function. Bellow, a working code.
#include <stdio.h>
#include <math.h>
int prime(int a)
{
int b;
for (b = 2; b < a; b++) {
if (a % b == 0)
return (0);
}
return 1;
}
int main(void)
{
int c, answer;
printf
("Please enter the number you would like to find is prime or not= ");
scanf("%d", &c);
answer = prime(c);
if (answer == 1) {
printf("%d is a prime number \n", c);
} else {
printf("%d is not a prime number\n", c);
}
return 0;
}
On a side note, don't use the CAPSLOCK to write full sentences. Seems like you're yelling.
Mathematically the maximum divisor of a number can be as a large as the square of it, so we just need to loop until sqrt(number).
A valid function would be:
//Function that returns 1 if number is prime and 0 if it's not
int prime(number) {
int i;
for (i = 2; i < sqrt(number); i++) {
if (a % i == 0)
return (0);
}
return 1;
}
#include<stdio.h>
int main()
{
int n , a, c = 0;
printf ("enter the value of number you want to check");
scanf ("%d", &n);
//Stopping user to enter 1 as an input.
if(n==1)
{
printf("%d cannot be entered as an input",n);
}
for(a = 2;a < n; a++)
{
if(n%a==0)
{
c=1;
break;
}
}
if(c==0 && n!=1)
{
printf("%d is a prime number \n",n);
}
else
{
if(c!=0 && n!=1)
{
printf("%d is not a prime number \n",n);
}
}
return 0;
}
#include <stdio.h>
#include <stdlib.h>
int main()
{
int x,i;
printf("enter the number : ");
scanf("%d",&x);
for ( i=2; i<x;i++){
if ( x % i == 0){
printf("%d",x);
printf(" is not prime number ");
printf("it can be divided by : ");
printf("%d",i);
break;
}[this is best solution ][1]
}
if( i>=x) {
printf("%d",x);
printf(" is prime number");
}
}
I wanted to print the divisors of given range of numbers. It works alright. But when I try to modify it to put **** at the end of the prime number's divisors it acts like bizarre.
#include <stdio.h>
int main()
{
int a,start,rounds,b,c,k=0;
printf("Please enter a number to start :");
scanf("%d",&start);
printf("Please enter how many numbers you want to print from that number :");
fflush(stdin);
scanf("%d",&rounds);
for(a=start;a<=start+rounds;a++)
{
printf("\n\nThe divisors of number :%d are \n",a);
for(b=1;b<=a;b++)
{
c=a%b;
if(!c)
{
k++;
printf("%d\n",b);
}
}
//printf("%d",k);
if((k==2)||(k==1))
printf("***\n");
}
getchar();
return 0;
}
PS:- The trick I used to find a prime number is counting how many printf statement has been executed before loop ends. Is there any wrong with it? When I remove // from printf statement it prints like below.
start=========>k
1 =========>1
2 =========>3
3 =========>5
4 =========>8
5 =========>10
Why is that?
if((k==2)&&(k==1))
There is no way in todays computers that k can be 2 and 1 at the same time. Maybe you meant to say if k is 2 OR k is 1 ?
You need to reset k back to 0 in your outer loop
printf("\n\nThe divisors of number :%d are \n",a);
k = 0;
(And, as others have pointed out, you need an || operator rather than && for the final test)