C語言實現輸出一組數字中的所有奇數


 
         
/*第二題*/
#include<stdio.h>
//輸入186732468
//輸出173
//輸入12345677
//輸出13577
main(){
    int input,length=0;//輸入的數字,數字的長度
    printf("請輸入:\n");
    //input=186732468;
    scanf("%d",&input);
    int tmp=0;tmp=input;//緩存一下原始數據
    if(input==0){
        printf("長度為:%d\n",1);
    }else{
        while(input){
            input=input/10;
            length++;
        }
    }
    input=tmp;
    int i=length-1;
    int array[length];
    for(i;i>=0;i--){
        array[i]= input%10;
        input=(input-array[i])/10;
    }
    i=0;
    printf("輸出結果:");
    for(i;i<length;i++){
        if(array[i]%2){printf("%d",array[i]);}
    }
}

實現思路:

先從鍵盤輸入一個數,暫定為int類型。(實際上以int類型會比較復雜)

然后獲得這個數有多少數字,存在length中,便於后期使用它來控制循環次數

然后遍歷循環,把數中的所有數字存進一個數組中。

遍歷數組輸出奇數。

以上步驟可以省略。自行優化。

新手級別代碼。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM