C++ ตัวอย่างโจทย์

โปรแกรมภาษาซี
ไม่อยากให้ก็อป วางแล้ว รัน
แต่อยากให้ศึกษา ดูดีๆ วิเคราะห์ ประยุกต์ในการเรียนนะคะ

C++ หาพื้นที่สี่เหลี่ยม

#include<stdio.h>
#include<conio.h>
main()
{
float width,length;
printf("Enter width : ");
scanf("%f",&width);

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

printf("area = %.2f\n\n",width*length);
getch;
}



C++ พื้นที่สามเหลี่ยม

#include <stdio.h>

int main()
{
float base;
float height;

printf("\nInput value of base : ");
scanf("%f", &base);

printf("Input value of height : ");
scanf("%f", &height);

printf("\nArea of a Triangle is %.2f \n\n", 0.5 * base * height);

return 0;
}

C++ ตรวจสอบอายุ
#include<stdio.h>
#include<conio.h>
main()
{
int age;
printf("Plaese input your age :");
scanf("%d",&age);

if(age<25)
{
printf("Age is %d years \n",age);
printf("you are teenager \n\n");
}
else
{
printf("Age is %d \n",age);
printf("You don't teenager \n\n");
}

}

C++ หาค่ามากกว่า น้อยกว่า หรือ เท่ากับศูนย์ โดยใช่ if-else
#include<stdio.h>
#include<conio.h>
main()
{
int num;
printf("Input integer : ");
scanf("%d",&num);

if(num>0)
{
printf("Number is %d \n",num);
printf("Number more than zero \n\n");
}
else if (num<0)
{
printf("Number is %d \n",num);
printf("Number less than zero \n\n");
}
else
{
printf("Number is %d \n",num);
printf("Number be equal to zero \n\n");
}

printf("******END PROGRAM****** \n\n\n");
getch();
}

C++ เปรียนเทียบนำหนักสองคน
#include<stdio.h>
#include<conio.h>
main()
{
int w1,w2;
char name1[30],name2[30];
printf("\nPlease input name1 :");
scanf("%s",&name1);
printf("\nPlease input weight :");
scanf("%d",&w1);

printf("\nPlease input name2 :");
scanf("%s",&name2);
printf("\nPlease input weight :");
scanf("%d",&w2);

if (w1<w2)
{
printf("\n\n %s have weight is %d",name1,w1);
printf("\n %s have less than %s",name1,name2);
}
else if (w1>w2)
{
printf("\n\n %s have weight is %d",name2,w2);
printf("\n %s have waight less than %s",name2,name1);
}
else printf("END PROGRAM");
getch();

}

C++ หา vat
#include<stdio.h>
#include<conio.h>
main()
{
float price;
printf(" input Price :");
scanf("%f",&price);

printf("Vat is %.2f",0.07*price);
getch();
}

C++ Operator + - x / โดย ใช้ switch-case
#include<stdio.h>
#include<conio.h>
main()
{
float num1,num2,answer;
int opr;
printf ("\nInput number1 :");
scanf ("%f",&num1);
printf("\nInput namber2:");
scanf("%f",&num2);

printf("\nInput operator by 1=+ or 2=- or 3=* or another = / :");
scanf("%d",&opr);

switch(opr)
{
case 1: answer = num1+num2; break;
case 2: answer = num1-num2; break;
case 3: answer = num1*num2; break;
default: answer = num1/num2;
}
printf("\n\nAnswer is : %.2f\n\n\n",answer);
getch();
}


C++ การใช้ scanf ("%__",&____); ใช้นำข้อมูลเข้าทางแป้นพิมพ์ printf("%____",______); ใช้แสดงผลข้อมูลออกทางจอภาพ

#include<stdio.h>
#include<conio.h>
int main ()
{
float num1,num2;
printf("num1 = ");
scanf("%f",&num1);
printf("num2 =");
scanf("%f",&num2);

printf("\n\nsum = %.2f \n",num1+num2);
printf("Remainder = %.2f \n",num1-num2);
printf("Multiple = %.2f \n",num1*num2);
printf("Quotient = %.2f \n\n\n",num1/num2);
getch();

}

C++ ตรวจสอบ salary
#include<stdio.h>
#include<conio.h>
main()
{
int salary;
printf("Press input your salary :");
scanf("%d",&salary);

if(salary>10000)
{
printf("\nYour salary is %d \n",salary);
printf("You have good salary");
}
else
{
printf("\nYour salary is %d \n",salary);
printf("You have not good salary");
}

getch();
}
แก้ไขข้อความเมื่อ

แสดงความคิดเห็น
โปรดศึกษาและยอมรับนโยบายข้อมูลส่วนบุคคลก่อนเริ่มใช้งาน อ่านเพิ่มเติมได้ที่นี่