Determined the PRIME numbers - c

Is it 1 & 0 are prime numbers ? because when i Input 1 & 0 it says that it is a PRIME
#include <stdio.h>
int main(){
int num, i,y = 0;
printf("Enter a Number: ");
scanf("%d",&num);
for(i = 2; i <= num /2; ++i){
if(num % i == 0){
y=1;
}
}
printf("the number %d is a ",num);
if (y == 0){
printf("(PRIME)");
}
if(num % 2 == 0){
printf("(EVEN)");
}else
printf("(ODD)");
printf(" Number.");
}
can anybody help me with my code

No, neither 0 nor 1 are considered to be prime numbers, that is, the lowest prime number is 2. You need to change the code into, for instance:
if (y == 0 && n >= 2)
this covers both 0 and 1 along with negative integers (thanks to #MikeCAT)

Related

Issue in calculating LCM of two -ve numbers or if either one is negative in C lang

The code is running perfectly for finding
LCM of 2 and 3 , LCM of 0 and 2 BUT
not able to execute for (-2 and 3) or (-2 and -3). I have written code for this type.
Check else if block that's the problem.
I expect LCM of -2 and -3 to get printed as 6. And LCM of -2 and 3 to get printed as 6.
#include <stdio.h>
int main()
{
int a,b;
printf("\n\t\t\t\t\tThis program calculate LCM of two numbers");
printf("\nEnter two numbers: ");
scanf("%d%d",&a,&b);
int i=0;
if(a!=0 && b!=0)
{
if(a>0 && b>0)
{
for(i=1; i<=a*b; ++i)
{
if((i%a==0)&&(i%b==0))
break;
}//for loop end
printf("LCM is %d",i);
}
else if(a<0 || b<0) //if any one number is -ve or both are -ve
{
// while(1)
// {
// int max = (a > b) ? a : b;
// if ((max % a == 0) && (max % b == 0))
// {
// printf("The LCM of %d and %d is %d.", a, b, max);
// break;
// }
// ++max;
// }
//Above commented portion not working for -ve numbers. This is my issue.
}
}
else
{
printf("LCM is %d",i);
}
return 0;
}
Potential infinite loop
When (max % a == 0) && (max % b == 0) is not true, loop goes on forever.
// OP's code
while(1) {
int max = (a > b) ? a : b; // Did OP want this before the loop?
if ((max % a == 0) && (max % b == 0)) {
printf("The LCM of %d and %d is %d.", a, b, max);
break;
}
++max; // This line serves no purpose.
}
Since OP wants a positive result, consider 1) using the absolute value of both arguments. 2) Use long long math to avoid int overflow in the absolute value and in a*b.
There are faster approaches than iterating [1...a*b].

Guess a number game , How to add or display number of guesses left using while loop

Number guessing game which a user guess a number from 0-20 and i want it to display how many tries are left for the user for example the maximum tries is 5 and if the user got it wrong on the first try it will display something like "tries left : 4", how do i implement that in my code?
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
int main()
{
time_t t;
srand((unsigned)time(&t));
int randomNumber = rand() % 21;
int num;
int guess = 0;
printf("\nThis is a guessing game.");
printf("\nGuess 5 times only\n");
printf("\nEnter your guess:");
scanf("%d",&num);
while (num != randomNumber && guess < 5){ //checks if num is equal or not to randomnumber and count is less than 5 or limit 5
if(num > randomNumber){ // checks if num is greater than randomNumber
printf("Too high! try again:");
scanf("%d",&num);
guess++;
}
if(num < randomNumber){ //checks if num is less than randomNumber
printf("Too low! try again:");
scanf("%d",&num);
guess++;
}
if(num == randomNumber){ //checks if num is equal to randomNumber
printf("You got it right!\n");
return 0;
}
if(guess == 5) //checks if tries is 5 then exits program.
{
printf("your out guess of guesses!\n");
return 0;
}
}
return 0;
}
You can add this inside your while loop (at the end)
// For total no of guesses = 5, if that is n then n - 1 - guess
printf("tries left %d\n", 4 - guess);
If you’re trying to print a value in a loop something along the lines of printf(“You have %d guesses left\n”,5-guess); might work
You already have an answer, but I would advise you using symbolic constants #define GUESS_NUM 4 instead of actual numbers in your statements and your program in general, in programs like this it makes no difference, but you can get confused very easily in larger projects.
Also, your loop should end when guess reaches 4 because you already have one guess before the loop. You could write something like this:
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#define GUESS_NUM 4
int main()
{
time_t t;
srand((unsigned)time(&t));
int randomNumber = rand() % 21;
int num;
int guess = 0;
printf("\nThis is a guessing game.");
printf("\nGuess 5 times only\n");
printf("\nEnter your guess:");
scanf("%d",&num);
while (num != randomNumber && guess < GUESS_NUM){ //checks if num is equal or not to randomnumber and count is less than 5 or limit 5
if(num > randomNumber){ // checks if num is greater than randomNumber
printf("tries left : %d\n", GUESS_NUM-guess);
printf("Too high! try again:");
scanf("%d",&num);
guess++;
}
if(num < randomNumber){ //checks if num is less than randomNumber
printf("tries left : %d\n", GUESS_NUM-guess);
printf("Too low! try again:");
scanf("%d",&num);
guess++;
}
if(num == randomNumber){ //checks if num is equal to randomNumber
printf("You got it right!\n");
return 0;
}
if(guess == GUESS_NUM) //checks if tries is 5 then exits program.
{
printf("your out guess of guesses!\n");
return 0;
}
}
return 0;
}

Factoring Pointer Error in C programming

So i'm having trouble making a program that asks the user to enter a number and then using that number I must increase the value of the pointer towards two_count and three_count. These are counter the factors of two's and three's in the number entered.
For example if the user input 2, then the program should spit out
"There have been 1 factor of 2 and 0 factors of 3"
Then the user can input 0 to exit program
What I have so far is
include <stdio.h>
void main()
{
int* two_count;
int* three_count;
int num;
while(two_count >= 0 || three_count >= 0)
{
printf("Enter a number: \n");
scanf("%d", &num);
if(num % 2)
{
two_count++;
}
else if(num % 3)
{
three_count++;
}
else if(num == 0)
{
printf("Thank you for playing, enjoy your day!\n");
break;
}
printf("So far, there have been %d factors of 2 and %d factors of 3\n", two_count, three_count);
}
}
Thanks!
If you want to use pointers, you can do it like this
int two_count = 0;
int* two_count_ptr = &two_count;
int three_count = 0;
int* three_count_ptr = &three_count;
Then, for retrieving value and incrementing you would need to dereference the pointer
while(*two_count_ptr >= 0 || *three_count_ptr >= 0)
(*two_count_ptr)++;
Hope this helps.

Printing all the armstrong numbers between 1 - 999 in c

If i want the output like:
1st Armstrong number = 0
2nd Armstrong number = 1
3rd Armstrong number = 153
.............................................
.............................................
20th Armstrong number = ....
here my question is : if i have to print many armstrong numbers(1st to 20th) then is it the proper way to write printf one by one ? then i need to much time & code will be so long,how i minimize it?
please help....
This is my code which is able to find first 6 Armstrong Number..
int main(){
int a, b, c, num, i=0,j=0;
printf("Printing all the armstrong numbers between 1 - 999");
while(i<=999)
{
a= i/100;
a= a*a*a;
num= i%100;
b= num/10;
b= b*b*b;
c= num%10;
c=c*c*c;
if(i==a+b+c)
{
j++;
if(j==1) printf("\n1st");
else if(j==2) printf("\n2nd");
else if(j==3) printf("\n3rd");
else if(j==4) printf("\n4th");
else if(j==5) printf("\n5th");
else if(j==6) printf("\n6th");
printf(" Armstrong number= %d",i);
}
i++;
} // end of while
return 0;
} // end of main
It's simple :
if(i==a+b+c)
{
j++;
int key = j % 10;
if(j == 11)
key = 11;
switch(key){
case 1:
printf("\n%dst Armstrong number= %d",j,i);
break;
case 2:
printf("\n%dnd Armstrong number= %d",j,i);
break;
case 3:
printf("\n%drd Armstrong number= %d",j,i);
break;
case 11:
default:
printf("\n%dth Armstrong number= %d",j,i);
}
}
It appears that the rule for ordinal numbers is as follows:
x % 10 == 1: *st
x % 10 == 2: *nd
x % 10 == 3: *rd
Otherwise: *th
Let's write this up in code:
const char * format =
(x % 10 == 1) ? "%dst armstrong number: %d\n" :
(x % 10 == 2) ? "%dnd armstrong number: %d\n" :
(x % 10 == 3) ? "%drd armstrong number: %d\n" :
"%dth armstrong number: %d\n" ;
printf(format, j, i);
Here's my solution. Try to keep your problems divided rather than trying to solve them all in one function. I hope this helps.
#include <stdio.h>
#include <stdbool.h>
#include <stdint.h>
#define CUBE(n) (n*n*n)
char* getCountSuffix(uint16_t n) {
n %= 100; // We don't care about the hundreds place
if(n >= 10 && n <= 20) { // 10-19 always use "th" ("tenth", "eleventh", "twelveth", etc.)
return "th";
}
n %= 10;
switch(n) {
case 1:
return "st";
break;
case 2:
return "nd"; // edit: was "nt"
break;
case 3:
return "rd";
break;
default:
return "th";
}
}
bool isArmstrong(uint16_t n) {
uint16_t hundreds = n / 100;
uint16_t tens = (n % 100)/10;
uint16_t ones = n % 10;
return (CUBE(hundreds) + CUBE(tens) + CUBE(ones)) == n;
}
int main() {
size_t i, count;
for(i = 0, count = 1; i < 1000; i++) {
if(isArmstrong(i)) {
printf("%u%s. %u\r\n", count, getCountSuffix(count), i);
count++;
}
}
return 0;
}
You find a pattern and make use of that pattern. Find a program that converts Roman Numerals to numbers, see how they are extracting a pattern and achieving it.
PS: You are mixing up the presentation and implementation. Implementation should compute the armstrong number and should pass it to another method for display, which can keep track of it and display it whatever way required. Displaying is not the problem you want to solve here and I would propose dont spend too much time for this 1st, 2nd, 3rd cases.
Use printf's formatting capabilities. Replace all those else if printfs with:
printf("\n%dth", j);
The integer j will be substituted for %d. If you need to use (1st, 2nd, 3rd, 4rth) nd, th and st then have a few if statements to decide with one will be used, then printf using that one.
int sum = 0;
int count;
count = int.Parse(Console.ReadLine()); //Armstrong numbers from 0 to count
Console.WriteLine();
Console.Write(" Armstrong numbers from 0 to " + count + " are: ");
for (int c = 0; c <= count; c++)
{
sum = 0;
for (int i = 1; i <= c; i *= 10)
{
if (c / i % 10 >= 1)
{
int cube = (c / i % 10);
sum += cube * cube * cube;
}
}
if (sum == c)
{
Console.Write(sum + " ");
}
}

converting integers to binary

I am trying to learn how to convert ints into binary. it runs but this is the output: Enter a number: 33
New value: 16
Remainder: 1
Current VAlue: -17
Counter: 1
I appreciate any help. Thank you. Ok I am sorry my bad. The output should be: 00100001
#include <stdio.h>
int main()
{
int nv, r, num;
printf("Enter a number: ");
scanf("%d",&num);
int counter=0;
while(num>=0)
{
nv=num/2;
r=num%2;
num=-(nv+r);
counter++;
printf("New Value: %d\n",nv);
printf("Remainder: %d\n",r);
printf("Current Value: %d",num);
}
printf("Counter: %d\n",counter);
}
num=-(nv+r);
Is obviously negative, since both nv and r are positives.
I suspect you actually wanted
num = nv
or:
num -= (nv + r)
Also note that your stop condition is num >= 0 - if you do the first change, you will get an infinite loop, since when you reach num ==0, you will divide by 2, and get nv == num /2 == 0 / 2 == 0 and assign nv back to num
(*)Note that also the second change will proide infinite loop: 0 % 2 == 0 and 0 / 2 == 0, so num -= (nv + r) == 0 - (0 + 0) == 0
Is this what you were trying to accomplish?
#include <stdio.h>
int main()
{
int nv, r, num;
int counter=0;
printf("Enter a number: ");
scanf("%d",&num);
while(num>0)
{
nv=num/2;
r=num%2;
num-=(nv+r);
counter++;
printf("New Value: %d\n",nv);
printf("Remainder: %d\n",r);
printf("Current Value: %d\n",num);
}
printf("Counter: %d\n",counter);
return 0;
}
One easy way to do that is know that the machine store the number in binary. And what you need to do is only use this to print the number in binary.
int main()
{
int val=1;
int n=0;
int num;
printf("Enter a number: ");
scanf("%d",&num);
while(val <= num)
{
if(val & num) printf("bit %d is '1'\n", n);
else printf("bit %d is '0'\n", n);
n++;
val<<=1;
}
}
In this case the order is from the least significant to the most significant bit.

Resources