C Programming Questions and Answers
// a) If a 5 digit number is input through the keyboard, write a program to print the sum. #include <stdio.h> int main(){ int number, sum = 0, remainder = 0, counter = 0; printf("Enter an integer\n"); scanf("%d", &number); while (number != 0){ remainder = number%10; counter += 1; sum += remainder; number = number/10; } if(counter == 5){ printf("Sum of digits of %d.\n", sum); } else{ printf("\nIt was not 5 digit number.\n"); } return 0; } // b) If a four digit number is input through the keyboard, write a program to find the sum of first and last digit. #include <stdio.h> int main(){ int number, sum = 0, remainder = 0, counter = 0; printf("Enter an integer\n"); scanf("%d", &number); while (number != 0){ remainder = number%10; counter += 1; if(counter == 1 || counter == 4