Odd And Even Counter - c

I want to change this codes to get 20 Numbers from input and count how many are Odd and how many are Even? please can anyone help?!
#include <conio.h>
#include <stdio.h>
int main()
{
int n;
int odd=0;
int even=0;
printf("\nEnter any number \n");
scanf("%d",&n);
if(n%2!=0)
{
printf("%d is an odd number",n);
odd++;
}
else
{
printf("%d is an even number",n);
even++;
}
printf("\n odd%d / even%d",odd,even);
}

Here is a function that solves your problem:
Tip: You need a loop to take your input 20 times.
void countForJHikaam(){
int n,i;
int odd=0;
int even=0;
for(i=0;i<20;i++){
scanf("%d\n",&n);
if(n%2==0){
even++;
}else{odd++;}
}
printf("Odds: %d, Evens: %d",odd,even);
}
It won't really help you in learning. Now go learn what a function is.

#include <conio.h>
#include <stdio.h>
int main()
{
int n;
int odd=0;
int even=0;
printf("\nEnter any number \n");
while(scanf("%d",&n))
(n%2) ? (++odd) : (++even);
printf("\n odd%d / even%d",odd,even);
}

#include<stdio.h>
main()
{
int odd=0,even=0,no,count=20;
printf("Enter the 20 numbers...\n");
here:
scanf("%d",&no);
(no%2==0)? odd++ : even++ ;`
count--;
if(count>0)
goto here;
printf("No of odd numbers... :%d\n",odd);
printf("No of even numbers... :%d\n",even);
}

Related

Function gives me a wrong answer

I'm new to C. I've been tasked to run a program that calculates the percentage of students that passed an exam,based on N grade inputs.I don't really understand how functions work in though.This is what I came up with
#include <stdio.h>
#define MAX_N 300
main()
{
int N,grade,i;
float success(N)
{
float sum=0.0;
for (i=0;i<N;i++) {
if (grade>=5) {
sum+=1;
}
float success=sum/N;
return(success);
}
}
printf("How many students? ");
scanf("%d",&N);
printf("Enter grades(0-10) of %d students ",N);
for (i=0;i<N;i++){
scanf("%d",&grade);
}
printf("%f percent of students have passed the exam ",success(N);
return(0);
}
It looks like it should work, however I always get the wrong result.It is stuck on displaying 0.2 or 0.25 for any input I give.Can somebody help?
The problem is that in grade only the last entered data is being stored. Make grade as an array so that all data can be stored.
I guess you are taking multiple value for grade and not taking array for it.
grade should be an array and in loop scanf("%d",&grade[i]); should be implement.
grade should be an array of N integers so that each and every value is stored. You also forgot to multiply success by 100 to get the percentage.
I think I fixed the code:
#include <stdio.h>
#define MAX_N 300
float success(int grade[],int N)
{int i;
float sum=0.0;
for (i=0;i<N;i++) {
if (grade[i]>=5) {
sum+=1;
}
}
float success=sum/N;
return(success*100);
}
int main(){
int N, i;
printf("How many students? ");
scanf("%d",&N);
int grade[N];
printf("Enter grades(0-10) of %d students ",N);
for(i=0;i<N;i++){
scanf("%d", &grade[i]);
}
printf("%f percent of students have passed the exam ", success(grade, N));
return(0);
}
I think you should examine the code I wrote. A little bad code. But it can help.
#include <stdio.h>
int students_success(int *);
int main() {
int n;
printf("How many students?\n");
scanf("%d", &n);
printf("Enter grades(0-10) of %d students\n", n);
int grade;
int pass_std = 0;
for(int i = 0; i < n; ++i) {
scanf("%d", &grade);
pass_std = students_success(&grade);
}
printf("%.2f percent of students have passed exam.\n", (double)pass_std / n);
}
int students_success(int *grade) {
static int pass_std = 0;
if(4 < *grade) {
++pass_std;
}
return pass_std;
}

How can i fix this code in order to check if a number is palindrome, using pointers?

I am quite new to C. I don't really understand how to get the pointers right. I know it can be done without pointers , but I have to use them
#include <stdio.h>
#include <stdlib.h>
void palindrome(int *n)
{
int ok=0,*p,*m;
m=n;
while(*n!=0)
{
*p=*p*10+*n%10;
*n=*n/10;
}
if (*m==*p) ok=1;
if (*m!=*p) ok=0;
if (ok==1)
printf("Number is palindrome.");
if (ok==0)
printf("Number is not palindrome");
}
int main()
{
int n;
printf("Give value to n: ");
scanf("%d",&n);
palindrome(n);
}
The expected resut would be , for example, number 212 is palindrome, number 312 is not palindrome
Here is the code. I think you should try and understand how pointers work before actually using them. I didn't want to provide you the code, but since you are new, i am providing a working code. Try understand how it is working instead of just copy pasting this code to where ever you have to put.
#include <stdio.h>
#include <stdlib.h>
void palindrome(int *n)
{
int ok=0;
int *p = (int *)malloc(sizeof(int));
int *m = (int *)malloc(sizeof(int));
*p=0;
*m=*n;
while(*n!=0)
{
*p=*p*10+*n%10;
*n=*n/10;
}
if (*m==*p) ok=1;
if (*m!=*p) ok=0;
if (ok==1)
printf("Number is palindrome.");
if (ok==0)
printf("Number is not palindrome");
}
int main()
{
int n;
printf("Give value to n: ");
scanf("%d",&n);
palindrome(&n);
return 0;
}
#arpit-agrawal
You forgot to add free, at the end of
void palindrome(int *n)
{
...
free(p);
free(m);
}

Rolling dice with over 7 digits

My aim is to make a program that will throw two dice until the top faces of the two dice total to a number.
However, in my code, i roll a dice with over 7 digits. Help me please, Thank you!
My code:
#include <ctime>
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
int intRandom(int a=1,int b=6)
{
int n;
n=a+rand()%b;
return n;
}
int main()
{
int i,n,x,y; /*n:total, i:count*/
printf("Dice Thrower\n================\n");
printf("Total sought: ");
do
{
scanf("%d",&n);
} while (n<2 && n>12);
{
i=1;
srand(time(NULL));
do
{
x=intRandom(2,6);
y=intRandom(6,2);
printf("Result of throw %d: %d + %d\n",i,x,y);
if ((x+y)==n) printf("\nYou got your total in %d throws!\n",i);
fflush(stdin);
i++;
} while ((x+y)!=n);
}
getch();
}
Change the random number generation to
x=intRandom(1,6);
y=intRandom(1,6);

Output error 0 in a basic c program in finding repititions

I am trying to write a C program to print the number of repetitions for a given number in an array. what ever value I give as the input , the function returns the value only 0 to me.. what might be the error..the code is attached
#include<Stdio.h>
#include<conio.h>
int occurence(int n, int arr[], int x);
void main()
{
int x,arr[100],n,i;
clrscr();
printf("\nEnter the number of elements: ");
scanf("%d",&n);
printf("\nEnter the elements:");
for(i=0;i<n;i++)
{
scanf("%d",arr[i]);
}
printf("\nEnter the element to be searched for repetitions: ");
scanf("%d",&x);
printf("%d",occurence(n,arr,x));
getch();
}
int occurence(int n,int arr[100], int x)
{
int i,rep=0;
for(i=0;i<n;i++)
{
if(x == arr[i])
{
rep++;
}
}
return rep;wenter code here
}
Problem is on the line 13 it is supposed to be scanf("%d",&arr[i]);

do While loop. It doesn't work

I'm really new to C and I can't get this while loop to work. It just exits the loop for no apparent reason.
Here is the code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(void)
{
int min;
int max;
int random;
int guess;
char user='x';
printf("Please insert min number:");
scanf("%d",&min);
printf("Please insert max number:");
scanf("%d",&max);
random = (rand()%(max-min))+ min;
do
{
printf("please insert your guess:");
scanf("%d",&guess);
if(guess==random)
{
printf("YOU WON");
user=='y';
}
else if(guess!=random)
{
printf("UNLUCKY,would you like to play again:x for yes");
user=='x';
}
}while(user=='x');
}
The following is a comparison, not an assignment:
user=='y';
You need to change it to:
user='y';
(Note the single =.)
The same goes for the other assignment.

Resources