You are on page 1of 17

เรื่อง หลักการเขียนโปรแกรม

จัดทำโดย
นางสาว พลอยนรินทร์ วอไทสง เลขที่ 25 ปวส.1/3

เสนอ
อาจารย์ วราภรณ์ อุ้มอังวะ

รายงานนี้นะเป็ นส่วนหนึ่งของวิชาหลักการเขียน
โปรแกรม 20204-2004
คณะเทคโนโลยีอุตสาหกรรม สาขาเทคโนโลยีธุรกิจ
ดิจิทัล มหาวิทยาลัยนครพนม

1.โปรแกรมบวกเลข ของนิพจน์ต่อไปนี้ C=A+B


โค้ดโปรแกรม
#include<stdio.h>
main()
{
float a,b,c;
printf("Enter number a=");
scanf("%f",&a);
printf("Enter number b=");
scanf("%f",&b);
c=a+b;
printf("c=%.2f\n",c);
}
ผลลัพธ์โปรแกรม

2.โปรแกรมใบเสร็จรับเงิน โดยมีตัวแปรดังต่อไปนี้ รหัส


สินค้า,ชื่อ,จำนวน,ราคา,ราคารวม,ส่วนลด 10%,ราคาสุทธิ
โค้ดโปรแกรม
#include<stdio.h>
main()
{
int quan;
char code[4];
char name[20];
float price,total,discount,net;

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

printf("===============================
====\n");
printf(" EnterCode = ");
scanf("%s",code);
printf(" EnterName = ");
scanf("%s",name);
printf(" EnterQuan = ");
scanf("%d",&quan);
printf(" EnterPrice = ");
scanf("%f",&price);
total=quan*price;
discount=total*0.10;
net=total-discount;
printf("===============================
====\n");
printf(" Tatal =%.2f\n",total);
printf(" Discount =%.2f\n",discount);
printf(" Net =%.2f\n",net);
printf("===============================
====\n");
}
ผลลัพธ์โปรแกรม
3.โปรแกรมค่าคอมมิชชั่น โดยมีตัวแปรดังต่อไปนี้ รหัส
สินค้า,ชื่อ,สินค้า 1,สินค้า 2,สินค้า 3,ราคารวม,ค่า
คอมมิชชั่น 10%
โค้ดโปรแกรม
#include<stdio.h>
main()
{
char code[6];
char name[20];
float sales1,sales2,sales3,total,commis;
printf("===============================
====\n");
printf(" ProgramSale \n");
printf("===============================
====\n");
printf(" EnterCode =");
scanf("%s",code);
printf(" EnterName =");
scanf("%s",name);
printf(" Enter sales1 =");
scanf("%f",&sales1);
printf(" Enter sales2 =");
scanf("%f",&sales2);
printf(" Enter sales3 =");
scanf("%f",&sales3);
total=sales1+sales2+sales3;
commis=total*0.10;
printf("===============================
====\n");
printf(" Total =%.2f\n",total);
printf(" commiss =%.2f\n",commis);
printf("===============================
====\n");
}
ผลลัพธ์โปรแกรม
4.โปรแกรมภาษี โดยมีตัวแปรดังต่อไปนี้ รหัสประจำ
ตัว,ชื่อ,ชั่วโมง/เวลา,ค่าแรงต่อชั่วโมง,ค่าแรง,ภาษี10%,ค่าแรง
สุทธิ
โค้ดโปรแกรม
#include<stdio.h>
main()
{
int hours;
char code[4];
char name[20];
float rate,salary,tax,net;
printf("===============================
====\n");
printf(" ProgramTax \n");
printf("===============================
====\n");
printf(" EnterCode = ");
scanf("%s",code);
printf(" EnterName = ");
scanf("%s",name);
printf(" EnterHours = ");
scanf("%d",&hours);
printf(" EnterRate = ");
scanf("%f",&rate);
salary=hours*rate;
tax=salary*0.10;
net=salary-tax;
printf("===============================
====\n");
printf(" Salary =%.2f\n",salary);
printf(" Tax =%.2f\n",tax);
printf(" Net =%.2f\n",net);
printf("===============================
====\n");
}
ผลลัพธ์โปรแกรม
5.โปรแกรมเกรด โดยมีตัวแปรดังต่อไปนี้ คะแนนเกรด
โดยมีเงื่อนไขว่า ถ้าคะแนนมากกว่าหรือ = 80
ได้เกรด 4 ถ้าคะแนนมากกว่าหรือ = 70 ได้เกรด 3
ถ้าคะแนนมากกว่าหรือ = 60 ได้เกรด 2
ถ้าคะแนนมากกว่าหรือ = 50 ได้เกรด 1
โค้ดโปรแกรม
#include<stdio.h>
#include<conio.h>
main()
{
int score;
char grade;
printf("Enter score = ");
scanf("%d",&score);
if(score>=80)
grade = '4';
else if(score>=70)
grade = '3';
else if(score>=60)
grade = '2';
else if(score>=50)
grade = '1';
else
grade = '0';
printf("you grade is %c",grade);
}
ผลลัพธ์โปรแกรม
6.โปรแกรมส่วนลด โดยมีตัวแปรดังต่อไปนี้ รหัส
สินค้า,ชื่อ,จำนวน,ราคา,ราคารวม,ส่วนลดสินค้า,ราคาสุทธิ
โดยมีเงื่อนไขดังนี้
ถ้าซื้อสินค้าราคา <=499 discount = 0
ถ้าซื้อสินค้าราคา <=3000 discount = 0.03
ถ้าซื้อสินค้าราคา <=5000 discount = 0.05
ถ้าซื้อสินค้าราคา <=8000 discount = 0.07
ถ้าซื้อสินค้าราคา >8000 discount = 0.10
โค้ดโปรแกรม
#include<stdio.h>
main()
{
int quan;
char code[4];
char name[20];
float price,total,discount,net;
printf("===============================
====\n");
printf(" ProgramSale \n");
printf("===============================
====\n");
printf(" EnterCode = ");
scanf("%s",code);
printf(" EnterName = ");
scanf("%s",name);
printf(" EnterQuan = ");
scanf("%d",&quan);
printf(" EnterPrice = ");
scanf("%f",&price);
total=quan*price;
if(total<=499)
{
discount= 0;
}
else if(total<=3000){
discount=total*0.03;
}
else if(total<=5000){
discount=total*0.05;
}
else if(total<=8000){
discount=total*0.07;
}
else if(total>8000){
discount=total*0.10;
}
net= total-discount;

printf("===============================
====\n");
printf(" Tatal =%.2f\n",total);
printf(" Discount =%.2f\n",discount);
printf(" Net =%.2f\n",net);

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

ผลลัพธ์โปรแกรม

You might also like