Professional Documents
Culture Documents
SE17D04 - Bạch Gia Bảo DE180644 - Workshop4
SE17D04 - Bạch Gia Bảo DE180644 - Workshop4
Workshop 04
BẠCH GIA BẢO – DE180644
Objectives:
(1) Managing data using pointers
(2) Developing programs using simple menus
1- Process primes
2- Print min, max digit in an integer;
3- Quit
Select an operation:
Analysis Nouns:
- positive integral number int n
- A number represents a choice of user int choice;
Functions:
int prime( int n) see above
void printMinMaxDigits( int n) see above
Suggested algorithm Begin
(logical order of Do /* Print out the menu and get user choice*/
verbs) { Print out “1- Process primes\n”;
Print out “2- Print min, max digit in an integer \n”;
Print out “3- Quit\n”;
Print out “Select an operation:”;
switch(choice)
{ case 1: do
{ Input n;
}
while(n<0);
If ( prime(n)==1) Print “ It is a prime\n”;
Else Print “ It is not a prime\n”;
break;
case 2: do
{ Input n;
}
while(n<0);
printMinMaxDigits( int n) ;
break;
}
}
while ( choice >0 & choice<3);
End
#include <stdio.h>
#include <math.h>
int m=sqrt(n);
int i;
if (n<2){
printf("The input number is not a prime\n");
return ;
}
for (i=2; i<=m; i++)
if (n%i==0){
printf("The input number is not a prime\n");
return ;
}
printf("The input number is a prime\n");
}
int digit;
int min=9,max=0;
while (n>0){
digit=n%10;
n=n/10;
if (digit<=min) min=digit; //tim chu so be nhat
if (digit>=max) max=digit; //tim chu so lon nhat
}
printf("The minimum digit in the input number: %d\n",min);
printf("The maximum digit in the input number: %d\n",max);
}
int main(){
int choice, n;
do{
printf("Menu\n");
printf("1- Process primes\n"); //Kiem tra so nguyen to
printf("2- Print min, max digit in an integer\n"); // In ra chu so be nhat va lon nhat cua
so nhap vao
printf("3- Quit\n"); //Thoat khoi chuong trinh
printf("\nPlease choose a calculation tool: ");
scanf("%d",&choice);
switch (choice){
case 1:
prime();
break;
case 2:
printMinMaxDigits();
break;
case 3:
printf("\nThank for using tool");
break;
default:
printf("\nInvalid choice. Please try again.\n");
break;
}
printf("\n=============================\n");
}
while (choice!=3);
return 0;
}
1-Fibonacci sequence
2-Check a date
3-Quit
Choose an operation:
1- When the option 1 is selected, the program will accept a positive integral number,
called as n, then the first n Fibonacci numbers will be printed out
2- When the option 2 is selected, the program will accept a date then the program will
tell that whether this data is valid or not.
3- If the option 3 is selected, the program quits
#include <stdio.h>
#include <math.h>
int main(){
int choice, n;
do{
printf("Menu\n");
printf("1- Fibonacci sequence\n"); //In ra n so fibonacci
printf("2- Check a date\n"); // Kiem tra ngay thang hop le
printf("3- Quit\n"); //Thoat khoi chuong trinh
printf("\nPlease choose a calculation tool: ");
scanf("%d",&choice);
switch (choice){
case 1:
fibo();
break;
case 2:
validDate();
break;
case 3:
printf("\nThank for using tool");
break;
default:
printf("\nInvalid choice. Please try again.\n");
break;
}
printf("\n=============================\n");
}
while (choice!=3);
return 0;
}
Ví dụ : n = 10 => S = 1.855812
√
S= 1+
2! 3 ! 4 !
5
n!
+ 5 + 5 +…+ 5
2 4 6 2n
3. Viết chương trình nhập 1 số nguyên từ bàn phím và in ra các số hoàn hảo nhỏ hơn
n. (số hoàn hảo là số có tổng ước số = chính nó. Ví dụ số 6 là số hoàn hảo: 6 =
1+2+3
More Programs
You can pick 2 or 3 functions in the workshop 2, associate them to a new program.\