TrueBee
TrueBee

Reputation: 13

my program overlooks 'while' loop

I need to make a program that calculates 3^2+6^2+9^2+ ... +(3×N)^2, for a user given integer N.The program prompts the user to enter an integer that's less than 20. The program should stops after 3 wrong chances.This is my code:

#include <stdio.h>
int main(){
        int n,x,i ;
        float p;
        printf("Hey give me a positive integer number smaller than 20 \n ");
        scanf("%d" ,&n);
        x=3;
        p=0;
        while ((n<=0) && (n>=20)){
                printf("wrong input %d chances left \n" ,x);
                x--;
                if (x==0) return 0;
                scanf("%d" , &n);
        }
        for ( i=0; i<=n; i++){
                p= (3*i)* (3*i) + p ;
        }  
        printf("Yeah.. thats the result bro %f \n" , p);
        return 0;
}

I can't figure out why it won't enter the while loop. Please help.

Upvotes: 1

Views: 49

Answers (1)

ameyCU
ameyCU

Reputation: 16607

while ((n<=0) && (n>=20)){

See condition in this loop , do you know any number which is less that or equal to 0 and greater than 20 ? .

This loop will only work for such number because you used && operator , which will be true only when both conditions are fulfilled (therefore your loop doesn't iterate).

Use || operator for your loop-

while ((n<=0) || (n>=20)){

Upvotes: 5

Related Questions