runtime error

how to remove runtime error from the following code:

#include<stdio.h>
#include<stdlib.h>
void main()
{
	int a[2000],i;
	float n,m,p;
	a[0]=0;
	a[1]=1;
	for(i=2;i<2000;i++)
		a[i]=a[i-1]+a[i-2];
	printf("enter the no. to be rounded");
	scanf("%f",&n);
	for(i=0;i<2000;i++)
	{
		if(n==a[i])
		{
			printf("rounded number is %f",n);
			exit(0);
		}
	}
	i=0;
	while(a[i]-n<0)
		i++;
	m=a[i]-n;
	p=n-a[i-1];
	if(m<p)
		printf("rounded number is %d",a[i-1]);
	else
		printf("rounded number is %d",a[i]);
}

use #include for including purpose …
also use int main instead of void main cause c++ standard require main() to return int.

2 Likes

this is the corrected code…but dont know y it is giving WA…maybe the TCs are faulty…!!

#include<stdio.h>
#include<stdlib.h>
int main()
{
    int a[27],i,n,m,p;
    a[0]=0;
    a[1]=1;
    for(i=2;i<27;i++)
    {
        a[i]=a[i-1]+a[i-2];
      //  printf("%d ",a[i]);
    }
    //printf("\n");
    //printf("enter the no. to be rounded");
    scanf("%d",&n);
    i=0;
    while(a[i]-n<0)
        i++;
    m=a[i]-n;
    p=n-a[i-1];
    if(m>p)
        printf("%d",a[i-1]);
    else
        printf("%d",a[i]);
    return 0;
}

pls dont print that enter the number and such things…please follow the i/o format else u will get a WA verdict…hope this helps…:slight_smile:

EDIT

also as far as possible try solving the problems in the easy medium hard and challenge sections…as their TCs are checked by the tester and the admin i.e. all the officials…peer section is the section for problems of external contests…here the test cases may not be reliable…!!!