I want to accept only odd numbers - c

I am trying to write a code to accept only odd number. If the number is even, it will ask for another number until an odd number is entered.
If the number is odd, it will continue to run the remaining of the program using that odd value.
I am not sure how to write the syntax for the for loop in this program.
Here is part of the odd number validator code:
#include <stdio.h>
int main()
{
int num;
printf("Enter odd value for n: ");
scanf("%d", &num);
for(num; num % 2 == 0; num)
{
printf("%d is even. TRY AGAIN!: ");
scanf("%d",&num);
}
}

`#include <stdio.h>
int main()
{
int num;
odd: //used label
printf("Enter odd value for n: ");
scanf("%d", &num);
if(num%2==0)
{
printf("%d is even. TRY AGAIN!: \n",num);
goto odd;
}
printf("%d is Odd ",num);
}

Related

Finding the Factorial of range between 1 to N

trying to write a program that would find the factorial of range of numbers starting from 1 to N,N being the final Number to find the factorial for, i have written a non-recursive program.it only works for integers 1 and 2 in the loop, i'm not sure how to fix it because the logic seems fine,also i'm somewhat still a beginner,so i know i'm probably missing something that's obvious,but in any case here's the code :
#include<stdio.h>
int main() {
int firstnumber;
int finalnumber;
printf("this is a program to calculate the factorial of numbers between 1 to N\n");
printf("please enter the final number : ");
scanf("%d",&finalnumber);
int i;
int factorial=1;
for (firstnumber=1;firstnumber<=finalnumber;firstnumber++) {
printf("the factorial of %d is : ",firstnumber);
for (i=1;i<=firstnumber;i++) {
factorial=factorial*i;
}
printf("%d \n ",factorial);
}
return 0;
}
You need to initialize factorial before each calculation.
int i;
// delete this
//int factorial=1;
for (firstnumber=1;firstnumber<=finalnumber;firstnumber++) {
printf("the factorial of %d is : ",firstnumber);
// move the declaration here
int factorial=1;
for (i=1;i<=firstnumber;i++) {
factorial=factorial*i;
}
printf("%d \n ",factorial);
}
Your two loops are redundant: you can calculate factorial for number from factorial of number - 1
This is the programme with one loop only. Complexity O(N).
Pay attention that you will get overflow rapidly by using int.
#include<stdio.h>
int main() {
int number;
int finalnumber;
printf("this is a program to calculate the factorial of numbers between 1 to N\n");
printf("please enter the final number : ");
scanf("%d",&finalnumber);
int factorial = 1;
for (number=1; number<=finalnumber; number++) {
printf("the factorial of %d is : ",number);
factorial *= number;
printf("%d \n",factorial);
}
return 0;
}

i want to take number from user then print if it prime or not but i can't find what is error where result always not prime so what is problem?

i want to take number from user then print if it prime or not but i can't find what is error where result always not prime so what is problem??
#include <stdio.h>
int main(void)
{
int number;
char flag = 0;
printf("Please enter the number:");
scanf("%d",&number);
for (int i = 1; i <= number; i++) {
if (number %i == 0) {
flag = 1;
break;
}
}
if (number==1)
printf("%d neither prime nor not prime", number);
if (flag==1)
printf("%d is not prime",number);
else
printf("%d is prime",number);
return 0;
}
Take a look at the loop condition here:
for(int i=1;i<=number;i++)
With <=, i goes up to number. So the last check will be if(number%number==0), which is always true: your program says that 5 is not a prime number because 5 divided by 5 has remainder 0. The same applies to dividing the number by 1 (which also results in no remainder), so this check should start at 2. This line should be:
for(int i=2;i<number;i++)
Typically i only has to go up to sqrt(number) because no two numbers bigger than the root of number multiplied will result in number.
Also, if the number entered is 1, you get two of the three possible outputs instead of just the first one. To fix this, put an else before the if (flag == 1).
edit to work successfully
#include <stdio.h>
int main(void)
{
int number ;
char flag=0;
printf("please enter the number:");
scanf("%d",&number);
for(int i=2;i<number;i++)
{
if(number%i==0)
{
flag=1;
break;
}
}
if (number==1)
{ printf("%d neither prime nor not prime", number);}
else if (flag==1)
{ printf("%d is not prime",number);}
else
{ printf("%d is prime",number);}
return 0;
}
Just check reminder either it is 0 or 1, if you divide any number in the end it will give you either 0 or 1 in reminder.If reminder is 1 so number is prime or 0 number is not prime.
#include <stdio.h>
int main(void)
{
int number ;
char flag=0;
printf("please enter the number:");
scanf("%d",&number);
if((number>2)&&(number%2==0))
{
flag=1;
}
if (number==1)
{ printf("%d neither prime nor not prime", number);}
if (flag==1)
{ printf("%d is not prime",number);}
else
{ printf("%d is prime",number);}
return 0;
}

Programs counting even and odd numbers

I'm self-studying C and I'm trying to make 2 programs for exercise:
the first one takes a number and check if it is even or odd;
This is what I came up with for the first one:
#include <stdio.h>
int main(){
int n;
printf("Enter a number that you want to check: ");
scanf("%d",&n);
if((n%2)==0)
printf("%d is even.",n);
else
printf("%d is odd.",n);
return 0;
}
the second one should take n numbers as input and count the number of even numbers, odd numbers, and zeros among the numbers that were entered. The output should be the number of even numbers, odd numbers, and zeros.
I would like to ask how to implement the loop in this case: how can I set an EOF value if every integer is acceptable (and so I cannot, say, put 0 to end)? Can you show me how to efficiently build this short code?
#include <stdio.h>
int main(void) {
int n, nEven=0, nOdd=0, nZero=0;
for (;;) {
printf("\nEnter a number that you want to check: ");
//Pressing any non-numeric character will break;
if (scanf("%d", &n) != 1) break;
if (n == 0) {
nZero++;
}
else {
if (n % 2) {
nEven++;
}
else {
nOdd++;
}
}
}
printf("There were %d even, %d odd, and %d zero values.", nEven, nOdd, nZero);
return 0;
}
Check the return value of scanf()
1, 1 field was filled (n).
0, 0 fields filled, likely somehtlig like "abc" was entered for a number.
EOF, End-of-file encountered (or rarely IO error).
#include <stdio.h>
int main(void) {
int n;
for (;;) {
printf("Enter a number that you want to check: ");
if (scanf("%d",&n) != 1) break;
if((n%2)==0)
printf("%d is even.",n);
else
printf("%d is odd.",n);
}
return 0;
}
Or read the count of numbers to subsequently read:
int main(void) {
int n;
printf("Enter the count of numbers that you want to check: ");
if (scanf("%d",&n) != 1) Handle_Error();
while (n > 0) {
n--;
printf("Enter a number that you want to check: ");
int i;
if (scanf("%d",&i) != 1) break;
if((i%2)==0) {
if (i == 0) printf("%d is zero.\n",i);
else printf("%d is even and not 0.\n",i);
}
else
printf("%d is odd.\n",i);
}
return 0;
}
hey look at this
#include<stdio.h>
#include<conio.h>
void main()
{
int nodd,neven,num,digit ;
clrscr();
printf("Count number of odd and even digits in a given integer number ");
scanf("%d",&num);
nodd = neven =0; /* count of odd and even digits */
while (num> 0)
{
digit = num % 10; /* separate LS digit from number */
if (digit % 2 == 1)
nodd++;
else neven++;
num /= 10; /* remove LS digit from num */
}
printf("Odd digits : %d Even digits: %d\n", nodd, neven);
getch();
}
You can do something like this:
#include <stdio.h>
int main(){
int n,evenN=0,oddN=0,zeros=0;
char key;
do{
clrscr();
printf("Enter a number that you want to check: ");
scanf("%d",&n);
if(n==0){
printf("%d is zero.",n);
zeros++;
}
else if((n%2)==0){
printf("%d is even.",n);
evenN++;
}
else{
printf("%d is odd.",n);
oddN++;
}
puts("Press ENTER to enter another number. ESC to exit");
do{
key = getch();
}while(key!=13 || key!=27) //13 is the ascii code fore enter key, and 27 is for escape key
}while(key!=27)
clrscr();
printf("Total even numbers: %d",evenN);
printf("Total odd numbers: %d",oddN);
printf("Total odd numbers: %d",zeros);
return 0;
}
This program ask for a number, evaluate the number and then ask to continue for another number or exit.

Writing a program to continue to ints until a prime

Write a program to continue to read in positive integers
till a prime is encountered. Assume at least one number will be entered.
Eg.
Please enter #: 8
Please enter #: 9
Please enter #: 10
Please enter #: 11
Thanks you entered a prime, bye.
The main problem that I am having is the continuation until the prime occurs. The code I have to test if the number is prime is here:
// PRIME NUMBER TEST
#include <stdio.h>
int main () {
int number, n, is_prime = 0;
printf("Enter number: ");
scanf("%d", &number);
for (n=2; n<=number/2; n++) {
if (number%n==0)
is_prime = 1;
while(is_prime == 1) {
printf("Enter #: ");
scanf("%d", &number);
}
}
if (is_prime == 0)
printf("%d is a prime number.\n", number);
system("PAUSE");
return 0;
}
I know that it would be a for loop to keep it going and just initializing a counter until the prime occurs, but for whatever reason I just can't get it right. Thanks for the help.
You need to walk though your code and check your invariant for each loop, that is what I mean by my comment of that your for-while loops are inside out....
// PRIME NUMBER TEST
#include <stdio.h>
int main () {
bool is_prime = false;
while(is_prime == false) {
int number, n;
printf("Enter #: ");
scanf("%d", &number); // scanf is not recommended, but use it here anyway
is_prime = true; // we assume so for now, also test for numbers < 2
for (n=2; n<=number/2; n++) // this can be optimized, but leave as is anyway
if (number%n==0)
is_prime = false; // sorry it was not prime
}
printf("%d is a prime number.\n", number);
return 0;
}

Finding biggest and smallest numbers using user input

Well it is a problem about finding the biggest and smallest number in a group of numbers, but we do not know how many numbers the user wants-
So far this is what i have done:
#include <stdio.h>
#include <conio.h>
int main()
{
int num;
int i;
int maxi=0;
int minim=0;
int cont = 0;
printf ("\nQuantity of numbers?: ");
scanf ("%d", &num);
while (num>0)
{
printf ("\nEnter number:");
scanf ("%d", &i);
if (num>i)
minim=i++;
else
if (i>num)
max=i++;
cont++;
}
printf ("\nBiggest number is es: %d", maxi);
printf ("\nSmallest number is: %d", minim);
getch();
return 0;
}
I did my program to ask how many numbers the user will want to put and i made the program to read them, BUT when it reads the biggest or/and smallest numbers it will sometimes changes biggest with small and it will not read negative numbers.
How do i do to make my program better?
You're comparing against the wrong values.
do
{
printf("Enter a number.\n");
scanf("%i", &input);
if min > input
min = input
if max < input
max = input
} while (input > 0);
#include <stdio.h>
#include <conio.h>
#include <limits.h>
int main(){
int num;
int i;
int maxi=0;
int minim=INT_MAX;
int cont = 0;
printf ("\nQuantity of numbers?: ");
scanf("%d", &num);
if(num > 0){
while (num>0){
printf ("\nEnter number:");
if(scanf("%d", &i) == 1 && !(i<0)){
if(minim > i)
minim = i;
if (maxi < i)
maxi = i;
++cont;
--num;
} else {
//fprintf(stderr, "redo input!\n")
;
}
scanf("%*[^\n]%*c");
}
printf ("\nBiggest number is : %d", maxi);
printf ("\nSmallest number is : %d\n", minim);
}
getch();
return 0;
}
You should initialize mini to the largest possible int, i.e. INT_MAX and maxi to the smallest possible int, i.e., INT_MIN. This way, even if the first number is negative, it will be considered for maxi, and if the first number is positive it will still be considered for mini. The constants INT_MAX and INT_MIN are included in <climits> or <limits.h>.
Also, you are comparing the current entered number with num, which is the counter of numbers entered by user, not one of the values he wants to compare. A better modified code would be :
#include<limits.h>
#include<stdio.h>
int main()
{
int num;
int maxi=INT_MIN; //initialize max value
int mini=INT_MAX; //initialize min value
int temp;
scanf("%d", &num); //take in number of numbers
while(num--) //loop "num" times, num decrements once each iteration of loop
{
scanf("%d", &temp); //Take in new number
if(temp>maxi) //see if it is new maximum
maxi=temp; //set to new maximum
if(temp<mini) //see if new minimum
mini=temp; //set to new minimum
}
printf("\nMaxi is:\t%d\nMini is:\t%d\n", maxi, mini); //print answer
return 0;
}

Resources