So I'm trying to code something that when entered an integer will stop at a certain place.
That is n % 7 == 1;
for example, I'm I input 5, and I want it to stop at n % 7 == 1;
(User inputs 5 stops till 8 which is n % 7 == 1)
#include <stdio.h>
int main ()
{
int nDay, I , nStop;
scanf("%d", &nDay);
nStop = nDay % 7 ==1;
if(nStop != 1)
{
for (i=nDay; i == nDay % 7 == 1; i++)
{
printf("%d", nDay);
}
return 0;
The problem is it is only printed once, is there a way to code this using loops only?
#include <stdio.h>
int main()
{
int num, i = 5;
// printf("Enter the number:\n");
// scanf("%d", &num);
num = 5;
while(i % 7 != 1){
printf("%d\n", i);
i++;
}
return 0;
}
If you feel and problem in understanding the code than just visualize the code using this website http://pythontutor.com/visualize.html#mode=edit
Related
test case:
input: 1234
output: 24
input: 2468
output: 2468
input: 6
output: 6
I have this code:
#include <stdio.h>
#include <math.h>
int main() {
int num;
printf("Enter a number: \n");
scanf("%d", &num);
int numberLength = floor(log10(abs(num))) + 1;
int inputNumberArray[numberLength];
int evenNumberCount = 0;
int even[10];//new even no. array
int i = 0;
do {
inputNumberArray[i] = num % 10;
num = num / 10;
i++;
} while (num != 0);
i = 0;
while (i < numberLength) {
if (inputNumberArray[i] % 2 == 0) {
evenNumberCount ++;
even[i] = inputNumberArray[i];
}
i++;
}
printf("array count %d\n", evenNumberCount);
i = 0;
for (i = 0; i < 8; i++) {
printf(" %d", even[i]);//print even array
}
i = 0;
int result = 0;
for (i = 0; i < 10; i++) {
if (evenNumberCount == 1) {
if (even[i] != 0) {
result = even[i];
} else {
break;
}
} else {
if (even[i] != 0) {
result = result + even[i] * pow(10, i);
} else
break;
}
}
printf("\nresult is %d", result);
/*
int a = 0;
a = pow(10, 2);
printf("\na is %d", a);
*/
}
when I enter number 1234, the result/outcome is 4, instead of 24.
but when I test the rest of test case, it is fine.
the wrong code I think is this: result = result + even[i] * pow(10, i);
Can you help on this?
Thanks in advance.
why do you have to read as number?
Simplest algorithm would be
Read as text
Validate
loop through and confirm if divisible by 2 and print live
next thing, have you try to debug?
debug would let you know what are doing wrong. Finally the issue is with indexing.
evenNumberCount ++; /// this is technically in the wrong place.
even[i]=inputNumberArray[i]; /// This is incorrect.
As the user Popeye suggested, an easier approach to accomplish this would be to just read in the entire input from the user as a string. With this approach, you can iterate through each letter in the char array and use the isdigit() method to see if the character is a digit or not. You can then easily check if that number is even or not.
Here is a quick source code I wrote up to show this approach in action:
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int main()
{
char input[100] = { '\0' };
char outputNum[100] = { '\0' };
// Get input from user
printf("Enter a number: ");
scanf_s("%s", input, sizeof(input));
// Find the prime numbers
int outputNumIndex = 0;
for (int i = 0; i < strlen(input); i++)
{
if (isdigit(input[i]))
{
if (input[i] % 2 == 0)
{
outputNum[outputNumIndex++] = input[i];
}
}
}
if (outputNum[0] == '\0')
{
outputNum[0] = '0';
}
// Print the result
printf("Result is %s", outputNum);
return 0;
}
I figured out the solution, which is easier to understand.
#include <stdio.h>
#include <math.h>
#define INIT_VALUE 999
int extEvenDigits1(int num);
void extEvenDigits2(int num, int *result);
int main()
{
int number, result = INIT_VALUE;
printf("Enter a number: \n");
scanf("%d", &number);
printf("extEvenDigits1(): %d\n", extEvenDigits1(number));
extEvenDigits2(number, &result);
printf("extEvenDigits2(): %d\n", result);
return 0;
}
int extEvenDigits1(int num)
{
int result = -1;
int count = 0;
while (num > 1) {
int digit = num % 10;
if (digit % 2 == 0) {
result = result == -1 ? digit : result + digit * pow(10, count);
count++;
}
num = num / 10;
}
return result;
}
}
You are overcomplicating things, I'm afraid.
You could read the number as a string and easily process every character producing another string to be printed.
If you are required to deal with a numeric type, there is a simpler solution:
#include <stdio.h>
int main(void)
{
// Keep asking for numbers until scanf fails.
for (;;)
{
printf("Enter a number:\n");
// Using a bigger type, we can store numbers with more digits.
long long int number;
// Always check the value returned by scanf.
int ret = scanf("%lld", &number);
if (ret != 1)
break;
long long int result = 0;
// Use a multiple of ten as the "position" of the resulting digit.
long long int power = 1;
// The number is "consumed" while the result is formed.
while (number)
{
// Check the last digit of what remains of the original number
if (number % 2 == 0)
{
// Put that digit in the correct position of the result
result += (number % 10) * power;
// No need to call pow()
power *= 10;
}
// Remove the last digit.
number /= 10;
}
printf("result is %lld\n\n", result);
}
}
Make a program that asks the user for an integer and says if the
number is prime or not. A number greater than 1 is prime if only
is divisible by 1 and by itself. Then, it will tell us what the prime number is.
example:
Enter a number: 8
8 is not first. The first one immediately superior to 8 is 11.
Enter a number: 5
5 is first. The first one immediately above 5 is 7.
I can only solve first part.
Here is my code:
#include <stdio.h>
int main() {
int num, i;
do {
printf("Enter a numer: ");
scanf("%d", & num);
}
while (num < 1);
for (i = 2; i < num; i++) {
if (num % i == 0)
printf("Its prime");
}
if (num % 1 == 0 && num % num == 0)
printf("Not prime");
return 0;
}
Try this logic. Not tested
#include <stdio.h>
int main()
{
int num, i, flag = 0;
printf("Enter a positive integer: ");
scanf("%d", &num);
int isPrime=IsPrime(num)
if(isPrime==0){
numNext=num+1;
int nextPrimeNum=checkNextPrime(numNext);
}
}
int IsPrime(int num){
for(i = 2; i <= num/2; ++i)
{
// condition for nonprime number
if(num%i == 0)
{
flag = 1;
break;
}
}
if (num == 1)
{
flag=1;//neither prime nor composite
}
return flag;
}
int checkNextPrime(int numNext){
int isNextPrime=IsPrime(numNext)
if(isNextPrime==0){
printf("This is the required output :"numNext);
return numNext;
}
else{
numNext=numNext+1;
checkNextPrime(int numNext)
}
}
I am trying to create a C program that reads standard input until it reaches the end-of-input, then store the even numbers to an array and prints it.
I don't know why my program isn't working as intended. Any help would be appreciated.
#include <stdio.h>
int main(){
int num = getchar();
int list[10000];//array to store even nums
int i = 0;
while(num != EOF){
if(num % 2 == 0){
list[i] = num;//store even nums
i++;
}
num = getchar();
}
for(int j = 0;j < i;j++){
printf("%d ",list[j]);
}
return 0;
}
//example output from terminal
Test 8 (1 1 2 3 5 8 13 21 34) - failed (Incorrect output)
Your program produced these 1 lines of output:
10 10 50 10 10 10 56 10 10 50 10 52 10
Last line of output above was not terminated with a newline('\n') character
The correct 1 lines of output for this test were:
2 8 34
The difference between your output(-) and the correct output(+) is:
- 10 10 50 10 10 10 56 10 10 50 10 52 10
+ 2 8 34
The input for this test was:
1
1
2
3
5
8
13
21
34
If you only get one line of input you don't have to store all numbers, just print them:
#include <stdio.h>
int main(void)
{
int num;
while (scanf("%d", &num) == 1) {
if(num % 2 == 0)
printf("%d ", num);
}
putchar('\n'); // from your output it is clear that
// a newline at the end is expected.
return 0;
}
If you have to handle multiple lines:
#include <stddef.h>
#include <stdlib.h>
#include <stdio.h>
int main(void)
{
size_t capacity = 0;
int *numbers = NULL;
size_t count = 0;
int input;
while (scanf("%d", &input) == 1) {
if(input % 2 == 0) {
if(count == capacity) {
capacity += 10;
numbers = realloc(numbers, capacity * sizeof(int));
}
numbers[count++] = input;
}
}
for(size_t i = 0; i < count; ++i)
printf("%d ", numbers[i]);
putchar('\n');
free(numbers);
return 0;
}
getchar() reads only one char at a time, you need to use a function that reads a complete input.
scanf("%d", &num);
or better yet fgets and strtol:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define N 10000
int main(void)
{
int list[N];
int num, i = 0;
char buf[32], *ptr;
while ((i < N) && fgets(buf, sizeof buf, stdin)) {
if (buf[0] == '\n') {
break;
}
num = (int)strtol(buf, &ptr, 10);
if (*ptr != '\n') { // not a valid number
continue;
}
if (num % 2 == 0) {
list[i] = num; //store even nums
i++;
}
}
for (int j = 0; j < i; j++) {
printf("%d ", list[j]);
}
printf("\n");
return 0;
}
I would like to get an output of the biggest even number. but when I input 1 2 3 (3 calls to scanf) the output is 4.
#include <stdio.h>
#include <stdlib.h>
int main() {
int ary[100];
int x, y = 0;
int amount;
scanf("%d", &amount);
fflush(stdin);
for (x = 1; x <= amount; x++) {
scanf("%d", &ary[x]);
if (ary[x] % 2 == 0) {
if (ary[0] < ary[x]) {
ary[0] = ary[x];
}
}
}
printf("%d", ary[0]);
getchar();
return 0;
}
Before the loop initialize ary[0] for example the following way (otherwise uninitialized value of ary[0] is used in the program)
ary[0] = 1;
then substitute these if statements
if(ary[x]%2==0)
{
if(ary[0]<ary[x])
for
if( ary[x]%2==0 && ( x == 1 || ary[0]<ary[x] ) )
And at last write
if ( ary[0] != 1 ) printf("%d",ary[0]);
Take into account that this call
fflush(stdin);
has undefined behavior and should be removed.
In fact there is no need to declare an array. Without the array the program can look like
#include <stdio.h>
int main( void )
{
unsigned int n;
int max_even = 1;
printf("How many numbers are you going to enter: ");
scanf("%u", &n);
int x;
for (unsigned int i = 0; i < n && scanf( "%d", &x ) == 1; i++)
{
if ((x % 2) == 0 && (max_even == 1 || max_even < x))
{
max_even = x;
}
}
if (max_even != 1)
{
printf("maximum entered even number is %d\n", max_even);
}
else
{
puts("None even number was enetered");
}
return 0;
}
Its output might look like
How many numbers are you going to enter: 10
0 1 2 3 4 5 6 7 8 9
maximum entered even number is 8
#include <stdio.h>
#include <stdlib.h>
int main() {
int ary[100];
int ary[0 = 0;
int x, y = 0;
int amount;
scanf("%d", &amount);
fflush(stdin);
for (x = 1; x <= amount; x++) {
scanf("%d", &ary[x]);
if (ary[x] % 2 == 0) {
if (ary[0] < ary[x]) {
ary[0] = ary[x];
}
}
}
printf("%d", ary[0]);
getchar();
return 0;
}
Your code does not work because ary[0] is not yet initialized the first time you compare its value to the value read, furthermore it might not be even for the other comparisons.
You should use an indicator telling you whether an even value has been seen.
Here is a solution:
#include <stdio.h>
#include <stdlib.h>
int main(void) {
int has_even = 0, max_even = 0, value, amount, x;
if (scanf("%d", &amount) != 1)
return 1;
for (x = 0; x < amount; x++) {
if (scanf("%d", &value) != 1)
break;
if (!has_even || value > max) {
max_even = value;
has_even = 1;
}
}
if (has_even)
printf("%d\n", max_even);
else
printf("no even value\n");
getchar();
return 0;
}
I'm writing a C program that counts the number of odd digits from user input.
Eg.
Please enter the number: 12345
countOddDigits(): 3
int countOddDigits(int num);
int main()
{
int number;
printf("Please enter the number: \n");
scanf("%d", &number);
printf("countOddDigits(): %d\n", countOddDigits(number));
return 0;
}
int countOddDigits(int num)
{
int result = 0, n;
while(num != 0){
n = num % 10;
if(n % 2 != 0){
result++;
}
n /= 10;
}
return result;
}
The code is not working.
Can someone tell me where does it go wrong?
There were a few mistakes in your code. Here is a working version of your code:
#include <stdio.h>
int countOddDigits(int n);
int main()
{
int number;
printf("Please enter the number: \n");
scanf("%d", &number);
printf("countOddDigits(): %d\n", countOddDigits(number));
return 0;
}
int countOddDigits(int n)
{
int result = 0;
while(n != 0){
if(n % 2 != 0)
result++;
n /= 10;
}
return result;
}
You are mixing n and num together - there is no need for two variables.
n%=10 is just causing mistakes - you need to check the last digit if(n%2!=0) and then move to the next one n/=10, that's all.
Looping variable is not correct. Your outer loop is
while (num !=0)
but the num variable is never decremented; the final statement decrements the n variable. My guess is you want to initialize
int n = num;
while (n != 0 )
{ ...
n/= 10;
}