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);
Related
#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;
}
The task is:
Write a program that prints the first n perfect numbers. Checking that the number is perfect should be done in the perfect function.
I did it like this:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define MAX 100
/*
*/
int perfect_number(long int n)
{
long int sum=0;
for(int j=1;j<n;j++)
{
long int candidate=j;
if(n%candidate==0)
{
sum=sum+candidate;
}
}
if(sum==n)
{
return n;
}
return 0;
}
int main()
{
int n;
printf("Enter how much perfect numbers you want :");
scanf("%d",&n);
while(n<1)
{
printf("Enter how much perfect numbers you want:");
scanf("%d",&n);
}
int counter=0;
for(long int i=1;counter<n;i++)
{
if(perfect_number(i))
{
printf("%ld ",i);
counter++;
}
}
return 0;
}
The problem arises when I type that I want, the first 5 perfect numbers or more. The program will print only 4 and will continue to work, it will search for numbers but will not print anything.
If I type in the first four perfect numbers, they will print 4 and finish the program, they will do everything right.
I thought the problem was in representing the fifth number, so I replaced int with long int, but that didn't help.
for(int i=0;i<num; i++)
{
char word[32];
scanf(" %[^\n]s",word);
makeDictionary(word, readDictionary);
}
I have a program where I want to ask user for certain strings n times (with spacing allowed). However, when they input for example n = 2, it only loops once and exists. I know there is something wrong with my scanf.
I do Java and I'm a beginner at C. The way strings are done is very different.
This code can help you:
#include <stdio.h>
#include <string.h>
int main()
{
int N;
do
{
printf("Give me number of words :");
scanf("%d",&N);
}while(N<1);
for(int i=0;i<N;i++)
{char str[20];
printf("\nGive me the %d word :",i+1);
scanf("%s[^\n]",str);
printf("%s",str);
}
}
I know this is going to be something of a silly slip or oversight on my behalf, but I can't get the array in this to print out correctly. When I run the code and put in my inputs, I get seemingly random numbers.
For example,
number of rooms was 1
wattage of lights was 2
hours used was 2
TV/computers was 2
The output I got was 3930804. What did I miss?
#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
int main()
{
int room[20] = {0.0};
int i;
int rooms = 0;
char option = 0;
int lights[20];
int hrsUsed[20];
int Telly_Computer[20];
printf("Enter number of rooms");
scanf_s("%d", &rooms);
for(i=0;i<rooms;i++)
{
printf("input wattage of lights");
scanf_s("%d", (lights+i));
printf("input number of hours use/day (average)");
scanf_s("%d", (hrsUsed+i));
printf("input number of TV/Computers");
scanf_s("%d", (Telly_Computer+i));
}
printf("%d \n", lights);
}
printf("%d \n", lights);
You're printing the array directly. You need to loop over it and print the elements one at a time.
int i;
for (i = 0; i < 20; ++i)
printf("%d\n", lights[i]);
You are just printing the address of lights (and using UndefinedBehavior by the way, address must be printed with %p). You must use a loop to print out all of the contents of each array slot.
for(int i=0;i<(sizeof(lights)/sizeof(int));i++)
printf("%d\n",lights[i]);
This is a program that gets numbers input. From the numbers given or inputted, store in an array those numbers only that are even. Input will stop/terminates once 5 even numbers are already stored in the array. So here's my code:
#include <stdio.h>
#include <conio.h>
int main()
{
int num[5];
int x, counter, even[5], numEven=0;
for(counter=0; counter<5; counter++){ //loop for getting the numbers from the user
printf("Enter number: ");
scanf("%d", &num[counter]);
if(num[counter]%2==0){ //storing the even numbers
even[numEven] = num[counter];
numEven++;
}
}
printf("\n\nEven numbers: "); //printing even numbers
for(counter=0; counter<numEven; counter++){
printf("%d, ", even[counter]);
}
getch();
return 0;
}
I have confusion in the part where will I stop the inputting when there's already 5 even numbers stored. Is there something missing? Or am I doing the wrong way? I hope I can get help and suggestions with the code. Thank you very much.
#include <stdio.h>
#include <conio.h>
int main()
{
int x, even[5], numEven = 0;
while (numEven < 5)
{
scanf("%d", &x);
if (x % 2 == 0)
{
even[numEven++] = x;
}
}
printf("\n\nEven numbers: "); //printing even numbers
for(x=0; x<numEven; x++)
{
printf("%d, ", even[x]);
}
getch();
return 0;
}
You keep readin inputs till numEven reaches 5. If the read input is an even number store it in the array and increment numEven.
Use a while loop if the number of times the program will ask the user for input is not fixed and dependent on the user's input.
while (numEven < 5) {
printf("Enter number: ");
scanf("%d", &num[counter]);
if (num[counter] % 2 == 0) {
even[numEven] = num[counter];
numEven++;
}
}