0% found this document useful (0 votes)
38 views23 pages

C

The document contains code snippets for C programs that demonstrate various concepts like checking if a number is prime, calculating sums of digits, printing arrays, using switch statements, data type conversions, and more. Functions like calculating averages and finding maximum values are also presented.

Uploaded by

22070438
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)
38 views23 pages

C

The document contains code snippets for C programs that demonstrate various concepts like checking if a number is prime, calculating sums of digits, printing arrays, using switch statements, data type conversions, and more. Functions like calculating averages and finding maximum values are also presented.

Uploaded by

22070438
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/ 23

//check whether a number is prime or not!

#include<stdio.h>;

int main()

//{

// int a,n,i,ans;

// printf("Enter a=");

// scanf("%d",&a);

// printf("\n The prime number from 1 to %d is:",a);

// n=2;

// while(n<=a)

// {

// ans=1;

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

// {if(n%i==0)

// ans=0;

// break;}

// if(ans==1)

// printf("%d",n);

// n++;

// return 0;

//}}

//tong cua cac chu so, vd:2345=2+3+4+5

//{

//int n,sum=0;

//printf("input n:");

//scanf("%d",&n);

//while(n!=0) {

// sum+=n%10;

// n=n/10;

//}

//printf("sum of digits of n is %d",sum);


//return 0;

//}

//enter an int number a (0<a<10), print first 20 numbers of array (1,1+a,1+2a...) and summmary of them.

int a,i,sum,;

printf("enter a=");

scanf("%d",&a);

while(0<a<10)

printf("The first 20 numbers of array is: \n");

for(int i=0;i<20;i++)

printf("%d",1+a*i);

sum+=1+i*a;}

printf("\n Sum of first 20 numbers is: %d ",sum);}

return 0;

#include<stdio.h>;

int main()

//{

// float a,b,x;

// printf("Enter a=");

// scanf("%f",&a);

// printf("Enter b=");

// scanf("%f",&b);

// x=-b/a;

// if(a!=0)

// printf("x=%f",x);

// if else(a=0&&b=0) printf("infinite solutions");

// else printf("no solution");


//}

//{

//float x,y;

//printf("enter x=");

//scanf("%f",&x);

//if(x<=0)

//printf("y=%f",x+5);

//else printf("y=%f",2*x-3);

//

//display weekend

//{

//int n;

//printf("enter a number (note:n must be 1-7) n=");

//scanf("%d",&n);

//if(n<1 || n>7) printf("invalid value, 1<n<8");

//else

//{switch(n)

//{

//case 1:

//printf("\n Sunday");

//break;

//case 2:

//printf("\n Monday");

//break;

//case 3:

//printf("\n Tuesday");

//break;

//case 4:

//printf("\n Wednesday");
//break;

//case 5:

//printf("\n Thursday");

//break;

//case 6:

//printf("\n Friday");

//break;

//case 7:

//printf("\n Sartuday");

//break;

//}}

//}

int n;

printf("Welcome to Cloudy");

printf("Enter number 1-3, n=");

scanf("%d",&n);

{switch(n)

case 1:

printf("Bac Xiu - 40k");

break;

case 2:

printf("Freeze Matcha - 45k");

break;

case 3:

printf("Peach Tea - 35k");

break;

default:

printf("Invalid drink");
}}

} //#include <stdio.h>

//int main(void) {

// int x=200;

// printf("value of x=%d\n", x);

// printf("value of x=%o\n", x);

// printf("value of x=%x\n", x);

//}

#include <stdio.h>

//int main(void) {

// char x='c';

// char y='d';

// printf("sum of %c and %c is: %c\n" ,x,y,x+y);

// printf("sum of %d and %d is: %d\n" ,x,y,x+y);

//}

//#include <stdio.h>

//int main(void) {

// int i=200;

// float x=3.2167;

// char y='o';

// char z='z';

// int a=23;

// int b=5;

//

// printf("imcloud9");

// printf("\nhehehehehehehe");

// printf("\n%10.4d",i);

// printf("\n value of x=%.4f",x);

// printf("\n value of x=%g",x);

// printf("\n tong hai so: i+x=%g", i+x);


// printf("\n dang so mu cua x:%e\n",x);

//}

#include<stdio.h>

int main(void)

int a,b,c;

int sum;

printf("\nenter a:");

scanf("%d",&a);

printf("\nenter b:");

scanf("%d",&b);

printf("\nenter c:");

scanf("%d",&c);

sum=a+b+c;

printf("\na+b+c=%d", sum);

//Program with for the answer of a girl: if the boy is interesting or graceful then the answer is Yes, otherwise the
answer is No.

#include<stdio.h>

int main(void)

int x,y;

printf("Is the boy interesting?");

scanf("\n%d",&x);

if(x==1)

printf("\n yes");

else

printf("\n no");

printf("Is the boy graceful?");

scanf("\n%d",&y);

if(y==1)
printf("\n yes");

else

printf("\n no");

return 0;

//Enter 3 integer numbers from keyboard, find the largest number

int a,b,c;

printf("enter a=");

scanf("%d",&a);

printf("enter b=");

scanf("%d",&b);

printf("enter c=");

scanf("%d",&c);

if (a>=b && a>=c) {

printf("The largest number is: %d\n",a);

} else if (b>=a && b>=c) {

printf("The largest number is: %d\n",b);

} else {

printf("The largest number is: %d\n",c);

return 0;

//Ex1:Write a program to enter a number >0

//#include<stdio.h>;

//#define max

//#define factorial

//int main()

//{

// int a;
// do {

// printf("Enter a number greater than 0: ");

// scanf("%d", &a);

// if (a <= 0) {

// printf("Please enter a number greater than 0.\n");

// }

// } while (a>= 0);

//}

#include<stdio.h>;

#include <math.h>

int main(void)

float x1 = 1.0, x2 = 2.5, x3 = 3.0;

float P1, P2, P3;

P1=3 * pow(x1, 3) - 5 * pow(x1, 2) + 6;

printf("result P1:%.2f\n", P1);

P2=3 * pow(x2, 3) - 5 * pow(x2, 2) + 6;

printf("result P2:%.2f\n", P2);

P3=3 * pow(x3, 3) - 5 * pow(x3, 2) + 6;

printf("result P3:%.2f\n", P3);

return 0;

//#include<stdio.h>;

//int main(void) {

//{

// float C, F;

// printf("Enter the temperature in F:");

// scanf("%f",&F);

// C=(F-32)/1.8;
// printf("The temperature in C:%.2f",C);

//}

//#include<stdio.h>;

//#include <math.h>

//int main(void)

//{

//float x,y,z;

//printf("*******************\n");

//printf("* Happy New Year! *\n");

//printf("*******************\n");

//printf("enter x:");

//scanf("%f",&x);

//printf("enter y:");

//scanf("%f",&y);

//z=2*pow(x,4)+4*pow(y,2)+9;

//printf("result z:%.2f",z);

//}

////compare 2 int numbers a,b

//#include<stdio.h>;

//int main(void)

// {

//

// int a,b;

// printf("a=");

// scanf("%d",&a);

// printf("b=");

// scanf("%d",&b);

// if (a>b)

// printf("the larger one is %d",a);

// if (a==b)
// printf("a equal b");

// return 0;

//}

//even or odd?

//#include<stdio.h>;

//int main(void)

//{

//int a;

//printf("enter a=");

//scanf("%d",&a);

//if(a%2==0)

//printf("\n %d is even",a);

//else

//printf("\n %d is odd",a);

//return 0;

//}

//Ex3:Write a program to enter 3 integer numbers from keyboard, choose a number from 1 to 3 to pick one number
to calculate the factorial of it.

//#include<stdio.h>;

//#define max

//int enternumber()

//{ int num;

//printf("enter an integer number>0:");

//scanf("%d",&num);

//for(;;)

//{ if(num<=0)

//{printf("enter an integer number>0:");

//scanf("%d",&num);

//}else break;
//}

//return num;

//}

////tao function tinh giai thua

//long Factorial(int n)

//{ int i;

//long result=1;

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

//{result=result*i;

//}

//return result;

//}

//

//int main(void)

//{ int a,b,c;

//long fact;

//int choice;

////nhap 3 so

//printf("enter 3 integer numbers: \n");

//a=enternumber();

//printf("\nenter 1st number=%d",a);

//b=enternumber();

//printf("\nenter 1st number=%d",b);

//c=enternumber();

//printf("\nenter 1st number=%d",c);

//

////chon so can tinh giai thua

//printf("\n select 1 number 1-3:");

//scanf("%d",&choice);

//while(choice<1||choice>3)

//{printf("\n Select 1 number 1-3:");

//scanf("%d",&choice);
//}

////tinh giai thua cho so ban chon

//switch(choice)

// { case 1: fact=Factorial(a); break;

// case 2: fact=Factorial(b); break;

// case 3: fact=Factorial(c); break;

// }

//printf("\n Factorial of selected number is:%d",fact);

//}

//Ex4:Write a program to enter the sale amount of Quarter 1 -> 4 and show the VAT taxes amount (10%) for each
Quarter and Total tax.

//Ex5: enter costs of goods from vendor A,B,C and total sales of each of them. Show the max value for cost, sale
amount and margin (margin=(sale price-cost price)/cost price)

#include<stdio.h>;

#define max

//

//int enteramount()

//{ int amount;

//printf("enter a number>0:");

//for(;;)

//scanf("%f",&amount);

//{ if(amount<=0)

//{printf("enter a number>0:");

//scanf("%f",&amount);

//}else break;

//}

//return amount;

//

//

//int main()

//{
// float a,b,c, Sa, Sb, Sc;

// printf("cost of good from vendor A="); scanf("%f",&a);

// printf("sale amount of vendor A="); scanf("%f",&Sa);

// printf("cost of good from vendor B="); scanf("%f",&b);

// printf("sale amount of vendor B="); scanf("%f",&Sb);

// printf("cost of good from vendor C="); scanf("%f",&c);

// printf("sale amount of vendor C="); scanf("%f",&Sc);

// int maximum=max(a,max(b,c));

//}

//Ex6: Enter the scores of Math (x2), Physics, English of 3 students

//-show the avg score (math x2) of each student

//-highlight the student who has highest score.

#include<stdio.h>;

float enternumber()

{ float num;

printf("enter a number>0:");

for(;;)

scanf("%f",&num);

{ if(num<=0)

{printf("enter a number>0:");

scanf("%f",&num);

}else break;

return num;

float Avg(float a,float b, float c)

{return (a*a+b+c)/4;
}

float Max(float a,float b, float c)

{if(a>=b&&a>=c) return a;

if(b>=a&&b>=c) return b;

if(c>=b&&c>=a) return c;

int main()

int a,b,c;

printf("Enter a:"); scanf("%f",&a);

printf("\nEnter b:"); scanf("%f",&b);

printf("\nEnter c:"); scanf("%f",&c);}}

//Menu 5 types of drink, write program to enter a drink selection and print out the drink with its price. Ask the top up
milk for coffee with add 5k into the price.

#include<stdio.h>;

int main()

int n,m,l1,l2;

printf("Welcome to Cloudy");

printf("\n Enter number 1-5 n=");

scanf("%d",&n);
{switch(n)

case 1:

printf("Bac Xiu - 40k");

break;

case 2:

printf("Freeze Matcha - 45k");

break;

case 3:

printf("Peach Tea - 35k");

break;

case 4:

printf("Smoothie - 45k");

break;

case 5:

printf("Waterlemon juice - 40k");

break;

default:

printf("Invalid drink");

}}

printf("\n If you want to add more milk for coffee with, add 5k into the price!");

printf("\n Choose 1 for More milk, Choose 0 for No more milk,m=");

scanf("%d",&m);

if (m == 1) {

if (n == 1) {

printf("Total price: %dk\n", 40 + 5);}

else if (n == 2){

printf("Total price: %dk\n", 45+ 5);}

else if (n == 3){

printf("Total price: %dk\n", 35 + 5);}

else if (n == 4){

printf("Total price: %dk\n", 45+5);}


else{

printf("Total price: %dk\n", 40 + 5);}}

if (m == 0) {

if (n == 1) {

printf("Total price: %dk\n", 40);}

if (n == 2){

printf("Total price: %dk\n", 45);}

if (n == 3){

printf("Total price: %dk\n", 35);}

if (n == 4){

printf("Total price: %dk\n", 45);}

else{

printf("Total price: %dk\n", 40);}

return 0;

//Write a program to solve quadratic equation: ax2+ bx + c = 0 with a,b,c from the keyboard. (use library: <math.h>,
statement sqrt() for square root function).

#include<stdio.h>;

#include<math.h>;

int main()

float a,b,c,x,x1,x2,d;

printf("The equation: a*pow(x,2)+b*x+c=0 (a!=0)");

printf("\nEnter a=");

scanf("%f",&a);

printf("Enter b=");

scanf("%f",&b);

printf("Enter c=");

scanf("%f",&c);
d=b*b-(4*a*c);

printf("We have d=b*b-(4*a*c)=%.2f",d);

if (d<0) {

printf("\n No solution");}

else if(d=0) {

printf("\n The equation has a double solution: x1=x2=-b/(2*a)");

printf("\n x1=x2=%.2f",-b/(2*a));}

else {

printf("\n The equation has two distinct solutions:x1=(-b+sqrt(d))/(2*a),x2=(-b-sqrt(d))/(2*a)");

printf("\n x1=(-b+sqrt(d))/(2*a)=%.2f",(-b+sqrt(d))/(2*a));

printf("\n x1=(-b-sqrt(d))/(2*a)=%.2f",(-b-sqrt(d))/(2*a));}

return 0;

//Enter scores (in range 10) of 3 subjects & its credit, calculate the average score in range 4 and classify it with
following conditions: A (3.4-4.0), B(2.8 - <3.4),C (2.2- <2.8), D (1.6- <2.2), F (<1.6).

#include<stdio.h>;

int main()

float a,b,c,d;

int a1,b1,c1;

printf("Enter the score a (in rang 10):");

scanf("%f",&a);

printf("Enter the credit of a:");

scanf("%d",&a1);

printf("\n Enter the score b (in rang 10):");

scanf("%f",&b);

printf("Enter the credit of b:");

scanf("%d",&b1);

printf("\n Enter the score c (in rang 10):");

scanf("%f",&c);

printf("Enter the credit of c:");


scanf("%d",&c1);

d=(float)(((a*a1+b*b1+c*c1)/(a1+b1+c1))*0.4);

printf("Your score in range 4 is:%.1f",d);

if(d>3.4&&d<4.0) {

printf ("\n You get an A");}

else if(d>2.8&&d<3.4) {

printf ("\n You get a B");}

else if(d>2.2&&d<2.8) {

printf ("\n You get a C");}

else if(d>1.6&&d<2.2) {

printf ("\n You get a D");}

else {printf ("\n You get a F");}

return 0;

//Program with for the answer of a girl: if the boy is interesting or graceful then the answer is Yes, otherwise the
answer is No.

#include<stdio.h>

int main(void)

//{

// int x=0,y=1;

// printf("Is the boy interesting?");

// scanf("\n%d",&x);

// if(x==1)

// printf("\n yes");

// else

// printf("\n no");

// printf("Is the boy graceful?");

// scanf("\n%d",&y);

// if(y==1)

// printf("\n yes");

// else

// printf("\n no"); return 0;}

You might also like