Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 11 months ago.
Improve this question
I want to count numbers in int from user input from char to zero. This code make only 1 cycle. (Please help me understand what is wrong in second part.)
void count_down_from(int num);
int main()
{
int start;
char letter;
printf("print letter to reduce to zero in ACSCII");
scanf("%c", &letter);
count_down_from(letter);
return 0;
}
This part working not good:
void count_down_from(int num)
{
if (num > 0)
{
--num;
count_down_from;
printf("%d\n", num);
}
else
return;
}
}
What behaviour expected:
void count_down_from(int num)
{
printf("%d\n", num);
--num;
if (num < 0)
return;
else
count_down_from(num);
}
take a look at my modifications
void count_down_from(int num)
{
if (num >= 0) //need the >= operator to go down to zero not just 1
{
printf("%d\n", num);
--num;
count_down_from(num); //you need to send num to the call otherwise it won't be a recursive function
}
else
return;
}
Missed (num) in function call in second block.
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
#include <stdio.h>
int main() {
int x, i, counter = 0;
printf("Input number!\t");
scanf("%d", &x);
for (i = 0; i <= x; i++) {
if (x % i == 0) {
counter++;
}
}
if (counter <= 2) {
printf("%d is a prime number.", x);
} else {
printf("%d is not a prime number.", x);
}
return 0;
}
It seems the loop part is the problem but I don't know why. I'm very new to programming so please bear with it if its a silly mistake.
Try this Code.
Its going to Infinity when divide by ZERO after giving input. Make
sure loop start with 1, when division inside loop
#include <stdio.h>
int main()
{
int x,i,counter=0;
printf("Input number!\t");
scanf("%d",&x);
for(i=1;i<=x;i++)
{
if(x%i==0)
{
counter++;
}
}
if(counter<=2)
{
printf("%d is a prime number.",x);
}
else
{
printf("%d is not a prime number.",x);
}
return 0;
}
As #Some programmer dude mentioned, you can't do the x%0 because of division by 0 - this occured on first iteration.
So change your loop to starting from 1 like below:
for(i=1;i<=x;i++)
{
if(x%i==0)
{
counter++;
}
}
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
I have an issue with a function I've created. It doesn't return 1. I tried using the code in main instead of in the function and it worked.
I don't know if I'm missing something or what so please check it out and tell me:
int happynum(int n); // THIS IS THE FUNCTION
int main()
{
int num,digito,i,dig,temp,sum=0, lol;
do {
printf("Escribe un numero positivo:\n");
scanf("%d", &num);
} while (num <= 0);
lol = happynum(num); // THIS IS WHAT THE FUNCTION RETURN
printf("%d ", lol);
while(num!=89 && num!=1) //THIS IS THE SAME FUNCTION BUT IN THE MAIN
{
sum=0;
while(num>0)
{
dig=num%10;
num=num/10;
sum=sum+(dig*dig);
}
num=sum;
}
printf("%d", num);
/*
if(num== 1)
{
printf("Happy Number\n");
}
else
printf("UnHappy Number\n"); */
return 0;
}
This is the function:
int happynum(int n) // THIS IS THE FUNCTION
{
int i,dig,num,sum=0;
while(num!=89 && num!=1)
{
sum=0;
while(num>0)
{
dig=num%10;
num=num/10;
sum=sum+(dig*dig);
}
num=sum;
}
return num;
}
You're using the num local to happynum instead of the parameter that you pass. Name the argument num and then use that, like so:
int happynum(int num) // THIS IS THE FUNCTION
{
int i,dig,sum=0;
while(num!=89 && num!=1)
{
sum=0;
while(num>0)
{
dig=num%10;
num=num/10;
sum=sum+(dig*dig);
}
num=sum;
}
return num;
}
You should then probably un-comment the block at the end of your main so you get the Un/Happy Number result.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
Can anyone tell what I am doing wrong here?
Problem statement:
https://practice.geeksforgeeks.org/problems/good-or-bad-string/0
My code:
#include <stdio.h>
#include<string.h>
int is_vowel(char a) {
if(a==97||a==101||a==105||a==111||a==117){
return(1);
}
return(0);
}
int main() {
//code
int t,i;
scanf("%d",&t);
for(i=0;i<t;i++){
char str[100];
scanf("%s",str);
printf("%s",str);
int c_cnsnt=0;
int c_vwl=0;
int g_b=1;//suppose good
for(int j=0;j<strlen(str);j++){
//("%c",str[j]);
int num=is_vowel(str[j]);
printf("Debug %c %d %d\n",str[j],num,strlen(str));
if(is_vowel(str[j])) {
c_vwl++;
}
else { c_cnsnt++;}
if(c_vwl==c_cnsnt){
c_cnsnt=0;
c_vwl=0;
}
else {
if(c_vwl>5||c_cnsnt>=3){
g_b=0;
break;
}
}
}
printf("%d\n",g_b);
}
return 0;
}
Sample
Input:
2
aeioup??
bcdaeiou??
Output:
1
0
My solution link:
https://code.hackerearth.com/9bca55K
Why does the for loop not work for the 2nd string?
Hint: You have to clear the the consonant and vowel counts after increment the other (e.g {c_vwl++;c_cnsnt=0;}), not when they are equal, and always tests your BAD condition.
I will not give you a sample code. Good luck
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
How do you test for primes with the IsPrime method below? I cannot seem to get the printf to work in my IsPrime method and no errors were thrown.
#include <stdlib.h>
#include <stdio.h>
int IsPrime(unsigned int number) {
if (number <= 1) {
return 0; // zero and one are not prime
printf("zero and one are not prime.");
}
unsigned int i;
for (i=2; i*i<=number; i++) {
if (number % i == 0) {
return 0;
printf("not a prime.");
}
}
return 1;
printf("You've found a prime!");
}
int main(void) {
int a;
printf("Please input an integer value: ");
scanf("%d", &a);
if(a >= 1 && a <= 1000) {
printf("You entered: %d\n", a);
IsPrime(a);
}
else {
printf("Error! Please enter a value between 1 and 1000.");
}
}
You're return-ing from function before printf
You wrote printf statement after return; It's an easy fix, just swap the two instructions.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I need to make an array that accepts each number just once and if the user tries to insert a number more than once,then he must enter an other number...can anyone help me please?
I have tried this so far:
#include<stdio.h>
#include<stdlib.h>
int main(void)
{
int a[5];
int i,j,num;
scanf("%d",&num);
a[0]=num;
for(i=1;i<5;++i){
again: scanf("%d",&num);
if(a[i]!=a[i-1])
a[i]=num;
else
goto again;
}
for(i=0;i<5;i++)
printf("%4d\n",a[i]);
system("pause");
return 0;
}
but the code just doesn't work and i don't know why
it is observed from your code that given array must be filled but it should not contain redundant values.
following code iterates until the array is filled with no redundant value, once the array is filled it terminates.
int a[5],i=1,k=0,p;
int num;
scanf("%d",&num);
a[0]=num;
while(i<5)
{
scanf("%d",&num);
for(p=0;p<=k;p++)
{
if(a[p]==num)
{
break;
}
if(p==(k))
{
a[i]=num;
k=i;
i++;
}
}
}
for(i=0;i<5;i++)
{
printf("%d",a[i]);
}
hope this could help you
You are just comparing the new entered value with the previous one in a[i]!=a[i-1].
You better create a function to test the new value with the entire array, like
int valueExists(int num, int a[], int len) {
int i;
for (i = 0; i < len; i++) {
if (a[i] == num) {
return 1;
}
}
return 0;
}
Make sure you adding the new value to the array only after testing the value is not there.
again: scanf("%d",&num);
if (valueExists(num, a, i)) {
goto again;
} else {
a[i] = num;
}
(The loop you created with the goto can be replaced by a do-while loop, but that is not the issue here)