The question
A maths teacher asks her students to give 3 examples for positive odd numbers. When the student specifies a correct answer, his/her score is incremented by 1. When the student specifies a positive even number, his/her score is decremented by 0.5. When the student specifies a negative number, he/she will not be given any more chances to correct his or her mistake and his/her score will be decremented by 1. So a student's turn comes to an end when he/she has correctly specified 3 positive odd numbers or when the student has specified a negative number.
Few students didn't know the difference between odd numbers and even numbers and they made many mistakes and so it was difficult for the teacher to maintain the scores. The teacher asks for your help.
Can you please help her by writing a program to calculate the score?
Sample Input 1:
1
3
5
Sample Output 1:
3.0
Sample Input 2:
1
2
5
6
7
Sample Output 2:
2.0
Sample Input 3:
2
-4
Sample Output 3:
-1.5
Sample Input 4:
3
3
3
Sample Output 4:
3.0
Problem:
here i am not getting how to input multiple variables through single scanf
here is my code
#include<stdio.h>
int main()
{
int a,i;
float b=0;
for(i=1;i<=3;i++)
{
scanf("%d",&a);
do
{
if(a%2==0)
{
b=b-0.5;
}
if(a<0)
{
b--;
}
else if(a%2!=0)
{
b++;
}
printf("%.1f",b);
}
while(a%2!=0);
break;
}
return 0;
}
Look, the following for loop will just iterate only 3 times:
for(i=1;i<=3;i++)
whereas you input can be more than 3 times (see Sample Input 2)!
There are some syntactical issues besides some logical error in your code.
Instead of float b=0; use float b = 0.0;
Note that, if the input is negative even number then the following code will reduce b by 0.5
if(a%2==0)
{
b=b-0.5;
}
Where the immediate if condition will again reduce b by 1.
if(a<0)
{
b--;
}
that will make your result more reduced by 0.5, which is not correct.
As per your problem definition, the Sample Input is variable so that you have to implement your loop as well. Try something like this:
int input, correctResultCount = 0;
float result = 0.0;
while (true) {
scanf("%d", &input);
if (input < 0) {
result--;
break;
}
if (input % 2 == 0) {
result = result - 0.5;
}
if (input % 2 != 0) {
result++;
correctResultCount++;
}
if (correctResultCount == 3)
break;
}
printf("%.1f", result);
Run Live.
**NOTE**
THIS LINE IS IMPORTANT
"A Student's turn comes to an end when he/she has correctly specified 3 positive odd numbers or when the student has specified a negative number"
i.e HE CAN GIVE ANY NUMBER OF EVEN NUMBERS (2,4,6,8,10...) BUT THIS WILL HALT ONLY WHEN A TOTAL OF 3 ODD NUMBERS ARE GIVEN OR A SINGLE NEGATIVE NUMBER.
#include <iostream>
using namespace std;
int main()
{
int num;
float marks=0.0 ;
for(int i=0;i<3;)
{
cin>>num;
if(num<0)
{
marks=marks-1;
break;
}
else if(num%2==0)
{
marks=marks-0.5;
}
else{
marks=marks+1;
i++;
}
}
cout<<"Final MArks Are :"<<marks<<endl;
}
SAMPLE INPUT & OUTPUT 1:
1 3 5 ->3.0
SAMPLE INPUT & OUTPUT 2:
1 2 5 6 7 -> 2.0
Related
Something is wrong. I'm trying to make a cade which can count number count of any natural number. Like number count of 2 is 1, 30 is 2, 456 is 3. My code is running for 1 digit numbers but not for two digit numbers.
#include<stdio.h>
void main(void)
{
int num,count,check;
float div;
printf("Enter a natural number\n");
scanf("%d", &num);
while (num<=0)
{
printf("Error\n");
printf("Enter a number\n");
scanf("%d", &num);
}
while(num>1)
{
count=1;
check=10;
div=num/check;
if(div<=1)
{
printf("Number count is\n%d", count);
break;
}
check = check*10;
count = count+1;
}
}
The problem with your solution is that after check and count are modified at the end of the loop, they are re-declared to 1 and 10 respectively at the beginning of the loop at every passage.
You need to move the declaration just before the while loop.
Also div doesn't need to be a float given that the decimal part of this number is irrelevant.
You could also use less variables by replacing check by 10 and
using num directly instead of temporarily storing results in div.
I think this might be a simpler solution
#include <stdio.h>
int main(){
int num = 0, digits = 0;
while (num <= 0)
{
printf("Enter a natural number\n");
scanf("%d", &num);
num == 0 ? printf("Error\n") : 0;
}
for( ; num > 0; digits++)
num /= 10;
printf("number of digits: %d\n", digits);
}
As num is continuously divided by 10, the decimal of the result gets truncated since num is an int while digits steadily increases.
It is time to learn to use a debugger. Using it would have immediately shown the major problem in your code: you reset the value of count and check inside the loop. So if you enter a number greater or equal to 10, you enter an infinite loop because you will consistently divide that number by 10 and find that the result is >= 1!
There is another less important problem: you use if(div<=1) when it should be if(div<1). Because 10/10 is 1 and has 2 digits...
After those fixes you should have:
...
check = 10;
count = 1;
while (num > 1)
{
div = num / check;
if (div < 1)
{
printf("Number count is\n%d", count);
break;
}
check = check * 10;
count = count + 1;
}
return 0; // main shall return an int value to its environment...
}
Which correctly gives the number of decimal digit on positive integers. But as you were said in comments, you should always test the return value of scanf (what is the user inadvertently types a t instead of 5 for example?).
That being said, this answer intends to show you what the problems were, but Keyne's solution is better...
The output should print the statement "Case #(N): I will become a good boy." a certain number of times depending on user input. It should only print prime numbers of N. This is the latest attempt.
#include<stdio.h>
int main(){
int primeNum;
int primeCount;
int primeCheck;
int caseCount=1;
int caseCheck;
scanf("%d", &caseCheck);//gets number of cases
do {
scanf("%d", &primeNum);
primeCheck = 0;
if (primeNum<=1)
{
caseCount++;
}
for (primeCount=2 ; primeCount<=primeNum/2 ; primeCount++)//checks if number is prime
{
if ((primeNum%primeCount) == 0)//checks if number is not prime
{
primeCheck=1;
}
}
if (primeCheck==0)
printf("Case #%d: I will become a good boy.\n", caseCount);//prints if number is prime
} while (caseCount=caseCheck);//while case counter does not exceed number of cases
return(0);}`
The result of this piece of code when the output is 2 [enter] 4 [enter] 2 is "Case #2: I will become a good boy." Why does it not print more than one time and start from number 2?
Just change while (caseCount=caseCheck); to while (caseCount==caseCheck);
The problem with your code is you're not checking the condition. You're just reassigning a value to a variable.
I am solving problem in a online judge and I faced a problem. I think my code is correct but unfortunately the judge says wrong answer. Where did I made mistake?
Habib has learned so much about programming in the last few days! Today he faces a new challenge, handling input with test cases! For this, he must solve a set of inputs and for each input h must generate an output. And what is better than to practice this other than calculating factorials?! A factorial of an integer N is calculated by multiplying all the integers from 1 to N. For example, 4! (4 factorial) is calculated as- 1x2x3x4=24. In this problem, Habib is required to solve a set of inputs for a defined number of test cases. For example, if testcase = 3, then he must take 3 set of inputs and generate 3 sets of desired outputs, one output for one input. Help him solve the problem.
Input
The input starts with an integer Test (0 < Test < 100) which denoted number of inputs or testcases to be solved. For each testcase, input an integer n (0 <= n <= 10).
Output
For each testcase generate output in a single line in the format: Case x: y, where x is the testcase number and y is the answer for calculating n!.
#include <stdio.h>
int main()
{
int i,Test,n=0,x,j,y,s=1;
scanf("%d",&Test);
for(i=1;i<=Test;i++)
{
scanf("%d",&n);
for(j=1;j<=n;j++)
{
s=s*j;
}
printf("Case %d: %d\n",i,s);
}
return 0;
}
You forgot to reset s for each test cases.
Try adding s=1; after scanf("%d",&n);.
Input The input starts with an integer Test (0 < Test < 100) which denoted number of inputs or testcases to be solved. For each testcase, input an integer n (0 <= n <= 10).
Your code does not reflect this. You should definitely check if the inputs are valid or not, a way could be:
if(Test < 100 && Test > 0){
//do something
}else{
printf("Invalid input!");
}
Furthermore you should consider staying with either capital variable names or only lowercase, example: int test,n or int Test, N. Mixing both makes bigger projects a guessing game when you have to edit it after a few days.
Now to your Task:
Basically your task is to find n! and do this X-times.
As stated by #MikeCat you never reset your s, which is the starting point for your factorial.
#include <stdio.h>
int main(){
int i,test,n=0,x,j,y,s=1;
scanf("%d",&Test);
if(!(test <100 && test > 0)){
printf("Invalid input!");
}
for(i=1;i<=test;i++)
{
scanf("%d",&n);
s = 1;
for(j=1;j<=n;j++)
{
s=s*j;
}
printf("Case %d: %d\n",i,s);
}
return 0;
}
This should do the trick.
Change your code to
#include <stdio.h>
int main()
{
int i,Test,n=0,x,j,y;
scanf("%d",&Test);
for(i=1;i<=Test;i++)
{
int s=1;//observe this
scanf("%d",&n);
for(j=1;j<=n;j++)
{
s=s*j;
}
printf("Case %d: %d\n",i,s);
}
return 0;
}
This question already has answers here:
Algorithm to find all the exact divisors of a given integer
(7 answers)
Closed 6 years ago.
The problem statement is:
Watson gives an integer NN to Sherlock and asks him: What is the number of divisors of NN that are divisible by 2?.
Input Format
First line contains TT, the number of testcases. This is followed by
TT lines each containing an integer NN.
Output Format
For each testcase, print the required answer in one line.
Constraints
1≤T≤1001≤T≤100
1≤N≤109
Sample Input :
2\n
9\n
8
Sample Output:
0\n
3
I have typed '\n' which means the next integer will be in a new line.
Hacker Rank would not accept my code due to timeout. Please help me optimise this C code.
My C code is :
int main() {
int length,number,i,count =0;
scanf("%d",&length);
while(length--){
scanf("%d",&number);
for(i=2;i<=number/2;i++){
if(number % i == 0 && i % 2 == 0){
count = count + 1;
}
}
if(number % 2 == 0){
count = count + 1;
}
printf("%d\n",count);
count =0;
}
return 0;
}
Below is a algo to solve this problem:
If the number is even:
Run the loop from i=2 till square root of the number ie: sqrt(number) and increase the count in two cases below if i divides the number(number%i==0) :
if i is even(i%2==0)
for q where q=number/i if i not equal to q(i!=q)
and q is even(q%2==0) because if i divides the number it means q will also divide(number/q=i) it.
At last increase the count by 1 as number also divides itself.
If the number is odd: count will be 0 as no i(even) would divide an odd number.
int main()
{
int length,number,i,count;
scanf("%d",&length);
while(length--)
{
scanf("%d",&number);
count =0;i=2;
if(number%2==0)
{
while(i*i<=number)
{
if(number%i==0)
{
if(i%2==0)
count++;
int q=number / i;
if (i !=q && q%2==0)
count++;
}
i++;
}
count++;
}
printf("%d\n",count);
}
return 0;
}
I wanted to print the divisors of given range of numbers. It works alright. But when I try to modify it to put **** at the end of the prime number's divisors it acts like bizarre.
#include <stdio.h>
int main()
{
int a,start,rounds,b,c,k=0;
printf("Please enter a number to start :");
scanf("%d",&start);
printf("Please enter how many numbers you want to print from that number :");
fflush(stdin);
scanf("%d",&rounds);
for(a=start;a<=start+rounds;a++)
{
printf("\n\nThe divisors of number :%d are \n",a);
for(b=1;b<=a;b++)
{
c=a%b;
if(!c)
{
k++;
printf("%d\n",b);
}
}
//printf("%d",k);
if((k==2)||(k==1))
printf("***\n");
}
getchar();
return 0;
}
PS:- The trick I used to find a prime number is counting how many printf statement has been executed before loop ends. Is there any wrong with it? When I remove // from printf statement it prints like below.
start=========>k
1 =========>1
2 =========>3
3 =========>5
4 =========>8
5 =========>10
Why is that?
if((k==2)&&(k==1))
There is no way in todays computers that k can be 2 and 1 at the same time. Maybe you meant to say if k is 2 OR k is 1 ?
You need to reset k back to 0 in your outer loop
printf("\n\nThe divisors of number :%d are \n",a);
k = 0;
(And, as others have pointed out, you need an || operator rather than && for the final test)