Finding even numbers using for loop in C language [closed] - c

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 1 year ago.
Improve this question
Can someone please show or tell how to fix my code so that it can determine even numbers. Am new to for loops and C.
EDIT: please paste code here instead of image.

You could have just posted the code along with your question but man I could hardly understand your code so I'll make some silly assumptions about what you were trying to do.
1)
printf("even number is %d",);
Firstly I am doubly sure that this did not compile because of the hanging comma, number two removing the comma will either print rubbish or cause the entire program to crash because that %d is supposed to be accompanied by an integer argument for example:
printf("even number is %d", a);
It has to do with the way C handles variadic arguments.
2)
Instead of handling the input gotten from scanf you went on to check if i is an even number which makes no sense, of course the if statement there checks if it is an even number but then there's no need for asking for input.
3)
I suggest changing your code to this:
#include <stdio.h>
int main()
{
int a, i = 0; //remove one of the variables
printf("Enter 10 positive values\n");
while (i < 10)
{
printf("Enter a number:");
if(scanf("%d", &a) != 1) //ask for input first and check if scanf failed
{
printf("An error occured\n"); /*now scanf will push back the string or whatever it read back in the stdin stream
if it encounters a non integer value ('d' for example) and return 0 if this happens ther's something you can do about that but I'm not going to bother going into that
now such an error can happen because of other reasons do this research on your own*/
return 1;
}
if (a < 0) //check if a is positive
{
printf("%d is not positive please enter an even number\n", a); //print an error message if a is not positive
continue; //go back to the beginning of the loop
}
else
{
if ((a % 2) == 0)
{
printf("%d is an even number\n", a);
}
else
{
printf("%d is not an even number\n", a);
}
i++;
}
}
printf("Done!\n");
return 0;
}

Related

Continue statement in If Else condition gives infinite loop [closed]

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
I was solving a problem on strings
Given a string S, write a program to title case every first letter of words in string.
Input:
The first line consists of an integer T i.e number of test cases. T testcases follow. The first and only line of each test case consists of a string S.
Output:
For each testcase, in a new line, print the required output.
Constraints:
1 <= T <= 100
1 <= |S| <= 1000
Example:
Input:
1
I love programming
Output:
I Love Programming
and for that I came up with this solution.
#include <stdio.h>
#include <string.h>
int main() {
//code
int t,flag;
scanf("%d", &t);
while(t--){
int n,i=0;
scanf("%d", &n);
char str[100];
scanf("%s", str);
while(i<n){
if (str[i]!= str[n-1-i]){
flag = 1;
printf("%d", flag);
break;
}
else{
flag = 0;
printf("%d", flag);
continue;
}
i++;
}
if(flag ==1 )
printf("No\n");
else
printf("Yes\n");
}
return 0;
}
This code works fine when continue is removed, but when the above code is run, it prints 0 infinitly.
Can you help me where I'm going wrong?
Thanks in advance.
The "continue" here means "immediately perform the next iteration in the current loop, in this case the while loop".
In your code the line "i++" is therefor not executed and variable i never changes, thus causing an infinite loop.

Program won't exit for first for loop and I have no idea why [closed]

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
The program is supposed to find amicable pairs. The first input tells you how many numbers will follow and the program is supposed to figure out which of those numbers are amicable pairs. I don't know if the program is actually able to do that though, since I can't even get past the first for loop, which is literally just putting the elements that need to be checked into an array.
int main(int argc, char *argv[]) {
int numberOfNumbers, num;
scanf("%d", &numberOfNumbers);
int numbers[numberOfNumbers];
for (int i = 0; i < numberOfNumbers; i++) {
scanf("%d", &num);
numbers[i] = num;
}
I expect the program to move onto the calculation part of the code (which I didn't include) and produce some output, whether it's right or not, but instead after I've entered the last number it just acts as if it wants another input. At that point I could enter every digit of Graham's number and it still won't exit.
The first step of debugging a problem is validating you have the problem you're considering fixing.
for (int i = 0; i < numberOfNumbers; i++) {
scanf("%d", &num);
numbers[i] = num;
}
Is the loop that you are thinking of fixing.
printf("entering loop\n");
for (int i = 0; i < numberOfNumbers; i++) {
printf("i is %d, numberOfNumbers is %d\n", i, numberOfNumbers);
scanf("%d", &num);
numbers[i] = num;
}
printf("loop finished\n");
is the code you would would need to completely validate that your guess about the loop is correct (or wrong).
I hope this helps, even if it is not a direct answer. Your code looks good, but could be wrong based on a lot of items (including the user input).

Thinking of a code to show if a number is prime or non prime? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 4 years ago.
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.
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.
Improve this question
Now im having problems with the new code in terms of compiling. I have two great answers but chux's answer is addressed to rectify my code . So by his/her directions my new code is:
#include <math.h>
#include <conio.h>
int main()
{
int n,i,r;
printf("Enter A Number to know its prime or non prime");
scanf("%d",&n);
for(i=2;i<=n-1;i++)
{
if(n%i==0)
{r==1;
break;
}
}
if(r==1)
printf("%d is a non-prime number",n);
else
printf("%d is a prime number",n);
return 0;
}
But on the output it show as 87 is a prime number. I don't know why. But can someone spot my mistake?
At few problems
Assignment vs. compare
if (r=1) assigns 1 to r, so if (r=1) is always true. Certainly a compare was needed, #Ry
// if (r=1)
if (r == 1)
No early break
OP's code: The value of r depends on the last iteration. Certainly once a factor is found, loop should exit.
for(i=2;i<=n-1;i++) {
if(n%i==0)
// r=1;
{ r = 1; break; }
else
r=0;
}
Incorrect functionality for n == 0,1
All values n < 2 incorrectly report as prime.
Inefficient
Code performs up to n loops. Only need to perform sqrt(n) loops. Tip: Do not use floating point math here for an integer problem.
// for(i=2;i<=n-1;i++)
for(i = 2; i <= n/i; i++)
Alternate
Only peek if you must code.
First off, " ... conio.h is a C header file used mostly by MS-DOS compilers to provide console input/output. It is not part of the C standard library or ISO C .." I was able to get the code to compile without that library file, so you may wish to consider removing it. As for as the code goes, well here is what I came up with:
#include <math.h>
#include <stdio.h>
int isPrime(int value) {
int i = 2;
for(; i < value; i++) {
if((value % i) == 0) {
return 0;
}
}
return value > 1;
}
int main(void){
int n=0,i=0, r=0;
char * s;
printf("\nPlase enter a number to learn if it is prime:");
scanf("%d",&n);
r = isPrime(n);
printf("\n%d is ", n);
s = (r==0)? " not a prime number" : "a prime number";
puts(s);
return 0;
}
After the user inputs a number, the code checks whether it is prime by calling the function isPrime(), a function that returns an int. isPrime is a simple function that attempts to factor a number.
See here for similar live code that I devised.

Palidrome program is not workig [closed]

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 8 years ago.
Improve this question
i am trying to make a palindrome program using simple logic but unfortunate I can't get the output being typed in Printf( "This is palindrome")
#include<stdio.h>
int main(void)
{
int a,b,c,d,e,f;
printf("enter the 5 digit of palindrome");
scanf("%d%d%d%d%d",&a,&b,&c,&d,&e,&f);
if(a==f && b==e && c==d)
{
printf("Its a Palindrome");
}
else if( a!=f || b!=e || c!=d){
printf("its a palindrome");
}
getchar();
return 0;
}
scanf("%d%d%d%d%d",&a,&b,&c,&d,&e,&f);
You should have another %d there. Otherwise f contains a garbage value and it'll be never equals a.
I recommend you to reconsider your code and write something more general, like reading a string instead of individual ints.
There are a couple of things wrong with your code:
you call scanf() with 5 %d, but pass 6 parameters (you're missing a %d)
your condition in the else branch is superfluous; you already know that at least one of the pairs (a,f), (b,e), (c,d) doesn't contain two equal values
in your else branch, you have printf("its a palindrome"), whereas it clearly is not a palindrome
#include <stdio.h>
int main(void){
int a,b,c,d,e;
printf("enter the 5 digit of palindrome :");
scanf("%1d%1d%1d%1d%1d",&a,&b,&c,&d,&e);
if(a==e && b==d)
printf("it's a palindrome");
else
printf("it's not a palindrome");
getchar();
return 0;
}
else if( a!=f || b!=e || c!=d){
printf("its a palindrome");
}
If the first and the last digit do not equal then it is not a palindrome.

Error in calculation of the number of digits in a number [closed]

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
Write a program using if statement which calculates how many digits a number contains:
My code :
#include <stdio.h>
int main (void)
{
int n;
printf ("Enter number :");
scanf ("%d",&n);
if ( n<=9)
printf ("Textnumber has one digit:");
if ( n <=99)
printf ("Textnumber has two digits:");
if (n<=999)
printf ("Textnumber has three digits:");
if (n <=9999)
printf ("Textnumber has four digits:");
return 0;
}
The problem is that when I run this, and I put for example : 223
I have the result in my screen :
Textnumber has three digitsTextnumberhasfourdigits...
Where am I wrong?
This is because your compiler is punishing you for not indenting your code properly.
Just kidding, in fact, your logic is flawed (and/or your expectation doesn't match the working of the code): you need else ifs because if your number is greater than a certain limit, it will be greater than smaller limits too, so all of them will be printed - erroneously.
Just try this code, as H2CO3 has given the right explanation...
#include <stdio.h>
int main (void)
{
int n;
printf ("Enter number :");
scanf ("%d",&n);
if ( n<=9)
printf ("Textnumber has one digit:");
else if ( n <=99)
printf ("Textnumber has two digits:");
else if (n<=999)
printf ("Textnumber has three digits:");
else if (n <=9999)
printf ("Textnumber has four digits:");
return 0;
}
Try a neater way.
int digits=0;
while(num)
{
num/=10;
digits++;
}

Resources