Untitled
#include <stdio.h> /* Qsn: Take an integer from user If the number has 1 digit --> return sqaure of the number If the number has 2 digits --> return the difference between the sum of the digits and the original number If the number has 3 digits --> return the reverse of the given number If the number has 4 or more digits --> return the remainder of original number divided by the sum of the digits of the number */ int puzzleAdv(int n) { int temp = n, temp1 = n, temp3 = n; int sum = 0, sum1 = 0, sum3 = 0; int count = 0, result1 = 0, result2 = 0, result3 = 0, result4 = 0; while(temp>0) { count++; temp = temp/10; } switch (count) { case 1: result1 = n*n; // printf("Result1 = %d\n", result1); return result1; case 2: temp = n; sum = 0; while(temp>0) { sum = sum + (temp%10); temp = temp/10; } result2 = sum-n; // printf("Result2 = %d\n", result2); return result2; case 3: // printf("case 3 : Result \n"); while(temp1>0) { int rem = temp1%10; sum1 = (sum1*10) + rem; temp1 =temp1/10; } // printf("Result3 = %d\n", sum1); return sum1; case 4: temp3 = n; sum3 = 0; while(temp3>0) { sum3 = sum3 + (temp3%10); temp3 = temp3/10; } result4 = n%sum3; // printf("Result4 = %d\n", n%sum3); return result4; default: break; } } int main() { int n; printf("Enter an integer : \n"); scanf("%d", &n); if(n<0) { printf("Enter a positive number\n"); } int result = puzzleAdv(n); printf("Result : %d\n", result); }
Leave a Comment