Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

While Entering Numbers it becomes infinite loop, tell me where i did error.

+5 votes
asked Jul 29, 2024 by Uzumaaki Coder (170 points)
// Divisiblity Test for input numbers

// Prem 25.07.2024

#include <stdio.h>

int a[2],b[3],c;

int d[2]={0},i;

int main()

{ printf ("Write the range where you wnat to test divisiblity :\n");

scanf("%d",&a[0]);

scanf("%d",&a[1]);

printf ("\nBy which three numbers you want to test divisiblity ;\n");

//input

    scanf ("%d",&b[0]);

scanf ("%d",&b[1]);

    scanf ("%d",&b[2]);

for (c=a[0]; c<=a[1]; a[0]++)

{

if (a[0]%b[0]==0|| a[0]%b[1]==0||a[0]%b[2]==0)

{

d[0]++;

}

else

{

d[1]++;

}

}

printf("Total numbers that ranges from 1 to 100 and \nthat are divisible by 2,3 or 5 is %d",d[1]);

printf("\nTotal numbers that ranges from 1 to 100 and \n that are not divisible by 2,3 or 5 is %d",d[2]);

return 0;

}

2 Answers

0 votes
answered Aug 15, 2024 by Peter Minarik (101,340 points)
edited Aug 19, 2024 by Peter Minarik

You increment not your loop variable, but the initial value of your loop, hence the infinite loop. Find the fix below in yellow.

#include <stdio.h>

int a[2], b[3], c;
int d[2] = { 0 }, i;

int main()
{
    printf("Write the range where you wnat to test divisiblity :\n");
    scanf("%d", &a[0]);
    scanf("%d", &a[1]);
    printf("\nBy which three numbers you want to test divisiblity :\n");

    //input
    scanf("%d", &b[0]);
    scanf("%d", &b[1]);
    scanf("%d", &b[2]);

    for (c = a[0]; c <= a[1]; c++)
    {
        if (c % b[0] == 0 || c % b[1] == 0|| c % b[2] == 0)
        {
            d[0]++;
        }
        else
        {
            d[1]++;
        }
    }

    printf("Total numbers that ranges from 1 to 100 and \nthat are divisible by 2,3 or 5 is %d",d[1]);
    printf("\nTotal numbers that ranges from 1 to 100 and \n that are not divisible by 2,3 or 5 is %d",d[2]);

    return 0;
}
0 votes
answered Aug 15, 2024 by Pingili Chandana (140 points)

your code went to infinite loop because the variable C on which your for loop is based on to iterate is not incremented properly

There are some mistakes in your code to be corrected inorder to get desired output

#include <stdio.h>

int a[2],b[3],c;

int d[2]={0},i;

int main()

{ printf ("Write the range where you wnat to test divisiblity :\n");

scanf("%d",&a[0]);

scanf("%d",&a[1]);

printf ("\nBy which three numbers you want to test divisiblity ;\n");

//input

    scanf ("%d",&b[0]);

scanf ("%d",&b[1]);

    scanf ("%d",&b[2]);

for (c=a[0]; c<=a[1]; c++)

{

if (c%b[0]==0|| c%b[1]==0||c%b[2]==0)

{

d[0]++;

}

else

{

d[1]++;

}

}

printf("Total numbers that ranges from %d to %d and \nthat are divisible by 2,3 or 5 is %d",a[0],a[1],d[1]);

printf("\nTotal numbers that ranges from %d to %d and \n that are not divisible by 2,3 or 5 is %d",a[0],a[1],d[2]);

return 0;

}

Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and receive answers from other members of the community.
...