Retake CPE100 Computer Programming: Exam M1
1. Find total prime.
// ID: 66070503459 Anongnart Boonkleang
// 1) Write C program to find total number of prime numbers in a given range
(Inclusive).
#include<stdio.h>
int main() {
int num1,num2,i,j,count=0,isPrime;
scanf("%d %d", &num1,&num2); // scanf the range of the total number
//To find the total prime number from the range between num1 and num2
for(i=num1 ; i<=num2 ; i++){
isPrime=1;
for(j=2 ; j<=i-1 ; j++){
if(i%j == 0){
isPrime=0;
break;
}
}
//If it is the prime number, count and collect it, to find the total prime
number from the given range
if(i>1 && isPrime){
count++;
}
}
//printf the total number of prime number from the given range.
printf("%d", count);
return 0;
• num1 and num2 are the input integers that will be the range to find the total prime number
• for loop -> for(i=num ; i <= num2 ; i++) is used to set the range to find the prime number
• isPrime is the thing that use to check if it is prime number, it will collect the quantity in the count
variable.
• For loop -> for(j=2 ; j <= i-1 ; j++) is used to check i that it is divisible by 2 and i-1
• If i is not the prime number (isPrime = 0;) then the inner loop will stop working which is break.
• Then it will run like this until num2 and display the total number of the prime number.
66070503459: Anongnart Boonkleang
2. What day?
// ID: 66070503459 Anongnart Boonkleang
// 2) Write a C program to determine the day of the week for a given date in the year
2009.
#include <stdio.h>
#include <string.h>
char* getDay(int day, int month, int year) {
static char* days[7] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday",
"Friday", "Saturday"};
if (day < 1 || day > 31 || day==29 || month < 1 || month > 12)
return "ERROR";
int t[12] = {0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4};
year -= month < 3;
return days[(year + year / 4 - year / 100 + year / 400 + t[month - 1] + day) % 7];
}
int main() {
int d, m;
char day[20];
//printf("Enter day (dd): ");
scanf("%d %d", &d, &m);
strcpy(day, getDay(d, m, 2009));
printf("%s\n", day);
return 0;
}
For this question
• I have used “getDay” function to get the day of the week from input date and month in year 2009
• When you input the date, it will go check what day of this date or is it Error in “getDay” function
• Then strcpy will copy the result from “getDay” function
• Lastly, it will display the day of the date that we have input by using printf.
66070503459: Anongnart Boonkleang
3. 3 Decimal to Base-4 Conversion (Using for Loop)
// ID: 66070503459 Anongnart Boonkleang
// 3) Using the code below as a starting point, modify it to convert the decimal
number to a base-4 number using a For loop instead of a While loop.
#include <stdio.h>
int main()
{
int num, base = 0, rem = 0, place = 1;
//scanf the decimal number
scanf("%d", &num);
//Use to find the Base-4 number
for (num ; num>0 ; num/=4, place *= 10){
rem = num % 4;
base = base + (rem * place);
}
//printf the Base-4 number
printf("%d\n", base);
return 0;
}
• When we input the decimal number in this
• For loop will check that if that num is more than 0, the remainder of num will divide by 4 and
continue calculated in base = base + (rem * place)
• Then “num” will update that num/=4 and place *= 10
• Lastly, it will print the base-4 number.
66070503459: Anongnart Boonkleang