This is a code for input-output practice. I am getting the correct output for the first two input lines. But I am getting a zero for the third line input.
The given task is: To calculate the sum of some integers.
Input:
4 1 2 3 4
5 1 2 3 4 5
0
Output:
10
15
#include<stdio.h>
int main()
{
int i, first, next, total;
while(scanf("%d", &first) != EOF)
{
total = 0;
for(i = 1; i <= first; i++)
{
scanf("%d", &next);
total += next;
}
printf("%d\n", total);
if(first == 0)
{
printf(" ");
}
}
return 0;
}
If you do not want output when the first number on a line is zero, then you should test first == 0 before calculating and printing a total and break from the loop (break; if you want to stop the loop) or continue to the next iteration (continue;).
Related
I am trying to solve a problem where i need to input n numbers in a array and need to find is the value of the array is whether odd or even (means 2 2 2 2 will accepted but 1 2 1 3 will not gonna accepted cz it has both even and odd number) but when i am trying to print the value it is showing yesyesyes how can i do it??
#include <stdio.h>
#include <stdlib.h>
int main()
{
int t,n,a[1000],i;
scanf("%d",&t);
if(t>=1 && t<=100)
{
scanf("%d",&n);
if(n>=2 && n<=50)
{
for(i=1;i<=n;i++)
{
scanf("%d",&a[i]);
if(a[i]%2==0)
{
break;
}
if(a[i]%2!=0)
{
break;
}
else
printf("NO");
}
printf("YES");
}
}
return 0;
}
I presume the problem statement:
Task: To find if all the elements in a given list are all-odd or all-even.
There are T test cases, 1 <= T <= 100
In each test case N specifying the number of elements in a list(array), 2 <= N <= 50
Followed by N space separated integers.
For each test case print YES if all list-members are all-odd or all-even. Print NO otherwise.
Since the inputs are curated, we're assuming scanf() always succeeds.
We don't need to store numbers in an array, as we don't need them later.
Simplified code :
#include <stdio.h>
#include <stdlib.h>
int main () {
int T; // number of tests
scanf ("%d", &T);
while (T--) {
int N; // numbers in a given test
scanf ("%d", &N);
int even = 0;
int odd = 0;
for (int ni = 0; ni++ < N; ) {
int X;
scanf ("%d", &X);
(X % 2) ? ++odd : ++even;
//if (odd && even) break; // but, you need to clear the inputs before next test case
}
if (N == odd || N == even)
printf ("YES\n");
else
printf ("NO\n");
}
return 0;
}
For input file:
3
4
2 2 2 2
4
1 2 1 3
5
1 5 7 33 5
Output will be:
YES
NO
YES
It just creates random values
I tried using a separate value for the variable of the array and I also don't know why it starts counting at element 6.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i;
int array[3][5];
for (i = 0; i<5; i++);
{
printf("Input a whole number for row 1 element %d:\n", i+1);
scanf("%d", &array[0][i]);
}
printf("Row 1 elements:\n");
for(i = 0; i<5; i++)
{
printf("%d\n", array[0][i]);
}
return 0;
}
Ouput:
> Input a whole number for row 1 element 6: 4 Row 1 elements: 0 0 0 0
> 1897665488
>
> Process returned 0 (0x0) execution time : 1.969 s Press any key to
> continue.
It starts counting from 6 because the line for (i = 0; i < 5; i++);, is iterating (incrementing) i 5 times so, i becomes 5, then you print i + 1 to stdout.
So, basically your call to printf() and scanf() functions were never a part of any sort of loop.
NOTE: Adding a semi-colon ;, after any loop means that there is no body for the loop. Basically it's an empty loop. It can be useful for finding the length of a string, and so on.
Some tips:
Also instead to using bare return 0;, use return EXIT_SUCCESS;, which is defined in the header file stdlib.h.
use int main(void) { }, instead of int main() { }
always check whether scanf() input was successful or not
Correct Code
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int i;
int array[3][5];
for (i = 0; i < 5; i++)
{
printf("Input a whole number for row 1 element %d:\n", i + 1);
if (scanf("%d", &array[0][i]) != 1)
{
perror("bad input: only numbers are acceptable\n");
return EXIT_FAILURE;
}
}
printf("Row 1 elements:\n");
for (i = 0; i < 5; i++)
{
printf("%d\n", array[0][i]);
}
return EXIT_SUCCESS;
}
Output:
Input a whole number for row 1 element 1:
1
Input a whole number for row 1 element 2:
2
Input a whole number for row 1 element 3:
3
Input a whole number for row 1 element 4:
5
Input a whole number for row 1 element 5:
7
Row 1 elements:
1
2
3
5
7
I tried using the for loop for the test cases as I thought that would bring the desired output but it didn't
int main()
{
int num, temp, digit, sum = 0;
int test,i;
scanf("%d",&test);
for(i=1;i<=test;i++)
{
printf("\n");
scanf("%d", &num);
temp = num;
while (num != 0)
{
digit = num % 10;
sum = sum + digit;
num /= 10;
}
printf("%d",sum);
}
return 0;
}
Expected results- 2 123 456
Output- 6 15
Obtained Results- 2 123 456
Output- 6 21
The first output is correct but at the second print it is summing up the first result with the second line which I don't want.
You never clear sum after processing 123. You are calculating the correct sum (15) but it is being added to the sum from the previous step (6). To fix the problem clear sum inside the for loop.
for(i=1;i<=test;i++)
{
sum = 0;
printf("\n");
I want user to enter 8 numbers.
If they enter less than 8 numbers, the program will exit.
What if statement should I use?
Should I put in my loop sum += i then if sum not 8 then exit?
This is what I got so far but it doesn't work out:
int main() {
int i, numb;
int sum = 0;
// the loop to enter 8 numb
printf("enter 8 numbers");
if (i=0;i<8;i++) {
scanf("%d", &numb);
sum =+i;
if (sum < 8)
exit(1);
}
return (0);
}
You want the user to enter 8 numbers and since you have not mentioned anything about the sum, I'll assume it doesn't matter what it is. Remove the inner if condition altogether and replace the outer if with a loop.
Here is the code:
for (i = 0; i < 8; i++) {
scanf("%d", &numb);
//Do whatever you want to do with the number here
Because of the way the console works you can't tell where the EOF is. You can achieve what you want by checking the separators between the numbers. If you want the numbers to be no less than 8 on the same line, you can do it this way
int main() {
int i, numb;
int sum = 0;
char separator=' ';
// the loop to enter 8 numb
printf("enter 8 numbers");
for (i = 0; i<8; i++)
{
if (separator == '\n')//enter character encountered
break;
scanf("%d%c", &numb,&separator);
}
if (i < 8)
exit(1);
return (0);
}
Here is an algorithm which does exactly what you want:
1. Set a counter to 0
2. While not end of file (EOF) do
1. Read a number
2. Increase counter by 1
3. If sum counter equals 8, return 1, else return 0
and your code repaired (since you've finally posted what you tried to do):
int main()
{
int i, numb, counter = 0;
printf("enter 8 numbers");
while(scanf("%d", &numb) != EOF)
{
counter++;
}
if (counter < 8)
{
printf("not enough numbers\n");
exit(1);
}
return (0);
}
Live demo: http://ideone.com/iiOl1A
I need to generate the following output of odd numbers in pyramid pattern.
The output will be like
1
3 3
5 5 5
7 7 7 7
I have written the following code. What portion i should modify?
#include<stdio.h>
int main()
{
int num,r,c;
printf("Enter structure number : ");
scanf("%d", &num);
for(r=1; r<=num; r++)
{
if(r%2 != 0){
m=1;
for(c=1; c<=m; c++)
printf("%d",r);
printf("\n");
}
}
return 0;
}
Current Output:
Current output is like-
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
You could write:
for(r=1; r <= num; r+=2) //we only need odd numbers
{
times = r/2 + 1; //how many times to print odd number
for(c=1; c <= times; c++)
printf("%d",r); //print one character at a time
printf("\n");
}
You'll probably understand it better if you only iterate through odd numbers. I'm currently doing that and calculating how many times I need to print that number, then I'm just printing it as many times as times is.
you have 2 errors in this code .
m is not declared anywhere .
you are running a infinite loop
try this .
#include<stdio.h>
int main()
{
int num,r,c,m;
printf("Enter structure number : ");
scanf("%d", &num);
for(r=1; r<=num; r++)
{
if(r%2 != 0){
m=r;
for(c=1; c<=m; c++)
printf("%d",r);
printf("\n");
}
}
return 0;
}
In your code, instead of
m=1;
you should write
m= ( (r/2) + 1);
Oterwise, all the time, you'll be iterating in the for loop only once.
Some little modifications and it works:
#include<stdio.h>
int main()
{
int num,r,c,m=0;
printf("Enter structure number : \n");
scanf("%d", &num);
for(r=1; r<=num; r++)
{
if(r%2 != 0){
m++;
for(c=1; c<=m; c++)
printf("%d ",r);
printf("\n");
}
}
return 0;
}
m is undeclared
line feed at the end of the printf message
m incremented each odd iteration
space between printed unmbers
Here is a demo.
the inner for loop should look like this:
for(c=1; c <= r/2; c++)
printf("%d ",r);
just think about it for a second. you want to print a rounded r/2 of numbers in every line, right?
like:
3/2 -> 1.5 -rounded-> 1 -> prints: 3
5/2 -> 2.5 -rounded-> 2 -> prints: 5 5
and so on.
you can run the code here on ideone.com
#include<stdio.h>
int main()
{
int num,r,c;
printf("Enter structure number : ");
scanf("%d", &num);
for(r=1; r<=num; r++)
{
if(r%2 != 0){
int m=r;
for(c=1; c<=m; c++)
{
if(c%2 != 0){
printf("%d ",r);
}
}
printf("\n");
}
}
return 0;
}
and test
sh-4.3# main
Enter structure number : 9
1
3 3
5 5 5
7 7 7 7
9 9 9 9 9
In row 1, you should print 1.
In row 2, you should print 3.
In row 3, you should print 5.
In row 4, you should print 7.
............................
............................
In row n, you should print 2*n-1.
You can check this:
#include<stdio.h>
int main()
{
int num,r,c;
printf("Enter structure number : ");
scanf("%d", &num);
for(r=1; r<=num; r++)
{
for(c=1; c<=r; c++)
printf("%d",2*r-1);
printf("\n");
}
return 0;
}
Please take a look at this:
for(r=1; r<=num; r+=2) // increment by 2, work for r= 1,3,5,7...
{
for(c=1; c<=r; c+=2)// increment by 2
printf("%d",r);
printf("\n");
}
If you want alternate numbers like 1,3,5,7... just increment value by 2.