1 / 6
Sep 2019

why my this code showing time limit exceeded while compiling ??? I run same code on codeblocks and its working perfectly

#include <stdio.h>
int sum(int n)
{
int i=0,total=0;
for( i=1;i<n;i++)
{
if(n%i==0)
{
total=i+total;
}

}
        return total;

}
int main()
{
int t,n,z;
scanf("%d",&t);
while(t!=0)
{
scanf("%d",&n);
z = sum(n);
printf(" %d",z);
}
}

  • created

    Sep '19
  • last reply

    Sep '19
  • 5

    replies

  • 822

    views

  • 3

    users

  • 1

    link

Could it be the infinite loop? Where do you decrement t?

It leads to any infinite loop as you didn’t decrement t.
Instead of t!=0 keep t-- in while loop.It’ll work for sure