C Program to Check Prime Use Loop And Recursive

How to write a C Program to Check Prime Use Loop And Recursive in C Programming Language ?


Solution For C Program :
/*Check Prime Use Loop And Recursive*/

#include<stdio.h>
#include<conio.h>

#ifndef title
#define title "\n 1. Chon 1 de Nhap So Nguyen Duong. \n 2. Chon 2 de Hien Thi Cac So Nguyen To Dung Vong Lap. \n 3. Chon 3 de Hien Thi Cac So Nguyen To Dung De Quy. \n 4. Chon 4 de Ket thuc. \n Moi chon: "
#endif

int KiemTraVongLap(int you_number){
int count = 0;
for (int i = 2; i < you_number; i++)
{
if (you_number % i == 0)
{
count = count + 1;
}
}
if (count == 0){
return 1;
}
else {
return 0;
}
}
int InVongLap(int you_number){
for (int  i = 2; i < you_number; i++)
{
if (KiemTraVongLap(i))
{
printf("\t\n %d \n", i);
}
}
}
int KiemTraDeQuy(int you_number, int i){
if (i == 1){
return 1;
}
else {
if (you_number % i == 0){
return 0;
}
else {
return KiemTraDeQuy(you_number, i - 1);
}
}
}
int InDeQuy(int you_number){
for (int i = 2; i < you_number; i++){
if (KiemTraDeQuy(i, i / 2)){
printf("\t\n %d \n ", i);
}
}
}
int main(){
int you_choose, you_number;
do
{
printf(title);
scanf_s("%d", &you_choose);
switch (you_choose)
{
case 1:
system("cls");
printf("\n Nhap So Bat Ky: ");
scanf_s("%d", &you_number);
system("cls");
continue;
case 2:
system("cls");
InVongLap(you_number);
continue;
case 3:
system("cls");
InDeQuy(you_number);
continue;
case 4:
system("cls");
printf("\n\n\n\t\t\t\t Ket Thuc.");
break;
default:
system("cls");
printf("\n \t\t\t\t Da Chon Sai. Moi Chon Lai! \n");
continue;
}
} while (you_choose != 4);
_getch();
return 0;
}


Learn More :