Home > Back-end >  Questions about the if statement execution
Questions about the if statement execution

Time:09-16

? Four given two positive integers a, b, find out between these two Numbers (including) all meet the following conditions:

? 1. The four digits are prime Numbers;

? 2. The one thousand number and one hundred number of double-digit AB is a completely square number;

? 3. Ten digits with double-digit composed of bits digital CD is a completely square number

# include
Int main ()
{
Int a, b, c=0, I, j, p1, p2, n=0;
The scanf (" % d % d ", & amp; A, & amp; B);
For (I=a; i<=b; I++)
{
For (j=2; J{
If (I % j==0)
{
C=1;
break;
}
The else
C=c;
}
If (c==0)
{
P1=I/100;
The p2=I % 100;
If (p1==16 | | p1==25 | | p1==36 | | p1==49 | | p1==64 | | p1==81)
{
If (p2==1 | | p2==4 | | p2==9 | | p2==16 | | p2==25 | | p2==36 | | p2==49 | | p2==64 | | p2==81)
{
n=n+1;
Printf (" % d \ n ", I);
}
}
}

}
Printf (" % d ", n);
}
Why my code at run time is always not the if (c==0) the inside, who can teach, a little confused,

CodePudding user response:

 # include 
Int main ()
{
Int a, b, c=0, I, j, p1, p2, n=0;
The scanf (" % d % d ", & amp; A, & amp; B);
For (I=a; i<=b; I++)
{
For (j=2; J{
If (I % j==0)
{
C=1;
break;
}
The else
C=c;
}
If (c==0)
{
P1=I/100;
The p2=I % 100;
If (p1==16 | | p1==25 | | p1==36 | | p1==49 | | p1==64 | | p1==81)
{
If (p2==1 | | p2==4 | | p2==9 | | p2==16 | | p2==25 | | p2==36 | | p2==49 | | p2==64 | | p2==81)
{
n=n+1;
Printf (" % d \ n ", I);
}
}
}
C=0;//need to initialize

}
Printf (" % d ", n);
}

For your reference ~

Need to c the re-initialization

CodePudding user response:

/* redundant code ~ 
The else
C=c;
*/

This part is redundant code, suggest delete ~
Or here to is as follows:
 
The else
c=0;

CodePudding user response:

reference 2 building self-confidence boy reply:
/* redundant code ~ 
The else
C=c;
*/

This part is redundant code, suggest delete ~
Or here to is as follows:
 
The else
c=0;

Has been solved, thank you??

CodePudding user response:

 # include 
Int main ()
{
Int a, b, c=0, I, j, p1, p2, n=0;
The scanf (" % d % d ", & amp; A, & amp; B);
For (I=a; i<=b; I++) {
C=0;
For (j=2; JIf (I % j==0) {
C=1;
break;
/* else}
C=c; */
}
If (c==0) {
P1=I/100;
The p2=I % 100;
If (p1==16 | | p1==25 | | p1==36 | | p1==49 | | p1==64 | | p1==81) {
If (p2==1 | | p2==4 | | p2==9 | | p2==16 | | p2==25 | | p2==36 | | p2==49 | | p2==64 | | p2==81) {
N=n + 1;
Printf (" % d \ n ", I);
}
}
}

}
Printf (" n=% d \ n ", n);
return 0;
}


Please take a look at the program, modify the individual places, in fact this algorithm can be better,

CodePudding user response:

 # include 
#include

Int main ()
{
Int a, b, num, num1, num3, k, n=0;
Bool flag;
The scanf (" % d % d ", & amp; A, & amp; B);
For (num1=4; Num1 & lt;=9; Num1 + +) {
For (num3=1; Num3 & lt;=9; Num3 + +) {
Num=100 + num3 num1 num1 * * * num3;//produce meet the conditions of 4 digits
If (aflag=true;
For (k=2; True & amp; & KIf (num % k==0) {
flag=false;
}
}
If (flag) {//if it is a prime number, count and output
N++;
Printf (" % d \ n ", num);
}
}
}
}
Printf (" n=% d \ n ", n);
return 0;
}

To verify that this application is correct,

CodePudding user response:

See this code is a headache, you can use the indentation, the appearance of the code to a bit of a specification!
  • Related