SKTN Solutions For C Part 1 - Online Article
Introduction
For a long time, I was pressurized to teach C to my friends and colleagues. As due to my busy schedule, this was not feasible. So, I decided to throw light on the solutions of the problems everyone faces in the learning phase of C. This is NOT a tutorial but the solutions of the problems where you or your friends will halt and will find it difficult to overcome. This tutorial is the first one in the series. The later tutorials will continue the trend and will discuss more challenging problems. So, let us once again delve into the ocean of C.
Problem 1: If a five digit number is input from the keyboard, write a program to calculate the sum of digits without using loop.
Every single part of a number is called a digit. For example in the number 123, the digits are 1, 2 and 3. Therefore, sum of digits of a five digit number 12345 is 1+2+3+4+5 = 15. To solve this problem, we will use % (modulus) operator of C. This operator divides first number by second and returns the remainder. For example, 10%3 = 1 and 15%4 = 3. Now, we will use this % (modulus) operator to extract each digit of the five digit number and then we will add them. Let us C the code now,
#include <stdio.h> void main() { int number = 12345, sum = 0, current_digit; current_digit = number % 10; sum = sum + current_digit; number = number / 10; current_digit = number % 10; sum = sum + current_digit; number = number / 10; current_digit = number % 10; sum = sum + current_digit; number = number / 10; current_digit = number % 10; sum = sum + current_digit; number = number / 10; current_digit = number % 10; sum = sum + current_digit; number = number / 10; printf("Sum of digits of 12345 is %d",sum); getch(); }
The input number is taken in number and sum is used for storing the sum of digits. The sum is initialized as 0. In the program, five times the following processes happen:
- The digit at the right-most position in the number is extracted in current_digit.
- The current_digit is then added to sum.
- The number is divided by 10 so that its value gets reduced by a factor of 10 and next digit can be extracted.
In this way, the digits are extracted in the sequence of right-to-left (5 then 4 then 3 then 2 then 1) and we get the result in sum which is shown on the screen by printf() function. The getch() function is optional and is used just to halt the screen until the user hits the enter key. To get the sum of digits of a different number, just change the value of number.
Problem 2: Write a program to reverse a five digit number without using loop.
Every single part of a number is called a digit. For example in the number 123, the digits are 1, 2 and 3. Therefore, the reverse of digits of a five digit number 12345 is 54321. To solve this problem, we will use % (modulus) operator of C. This operator divides first number by second and returns the remainder. For example, 10%3 = 1 and 15%4 = 3. Now, we will use this % (modulus) operator to extract each digit of the five digit number and then we will display each of them separately. Let us C the code now,
#include <stdio.h> Void main() { int number = 12345, current_digit; current_digit = number % 10; printf("%d",current_digit); number = number / 10; current_digit = number % 10; printf("%d",current_digit); number = number / 10; current_digit = number % 10; printf("%d",current_digit); number = number / 10; current_digit = number % 10; printf("%d",current_digit); number = number / 10; current_digit = number % 10; printf("%d",current_digit); number = number / 10; getch(); }
The input number is taken in number. In the program, five times the following processes happen:
- The digit at the right-most position in the number is extracted in current_digit.
- The current_digit is then displayed on the screen by printf().
- The number is divided by 10 so that its value gets reduced by a factor of 10 and next digit can be extracted.
In this way, the digits are extracted in the sequence of right-to-left (5 then 4 then 3 then 2 then 1) and we get the reversed number on the screen by calling printf() function for each digit. The getch() function is optional and is used just to halt the screen until the user hits the enter key. To get the reverse number of a different number, just change the value of number.
Problem 3: Write a program to convert a five digit number to print a new number by adding one to each of its digits. For Example if 12391 is given, output should be 23402.
Every single part of a number is called a digit. For example in the number 123, the digits are 1, 2 and 3. To solve this problem, we will use % (modulus) operator of C. This operator divides first number by second and returns the remainder. For example, 10%3 = 1 and 15%4 = 3. Now, we will use this % (modulus) operator to extract each digit of the five digit number and then we will add one to each of them. Let us C the code now,
#include <stdio.h> void main() { int number = 12391, new = 0, current_digit; current_digit = (number % 10) + 1; current_digit = (current_digit % 10); new = new + current_digit; number = number / 10; current_digit = (number % 10) + 1; current_digit = (current_digit % 10) * 10; new = new + current_digit; number = number / 10; current_digit = (number % 10) + 1; current_digit = (current_digit % 10) * 100; new = new + current_digit; number = number / 10; current_digit = (number % 10) + 1; current_digit = (current_digit % 10) * 1000; new = new + current_digit; number = number / 10; current_digit = (number % 10) + 1; current_digit = (current_digit % 10) * 10000; new = new + current_digit; number = number / 10; printf("Increment of digits of 12391 is %d",new); getch(); }
The input number is taken in number and new is used for storing the new number. The new is initialized as 0. In the program, five times the following processes happen:
- The digit at the right-most position in the number is extracted in current_digit.
- One is added to current_digit.
- The remainder of current_digit on dividing it by 10 is calculated by using current_digit%10. This is done to ensure that a digit must never exceed 9 on adding one to it in step (b).
- The current_digit is multiplied by 1, 10, 100, 1000 and 10000 respectively.
- The number is divided by 10 so that its value gets reduced by a factor of 10 and next digit can be extracted.
In this way, the digits are extracted in the sequence of right-to-left (5 then 4 then 3 then 2 then 1) and are incremented. Each digit is then multiplied with its appropriate factor (1, 10, 100, 1000, 10000 respectively) and is added to new. Thus, we get the result in new which is shown on the screen by printf() function. The getch() function is optional and is used just to halt the screen until the user hits the enter key. To get the incremented sum of digits of a different number, just change the value of number.
Problem 4: Write a program to find out whether a year is leap year or not.
A leap year is a year containing an extra day in order to keep the calendar year synchronized with the astronomical or seasonal year. The Gregorian calendar (the calendar we use) adds a 29th day to February in all years evenly divisible by four, except for centennial years (those ending in -00), which receive the extra day only if they are evenly divisible by 400. Thus 1600, 2000 and 2400 are leap years but 1700, 1800, 1900, 2100, 2200 and 2300 are not. Let Us C the code now,
#include <stdio.h> void main() { int year = 1986; if(year % 400 == 0) printf("This is a leap year."); else if(year % 100 == 0) printf("This is NOT a leap year."); else if(year % 4 == 0) printf("This is a leap year."); else printf("This is NOT a leap year."); getch(); }
The logic above explained is implemented as it is using if...else... statements. To get leap year information about any other year, just change the value of year.
Problem 5: Write a program to calculate sum of digits of a number by using loop.
Every single part of a number is called a digit. For example in the number 123, the digits are 1, 2 and 3. Therefore, sum of digits of a five digit number 12345 is 1+2+3+4+5 = 15. To solve this problem, we will use % (modulus) operator of C. This operator divides first number by second and returns the remainder. For example, 10%3 = 1 and 15%4 = 3. Now, we will use this % (modulus) operator to extract each digit of the five digit number and then we will add them. Let us C the code now,
#include <stdio.h> void main() { int number = 12345, sum = 0, current_digit; while( number != 0 ) { current_digit = number % 10; sum = sum + current_digit; number = number / 10; } printf("Sum of digits of 12345 is %d",sum); getch(); }
The input number is taken in number and sum is used for storing the sum of digits. The sum is initialized as 0. In the program, the loop is run until the number becomes zero. Every time the following processes happen:
- The digit at the right-most position in the number is extracted in current_digit.
- The current_digit is then added to sum.
- The number is divided by 10 so that its value gets reduced by a factor of 10 and next digit can be extracted.
In this way, the digits are extracted in the sequence of right-to-left (5 then 4 then 3 then 2 then 1) and the process continues until number becomes zero. Thus, we get the result in sum which is shown on the screen by printf() function. The getch() function is optional and is used just to halt the screen until the user hits the enter key. To get the sum of digits of a different number, just change the value of number.
About the Author:
No further information.
Related Online Articles:
Comments
No comment yet. Be the first to post a comment. |