89 / 92
Oct 2021

This solution no make sense, the solution don't does what it need do.

The code from Noix do:

input
output
input
output
... until you input 42

The problem ask you do

input
input
....until you input 42

output
output
...when 42 stop output.

someone for the love of GOD can explain me what you want we do in this problem ?

it is correct and efficient to do it the way like noix did. (though other approaches can get accepted, too)

  • read a value from input stream
  • print that value to output stream
23 days later

include

using namespace std;

int main(){
int arr[10],i, m = 42;
for (i = 0; i < 10;i++){
cin >> arr[i];

	if (arr[i] == m){
		break;
	}
	cout << arr[i] << " ";
}



return 0;
}
2 months later

Hi, this is the code I put for resolution for this problem, but every time SPOJ says Time Limit Exceeded:

#include
int main(int i)
{
while(scanf("%d",&i)>0 && printf("%d\n",i) && i != 42 );
return 0;
}

Could you please help me on this ?

3 months later

This was my first question on spoj and i am highly demotivated towards competitve programming.
this was my code

#include <iostream>
using namespace std;
int main() {
int a[10],i;
 for(i=0;i<10;i++)
 { cin>>a[i];
 }
for(i=0;i<10;i++)
 {
  if(a[i]!=42)
   cout<<a[i]<<endl;
   else
   break;
 }
	return 0;
}

It is running fine on codeblocks but is giving errors here on spoj.
Please someone help me.

1 month later

I wonder why it tell me time limit exceed??

#include <stdio.h>
int main()
{
int i=0,number[10];
do
{
scanf("%d",&number[i]);
i++;
}
while(number[i-1]!=42);

for(int j=0;j<i-1;j++)
{
    printf("%d\n",number[j]);
}

return 0;

}

5 months later

I tried making as short as possible for fun and I had this:

for (short x = 0; x != 42; scanf("%hd", &x), (x != 42)&& printf("%hd\n", x));
return 0;

But initially I had this:

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <limits.h>

#define ERROR_RET_NULL(x) fprintf(stderr, "ERROR: %s returned NULL in file '%s' at line: %i.", x, __FILE__, (__LINE__ - 2))


int 
main(void) 
{
        while (true)
        {
        	short temp = 0;

                char userInputString[5];
                char *endPtr;
                
                if (fgets(userInputString, 5, stdin) == NULL)
                {
                        ERROR_RET_NULL("fgets");
                        exit(EXIT_FAILURE);
                }
                else
                {
                        char *checkPtr = strchr(userInputString, '\n');
                        if (checkPtr) *checkPtr = '\0';
                        
                        long numberToCastToShort = strtol(userInputString, &endPtr, 10);
                        if (numberToCastToShort > SHRT_MAX || numberToCastToShort < SHRT_MIN)
                        {
                                /* Given the size restriction on the string this is basically deadcode */
                                fprintf(stderr, "ERROR: number too big");
                                exit(EXIT_FAILURE);
                        }
                        else if (numberToCastToShort == 42) break;
                        else temp = (short) numberToCastToShort;
                        
                }
                printf("%d\n", temp);
        }
        
        return 0;
}
2 months later

I am not getting the output required… It ask for the number but then page closes after entering the number.

5 months later

Can anyone tell me why am i getting an error for this code(life universe and everything)

#include <stdio.h>
#include <stdlib.h>

int main()
{
int i,n,t=3;
for(i=0;i<t;i++)
{
scanf("%d",&n);
if(n==42)
break;
else
continue;
}
return 0;
}

1 year later

#include<stdio.h>
int main(void) {
int numb;
do{scanf("%d",&numb);
if(numb==42)break;
printf("%d\n",numb);}while( numb>0);

return 0;

}
I WROTE THIS AND THEY TOLD ME I’M WRONG, CORRECT ME PLEASE

Your code is very bad formatted or bad pasted or both.

#include <stdio.h>

int main (void) {
	int numb;
	do {
          scanf ("%d", &numb);
	      if (numb == 42) break;
	      printf ("%d\n", numb);
    } while (numb != 42); // or simple: while (1);

	return 0;
}

PS

Please don’t shout, QUIETER PLEASE!!!

1 month later

My first try on SPOJ.
I can’t use argc+argv on SPOJ, can I?

#include <stdio.h>

int main(int argc, char** argv)
{
    int i = 0;
    for(; i < argc; ++i){
        if(argv[i][0] == '4' && argv[i][1] == '2')
            return 0;
        else
            printf("%s\n",argv[i]);
    }
    return 0;
}

No, You should read from standard input.
Example:
scanf("%i\n", &number);

1 year later

Yes it does give wrong answer because it displays input as well as output
like this :
1
1
4
4
5
5

which is incorrect. Correct me if I am wrong

2 months later

#include
using namespace std;

int main() {

int x;
while(1)
{
	cin>>x;
	cout<<x<<endl;
	if (x==42)
	break;
}
return 0;

}

  • _this code is accepted but what abot number with more than 2 digits .? They will also be taken as input .
2 years later

#include <studio.h>

int main(){
int x;
printf(“Enter the value of x : “);
scanf(”%d”,&x);
for(int i = 1; i < 42; i++){
printf("%d\n",x);
}
return 0;
}

// the solution in c programming language in proper way for the given program line by line…

#include
Using namespace std;

int main(){
int x;
cout << "enter the value of x : " << endl;
cin >> x;

For(int i = 0; i < 42; i++){
Cout << "the value of x is : " << x << endl;
}
return 0;
}

// for c++ this is the solution line by line … It’s looping

You are only reading the value of x once.
You are printing stuff that isn’t asked for. Only print x if it isn’t 42.
You are not checking the value of x to see if it is 42, when you should stop processing.