0% found this document useful (0 votes)
45 views28 pages

Workshop03 TranNhatMinh HE186799

The document contains 10 programs that demonstrate various C programming concepts like checking for prime numbers, validating dates, calculating factorials and Fibonacci sequences, finding the greatest common divisor and least common multiple of two numbers, and determining the minimum and maximum digits in an integer. The programs cover basic input/output operations, conditional statements, loops, functions, and mathematical operations in C.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views28 pages

Workshop03 TranNhatMinh HE186799

The document contains 10 programs that demonstrate various C programming concepts like checking for prime numbers, validating dates, calculating factorials and Fibonacci sequences, finding the greatest common divisor and least common multiple of two numbers, and determining the minimum and maximum digits in an integer. The programs cover basic input/output operations, conditional statements, loops, functions, and mathematical operations in C.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 28

Program 1:

#include<stdio.h>
#include<math.h>
int prime(int n);
int main()
{
int n;
int i;
do
{ printf("nhap n: ");
scanf("%d", &n);
}
while(n<2);
for (i=2; i<=n; i++){
if(prime(i) == 1){
printf("%5d", i);
}
}
getchar();
return 0;
}
int prime(int n){
int m = sqrt(n);
int i;
if (n<2) return 0;
for(i=2; i<=m; i++){
if(n%i == 0) return 0;}
return 1;
}

Program 2:
#include<stdio.h>
int validDate(int d, int m, int y);
int main(){
int d, m, y;
printf("nhap ngay: ");
scanf("%d", &d);
printf("nhap thang: ");
scanf("%d", &m);
printf("nhap nam: ");
scanf("%d", &y);
if(validDate(d,m,y) == 1){
printf("ngay %d/%d/%d la ngay hop le\n", d,m,y);
}else{
printf("ngay %d/%d/%d la ngay khong hop le\n", d,m,y);
}
}
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;
else if (m==2) {
if ( y%400 ==0 || y%4==0 && y%100 !=0) maxd=29;
else maxd = 28;

}
return d <= maxd;
}

Program 3 :
#include<stdio.h>
#include<math.h>
int getRelPos(double x, double y, double r);
int main()
{
double x, y, r, result;
printf("enter x: ");
scanf("%lf", &x);
printf("enter y: ");
scanf("%lf", &y);
do {
printf("enter r: ");
scanf("%lf", &r);}
while (r<0);
result = getRelPos(x,y,r);
if(result==1){
printf("the point is in the circle!");
}else if (result==0){
printf("the point is on the circle!");
}else{
printf("the point is out the circle!");
return 0;
}
}
int getRelPos(double x, double y, double r){
double d2=x*x + y*y;
double r2= r*r;
if (d2<r2) return 1 ;
else if (d2==r2) return 0 ;
return -1 ;
}
Program 4 :
#include<stdio.h>
#include<math.h>
double factorial(int n);
int main()
{
int n;
do{
printf("enter n: ");
scanf("%d", &n);
}
while(n<0);
printf("factorial is: %d", factorial(n));
return 0;
}
double factorial(int n){
double p=1;
int i;
for (i=2; i<=n; i++) p *= i;
return p;
}

Program 5:
#include<stdio.h>
double fibo(int n);
int main()
{
int n;
do{
printf("enter n: ");
scanf("%d", &n);
}
while(n<1);
printf("fibo number is: %lf", fibo(n));
return 0;
}
double fibo(int n){
int t1=1, t2=1, f=1, i;
for (i= 3; i<=n; i++) {
f= t1 + t2;
t1= t2;
t2=f;
}
return f;
}
Program 6:
#include<stdio.h>
int isFibonaci(int n);
int main()
{
int n;
do{
printf("nhap n: ");
scanf("%d", &n);
}
while(n<1);
if(isFibonaci(n)==1){
printf("it is a fibonaci element");
}else{
printf("it is not a fibonaci element");
}
return 0;
}
int isFibonaci(int n){
int t1=1, t2=1, f=1;
if (n==1) return 1;
while(f<n)
{
f= t1 + t2;
t1=t2;
t2=f;
}
return n==f;
if(n==f){
printf("n is Fibo element");
}
}
Program 7:
#include<stdio.h>
int sumDigits(int n);
int main()
{
int n;
int S;
do{
printf("\nnhap n: ");
scanf("%d", &n);
if(n>=0){
S = sumDigits(n);
printf("sum = %d", S);
}
}while(n>=0);
return 0;
}
int sumDigits(int n){
int sum=0;
do{
int remainder = n%10;
n=n/10;
sum += remainder;
}
while(n>0);
return sum;
}
Program 8:
#include<stdio.h>
double makeDouble(int ipart, int fraction);
int main()
{
int ipart;
int fraction;
double value;
printf("enter ipart: ");
scanf("%d", &ipart);
do
{
printf("enter fraction: ");
scanf("%d", &fraction);
}
while(fraction<0);
value = makeDouble(ipart,fraction);
printf("the value is: %lf", value);
return 0;
}
double makeDouble(int ipart, int fraction){
double d_f=fraction;
while(d_f>=1) d_f=d_f/10;
if(ipart<0) return ipart - d_f;
return ipart + d_f;
}
Program 9:
#include<stdio.h>
int gcd(int a, int b);
int lcm(int a, int b);

int main()
{
int a,b;
int d;
int m;
do{
printf("enter a,b: ");
scanf("%d %d", &a, &b);
}
while(a<=0 || b<=0);
d=gcd(a,b);
m=lcm(a,b);
printf("\ngcd of ab is: %d", d);
printf("\nlcm of ab is: %d", m);
return 0;
}
int gcd(int a, int b){
while(a!=b)
if(a>b){
a-=b;}
else{
b-=a;}
return a;
}
int lcm(int a, int b){
return a*b/gcd(a,b);
}

Program 10:
#include<stdio.h>
void printMinMaxDigits(int n);
int main()
{
int n;
do{
printf("enter n: ");
scanf("%d", &n);
printMinMaxDigits(n);
}
while(n<0);
return 0;
}
void printMinMaxDigits(int n){
int digit;
int min, max;
digit = n%10;
n=n/10;
min=max=digit;
while(n>0)
{
digit=n%10;
n=n/10;
if(min>digit) min=digit;
if(max<digit) max=digit;
}
printf("\nmin = %d", min);
printf("\nmax = %d", max);
}

ouput= 6
output= 3

output= 13.100000
int
n=7,m=8;
int* p1= &n,
*p2=&m;
*p1 +=12-
m+ (*p2);
*p2 = m + n-
2*(*p1);
printf(“%d”,
m+n);
What is the
output? output: 8
int
n=7,m=8;
int* p1= &n,
*p2=&m;
*p1 +=12-
m+ (*p2);
*p2 = m + n-
2*(*p1);
printf(“%d”,
m+n);
What is the
output? output:8

output: 260 và 256


L= 5

a= 6, b= 7

c= 2
Program 1:
#include <stdio.h>
#include <math.h>
int primes (int);
void minMaxDigit (int n);
int main() {
int luaChon;
do{
printf("----------MENU------------\n1.Process primes\n2.Print min/max
digit in an interger\n3.Quit\n");
printf("please choose = ");
scanf ("%d",&luaChon);
switch (luaChon){
case 1: {
int n;
printf("import n = ");
scanf("%d",&n);
if(primes(n) == 0) printf("n doesn't primes");
else printf("n is primes\n");
break;
}
case 2: {
int a;
printf("import a = ");
scanf ("%d",&a);
minMaxDigit(a);
break;
}
case 3: break;
default:
break;
}
}while(luaChon<0 || luaChon>3);
return 0;
}
int primes (int n){
int i=2;
if (n==1 || n== 0) return 0;
for (; i<=sqrt(n); i++){
if(n%i==0) return 0;
}
return 1;
}
void minMaxDigit (int n){
int min = 9 , max = 0,k;
while(n>0){
k=n%10;
n/=10;
if (max<k) max = k;
if (min>k) min = k;
}
printf("max = %d\n",max);
printf("min =%d\n",min);

Program 2 :
#include<stdio.h>
#include<math.h>
double fibo ( int n) {
int t1=1, t2=1, f=1, i ;
for ( i= 3; i<=n; i++) {
f= t1 + t2;
t1= t2;
t2=f;
}
return 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;
else if(m==2) {
if ( y%400==0 || y%4==0 && y%100!=0 ) maxd=29;
else maxd=28;
}
return d<=maxd;
}
int main(){
int n , choice ;
int d, m , y ;
do {
printf(" 1- Fibonacci sequence\n");
printf(" 2-Check a date\n");
printf(" 3- Quit\n");
printf(" Select an operation: ");
scanf("%d", &choice);
switch(choice){
case 1 :
do
{
printf(" Enter a positive integer (or 0 to stop): ");
scanf("%d",&n);}
while (n<1);
printf("The value at the nth position in Fibonacci sequence is :
%.0lf",fibo(n));
return 0;
break;
case 2 :
printf("Enter the day: "); scanf("%d", &d);
printf("Enter the month: "); scanf("%d", &m);
printf("Enter the year: "); scanf("%d", &y);
if (validDate(d, m, y)==1) {
printf ("The date of %d/%d/%d is a valid date!\n", d, m ,y);
} else
{
printf("The date of %d/%d/%d is not a valid date!\n", d, m ,y);
}
}
break;
}
while ( choice >0 && choice <3);
return 0;
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy