akashtadwai

Untitled

Oct 31st, 2019
231
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.11 KB | None | 0 0
  1. #include <stdio.h>
  2. #include<math.h>
  3. int isDigit(char a) { //Function to check if input is digit
  4.     if (a >= '0' && a <= '9')
  5.         return 1;
  6.     else
  7.         return 0;
  8. }
  9.  
  10. int res(long long int num) { //Recursive fn to get sum of prime digits in a number
  11.     if(num!=0) {
  12.         if (num % 10 == 2 || num % 10 == 3 || num % 10 == 5 || num % 10 == 7) //primality check
  13.             return (num % 10 + res(num / 10)); //recursive call
  14.         else return res(num / 10); //If not prime ignoring last digit
  15.     }
  16.     else return 0;
  17. }
  18. int main() {
  19.     char ch;
  20.     int cnt = 0;
  21.     long long int sum = 0;//Initialising sum
  22.     printf("Enter the number: ");
  23.     scanf("%c",&ch);//Taking input as digit by digit
  24.     while (isDigit(ch)) {//Incrementing cnt for each valid value of digit else exit while loop
  25.         cnt += 1;
  26.         sum += (ch - '0') * pow(10, 10 - cnt);
  27.         scanf("%c",&ch);
  28.     }
  29.     if (cnt == 10) //If no. of digits is 10 and a valid number we should print the result
  30.         printf("%d \n", res(sum));
  31.     else
  32.         printf("INCORRECT INPUT!! Please enter again... \n");
  33.     return 0;
  34. }
Add Comment
Please, Sign In to add comment