do While loop. It doesn't work - c

I'm really new to C and I can't get this while loop to work. It just exits the loop for no apparent reason.
Here is the code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(void)
{
int min;
int max;
int random;
int guess;
char user='x';
printf("Please insert min number:");
scanf("%d",&min);
printf("Please insert max number:");
scanf("%d",&max);
random = (rand()%(max-min))+ min;
do
{
printf("please insert your guess:");
scanf("%d",&guess);
if(guess==random)
{
printf("YOU WON");
user=='y';
}
else if(guess!=random)
{
printf("UNLUCKY,would you like to play again:x for yes");
user=='x';
}
}while(user=='x');
}

The following is a comparison, not an assignment:
user=='y';
You need to change it to:
user='y';
(Note the single =.)
The same goes for the other assignment.

Related

Display all numbers with a specific digit within the range (100, 1000000)

what's wrong with this code?
It's supposed to get a digit from me, then show all the numbers between 100 & 1000000 containing that digit...
#include <stdio.h>
int main () {
int n,m;
puts("Enter your digit:\n");
scanf("%d\n", n);
int j=100;
while (j<=1000000) {
m=10;
if (j%m==n) {printf("%d\n",j);}
while (j/m>=1) {
if ((j/m)%10==n) {printf("%d\n",j);}
m=m*10;}
j+=1;}
return 0;
}
Try using this code and see how it works
#include <stdio.h>
#include <math.h>
int main () {
int n,m;
puts("Enter your digit:\n");
scanf("%d", &n);
for(int i = 1;(pow(10,i)*n)<1000000;i++)
{
for(int j = 0;j<(pow(10, i));j++)
printf("%lf\t\t", ((pow(10, i)*n))+j);
printf("\n");
}
return 0;
}
When using scanf use the '&' sign after the comma. The passes a pointer to the variable so scanf can store the value.
It is wise to avoid using '\n' inside scanf; so it should look like this:
scanf("%d", &n);

When entering an integer it keeps executing the else statement, trying to search user's input by isdigit if its a number, don't understand why

#include<stdbool.h>
#include<ctype.h>
#include<stdlib.h>
#include<string.h>
int main(void)
{
double InputNum; //needs to be double for test cases
int NumLoops = 11; // loops runs 11 times
printf("Enter number please: ");
scanf_s("%lf", &InputNum);
for (int i = 0; i < InputNum; --NumLoops) //incrementally goes down
{
if (isdigit(InputNum))
{
printf("%lf\n", InputNum+1);
}
else
{
printf("Must be a number!");
exit(EXIT_FAILURE);
}
}
return 0;
}
program incrementally increases by one starting at user's input, this happens 11 times and than ends program, unfortunately it does not do that and keeps printing out the else statement in this code, Any suggestions?
So the problem is that scanf() is converting the integer you enter to a double. So when you type an int for example 5, will be converted to 5.00000, and isdigit() will not convert this to a digit so it always fails.
This is how I would do it.
#include <stdio.h>
#include <stdbool.h>
#include <ctype.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
int main(void)
{
double InputNum; //needs to be double for test cases
int NumLoops = 11; // loops runs 11 times
printf("Enter number please: ");
scanf("%lf", &InputNum);
for (int i = 0; i < NumLoops; ++i)
{
if ((InputNum - floor(InputNum)) == 0)
{
printf("%f\n", InputNum+1);
}
else
{
printf("Must be a number!");
exit(EXIT_FAILURE);
}
}
return 0;
}

Rolling dice with over 7 digits

My aim is to make a program that will throw two dice until the top faces of the two dice total to a number.
However, in my code, i roll a dice with over 7 digits. Help me please, Thank you!
My code:
#include <ctime>
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
int intRandom(int a=1,int b=6)
{
int n;
n=a+rand()%b;
return n;
}
int main()
{
int i,n,x,y; /*n:total, i:count*/
printf("Dice Thrower\n================\n");
printf("Total sought: ");
do
{
scanf("%d",&n);
} while (n<2 && n>12);
{
i=1;
srand(time(NULL));
do
{
x=intRandom(2,6);
y=intRandom(6,2);
printf("Result of throw %d: %d + %d\n",i,x,y);
if ((x+y)==n) printf("\nYou got your total in %d throws!\n",i);
fflush(stdin);
i++;
} while ((x+y)!=n);
}
getch();
}
Change the random number generation to
x=intRandom(1,6);
y=intRandom(1,6);

how to use long long int and remove segmentation fault

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
long long int a[10^9],sum=0;
int n,i,length;
scanf("%d",&n);
for(i=0;i<n;i++)
{
if(0<=a[i]<=10^10)
{
scanf("%lld",&a[i]);
}
}
for(i=0;i<n;i++)
{
sum=sum+a[i];
}
printf("%lld",sum);
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
return 0;
}
i dont know the reason why i am getting the segentation fault this code runs fine for this input 1000000001 1000000002 1000000003 1000000004 1000000005
Issues in your code:
0<=a[i]<=10^10 is not correct, should change to 0<=a[i] && a[i]<=(10^10)
^ is a bitwise xor, not power,
In your for loop, you always compare before read element of a[], so you need to read first, then compare.
use unsigned long long, don't need int at end.
Check this code:
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#define MAX_NUM 1000000000ULL
#define MIN_NUM 0ULL
int main() {
int n,i;
printf("input number count: ");
scanf("%d",&n);
unsigned long long a[n],sum=0;
for(i=0;i<n;i++) {
printf("input number[%d]: ", i);
scanf("%llu",&a[i]);
if(a[i]<MIN_NUM || a[i]>MAX_NUM) {
a[i] = 0;
printf("\t(ignored, due to out of range [%llu, %llu])\n", MIN_NUM, MAX_NUM);
}
}
for(i=0;i<n;i++) {
sum+=a[i];
}
printf("\nsum: %llu\n",sum);
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
return 0;
}

Odd And Even Counter

I want to change this codes to get 20 Numbers from input and count how many are Odd and how many are Even? please can anyone help?!
#include <conio.h>
#include <stdio.h>
int main()
{
int n;
int odd=0;
int even=0;
printf("\nEnter any number \n");
scanf("%d",&n);
if(n%2!=0)
{
printf("%d is an odd number",n);
odd++;
}
else
{
printf("%d is an even number",n);
even++;
}
printf("\n odd%d / even%d",odd,even);
}
Here is a function that solves your problem:
Tip: You need a loop to take your input 20 times.
void countForJHikaam(){
int n,i;
int odd=0;
int even=0;
for(i=0;i<20;i++){
scanf("%d\n",&n);
if(n%2==0){
even++;
}else{odd++;}
}
printf("Odds: %d, Evens: %d",odd,even);
}
It won't really help you in learning. Now go learn what a function is.
#include <conio.h>
#include <stdio.h>
int main()
{
int n;
int odd=0;
int even=0;
printf("\nEnter any number \n");
while(scanf("%d",&n))
(n%2) ? (++odd) : (++even);
printf("\n odd%d / even%d",odd,even);
}
#include<stdio.h>
main()
{
int odd=0,even=0,no,count=20;
printf("Enter the 20 numbers...\n");
here:
scanf("%d",&no);
(no%2==0)? odd++ : even++ ;`
count--;
if(count>0)
goto here;
printf("No of odd numbers... :%d\n",odd);
printf("No of even numbers... :%d\n",even);
}

Resources