C language 1 day a small program 2, C language small program
Program functions:
Enter an integer to print all the prime numbers within this integer.
Program example:
# Include <stdio. h>
# Include <stdlib. h>
# Include <math. h>
Bool IsPrime (int x)
{
Bool bResult = false;
Int I, k;
K = (int) sqrt (x );
For (I = 2; I <= k; I ++)
{
If (x % I = 0)
{
Break;
}
}
If (I> k)
{
BResult = true;
}
Else
{
BResult = false;
}
Return bResult;
}
Int main ()
{
Int a = 0;
Int B = 0;
Int I = 0;
Printf ("enter an integer :");
Scanf ("% d", & );
For (I = 3; I <= a; I ++)
{
If (IsPrime (I ))
{
Printf ("% d \ n", I );
}
}
System ("pause ");
Return 0;
}
Program parsing:
1. The IsPrime () function is used to determine whether an integer is a prime number. If yes, true is returned. Otherwise, false is returned. Because the C language library function sqrt () is called in this function, # include must be included in the header file.
2. The scanf_s () function is used to obtain user input data and save the data to a local variable.
Summary:
1. the math. h header file declares some common mathematical operations, such as multiplication and open operations. If you want to use functions in the C standard library, you need to include the header file of the function.
2. We can use the scanf () function to obtain data from the terminal.