0% found this document useful (0 votes)
13 views

Workshop 4

The document contains 3 programs written in C language. Program 1 displays a menu and allows the user to choose between processing prime numbers or printing the minimum and maximum digits of an integer. Program 2 is similar but allows the user to choose between calculating the Fibonacci sequence or validating a date. Program 3 prints all numbers between 1-100 that are divisible by 3 or 4. The second program prints all perfect numbers below a user-input value.

Uploaded by

vinh03072005
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Workshop 4

The document contains 3 programs written in C language. Program 1 displays a menu and allows the user to choose between processing prime numbers or printing the minimum and maximum digits of an integer. Program 2 is similar but allows the user to choose between calculating the Fibonacci sequence or validating a date. Program 3 prints all numbers between 1-100 that are divisible by 3 or 4. The second program prints all perfect numbers below a user-input value.

Uploaded by

vinh03072005
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

Exercise 1:

Sau phép tính đầu, *pn = 6 +12 – 21 =-3

Sau phép tính 2 *pm thành 9

Khi in m+n sẽ cho ra 6

*p1= 65 + 3 = 68

*p2= 70 – 5 = 65

c1 là *p1 = 68

c2 là *p2 = 65

c1 – c2 = 68-65=3
*p1 = 3.2 + 3 – 2*5.1=-4

*p2 = 5.1- 3*(-4)=17.1

X+y=*p1 +*p2 = 17.1-4 =13.1

Exercise 2:
Exercise 3:

-L will return 6

-a will return 6, b will return 7


-C will be 2

Part 2

Program 1

#include <stdio.h>

#include <math.h>

int getUserChoice()

{ int choice;

printf("\n============================================================");

printf("\n 1. Process primes. ");

printf("\n 2. Print min, max digit in an integer. ");

printf("\n 3. Quit. ");


printf("\n============================================================\
n");

printf("\nEnter Your Choice: ");

scanf("%d%*c", &choice);

return choice;

void primes()

{ int n, i, s = 1;

printf("\nEnter positive interger: ");

scanf("%d", &n);

int m = sqrt(n);

for ( i = 2; i <= m; i++)

if (n % i == 0)

s = 0;

if (s == 1 && n == 1)

printf("\nThe input number is not a prime!\n");

else if (s == 0 || n <= 0)

printf("\nThe input number is not a prime!\n");

else

printf("\nThe input number is a prime!\n");

void digits()

int n;

printf("\nEnter integer: ");

scanf("%d", &n);

int max, min;


max = n % 10;

min = n % 10;

while( n > 0)

int temp = n % 10;

n/=10;

if ( max < temp)

max = temp;

if ( min > temp)

min = temp;

printf("\nMinimum of digits: %d\n", min);

printf("Maximum of digits: %d\n", max);

int main()

{ int userChoice;

do

{ userChoice = getUserChoice();

switch(userChoice)

{ case 1: primes();

break;

case 2: digits();

break;

default: printf("\nGood Bye!");


}

while (userChoice>0 && userChoice<3);

return 0;

#include <stdio.h>

#include <math.h>

int getUserChoice()

{ int choice;

printf("\n======================================");
printf("\n1. Fibonacci sequence. ");

printf("\n2. Check a date. ");

printf("\n3. Quit. ");

printf("\n======================================\n");

printf("\nEnter Your Choice: ");

scanf("%d%*c", &choice);

return choice;

void fibonacci() {

int n, i;

printf("\nEnter a positive integral number: ");

do {

scanf("%d", &n);

while (n < 1);

printf("\nThe first %d Fibonacci numbers: ", n);

int t1 = 1, t2 = 1, f = 1;

for (i = 0; i < n; i++) {

f = t1 + t2;

t1 = t2;

t2 = f;

printf("%lld ", f);

int validDate(int d, int m, int y)

int maxd = 31;


if ((d < 1) || (d > 31) || (m < 1) || (m > 12))

return 0;

if ((m == 4) || (m == 6) || (m == 9) || (m == 11))

maxd = 30;

if ( m == 2) {

if ((y % 400 == 0) || ((y % 4 == 0) && (y % 100 != 0)))

maxd = 29;

else

maxd = 28;

return d <= maxd;

void date()

int d, m, y, s;

printf("\nEnter day, month, year: ");

scanf("%d%d%d", &d, &m, &y);

s = validDate(d,m,y);

if ( s == 1)

printf("\nValid Date!");

else

printf("\nInvalid Date!");

int main()

{ int userChoice;

do

{ userChoice = getUserChoice();
switch(userChoice)

{ case 1: fibonacci();

break;

case 2: date();

break;

default: printf("\nGood Bye!");

while (userChoice>0 && userChoice<3);

return 0;

Program 3

#include <stdio.h>

int main() {

printf("Cac so tu 1 den 100 chia het cho 3 hoac 4 la:\n");

int i;

for (i = 1; i <= 100; i++) {

if (i % 3 == 0 || i % 4 == 0) {

printf("%d ", i);

return 0;

}
#include<stdio.h>

int ispnum(int n) {

int s = 0;

int i =1;

for (i = 1; i <= n / 2; i++) {

if (n % i == 0)

s += i;

return s == n;

void printsmallerpnum(int n) {

int i =1;

for (i = 1; i < n; i++) {

if (ispnum(i)) {

printf("%d ", i);

int main() {

int n;

printf("Nhap n = ");

scanf("%d", &n);
printf("Cac so hoan hao nho hon %d la:\n", n);

printsmallerpnum(n);

getchar();

return 0;

You might also like