วันศุกร์ที่ 23 กันยายน พ.ศ. 2554

โปรแกรมหาค่า ห.ร.ม.(G.C.D program)Positive intergers หารร่วมมาก

โปรแกรมหาค่า ห.ร.ม.(หารร่วมมาก input 2 ค่า)


#include
main(){
int num1,num2,check;
printf("\t\t**********************************************\n");
printf("\t\t\t\tThe G.C.D program.\n");
printf("\t\t**********************************************\n");
printf("\t\t\tEnter two positive intergers: ");
scanf("%d %d",&num1,&num2);/*8,4*/
if(num1 check=num1;
}else{
check=num2;/*check=4*/
}
while(((num1%check)!=0)||((num2%check)!=0)){/*ทำการวนลูป*/
check=check-1;
}
printf("\t\t**********************************************\n");
printf("\t\t\t\tThe G.C.D of %d and %d is %d\n",num1,num2,check);
printf("\t\t**********************************************\n");

}

วันพฤหัสบดีที่ 15 กันยายน พ.ศ. 2554

โค๊ตโปรแกรมเป่ายิ้งฉุบ แข่งกับคอมฯ ตอนที่ 2

โค๊ตโปรแกรมเป่ายิ้งฉุบ แข่งกับคอมฯ  ตอนที่ 2
โปรแกรมภาษาซี ตอน โค๊ต ตัวอย่างโปรแกรมเป่ายิ๊งฉุบ ตอนที่ 2


#include <stdio.h>
#include <time.h>

main(){
    int i,ran,user2,cp1=0,cp2=0;
    char com,user;
    srand(time(0));
    printf("\t\tProgram game powjingshup r=rock p=paper s=scissor.\n");
    for(i=1;i<=5;i++){
            ran= (rand()%3)+1;

            if(ran==1){
                    com='r';
                }else if(ran==2){
                    com='p';
                }else{
                    com='s';
                }

            printf("\n\t%d. Time player1:",i);
            scanf("%c",&user);
            printf("\t%d. Time player2: %c \n",i,com);
                if(user=='r'){
                    user2=1;
                }if(user='p'){
                    user2=2;
                }if(user='s');
                    user2=3;



            if((ran==user2)){
                printf("\tEqual\n");
                }
            if((ran==1&&user2==3)||(ran==2&&user2==1)||(ran==3&&user2==2)){
                printf("\n\tPlayer2 Win!\n");
                cp2+=1;
            }else{
                printf("\n\tPlayer1 Win!\n");
                cp1+=1;
            }



    }


}

วันพุธที่ 14 กันยายน พ.ศ. 2554

โค๊ตโปรแกรมภาษาซี ตรวจสอบตำแหน่งตัวเลขที่รับเข้ามาว่าอยู่ที่ตำแหน่งใด

โปรแกรมตรวจสอบตำแหน่งตัวเลขที่รับเข้ามาว่าอยู่ที่ตำแหน่งใด

สำหรับโปรแกรมนี้นะครับ เราจะมาตรวจสอบว่า ตัวเลขที่เรารับ่เข้ามาจำนวน 10 จำนวน

                  เช่น Input : 1234567890
                  และต้องการรู้ว่า เลข 9 อยู่ตำแหน่งใดที่เรารับเข้ามา

โปรแกรมรับเลขจำนวนเต็ม 10 จำนวน จากนั้นรับตัวเลขที่ต้องการเข้ามาตรวจสอบว่าตัวเลขนั้น
อยู่ที่ตำแหน่งใด

#include
int main(){
int i,j,k,check,checknall,pnum[10];
printf("Check index number 10 number.\n");
for(i=1;i<=10;i++){
printf("Input number[%d]:",i);
scanf("%d",&pnum[i]);
}
printf("\n");
printf("You're input 10 positive numbers.\n");
for(j=1;j<=10;j++){
printf("[%d]%d ",j,pnum[j]);
if(j==5){
printf("\n");
}

}
printf("\nCheck index integer:");
scanf("%d",&check);
for(k=1;k<=10;k++){
if(check==pnum[k]){
printf("Index integer is %d\n",k);
checknall=1;
break;
}
}
if(checknall!=1)
printf("No index position!\n");
return (0);
}

ปล.เราต้องการตำแหน่งนะครับ อิอิ ว่าตัวที่เราต้องการตรวจสอบอยู่ตำแหน่งที่เท่าไหร่ที่เรารับเข้ามา


วันอังคารที่ 13 กันยายน พ.ศ. 2554

โปรแกรมเพื่อทำการ transpose เมตริกซ์

โปรแกรมเพื่อทำการ transpose เมตริกซ์ขนาด nXm ที่ใช้เก็บข้อมูลจำนวนเต็ม
#include
              main(){
                        int n,i,j;
                        printf("Input dimension of Matrix:");
                        scanf("%d",&n);
                        int mat[n][n];
                             printf("Input element nXn matrix.\n");
                        for(i=0;i




          int transp[n][n];
            for(i=0;i<n;i++){
                for(j=0;j<n;j++){
                    transp[i][j]=mat[j][i];
                }
            }
            for(i=0;i<n;i++){
                for(j=0;j<n;j++){
                    printf("%d ",transp[i][j]);
                }
            printf("\n");
            }
}

มีคำถามสงสัยถาม-ตอบกันได้ครับ หัดคิด ทำโจทย์บ่อยนะครับ