SKTN Solutions For C Part 3 - Online Article
Introduction
The tutorial series "SKTN Solutions for C" is again continued with this tutorial. In this part 3 of the series, we will take a different set of problems generally using loops. These problems may seem easy at first glance but at the time of coding they can prove out to be a night burner. I hope you will enjoy and understand. So, let us once again delve into the ocean of C.
Problem 11: Write a program to print all prime numbers from 1 to 300 using loops.
A prime number is a number which is only divisible completely by 1 and itself. For example: 5, 7 and 13. The lowest prime number is 2 as 1 is not considered a prime number. We can divide each number between 1 and 300 by every number less than it. If none of them divides completely the number, then the number is concluded as a prime number. To check whether a number completely divides another, 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.
One more trick we can apply to this. As we know that none of the number, more than half of itself, can divide it completely. For example: 100 can only be completely divided by 50 or less, that is less than or equal to half of it. Let us C the code now,
#include <stdio.h> #include <conio.h> void main() { int i,j,flag; for(i=2; i<=300; i++) { flag = 0; for(j=2;j<=i/2;j++) if(i%j == 0) flag = 1; if(flag == 0) printf("\t%d",i); } getch(); }
The i loop runs from 2 to 300 and provides each number for checking in the form of i. Every time the main loop runs, the following process happen,
- An integer flag is set to zero initially.
- Another loop of j runs from 2 to (i/2) and checks if I can be completely divided by j. If it does so, flag is set to 1.
- Flag is checked. If it is zero then the number in i is prime. This is because a zero in flag indicates that it is never set to 1 during step (b) which in turn means that i was never divided by j.
In this way, all the prime numbers between 1 and 300 are printed on the screen. The getch() function is optional and is used just to halt the screen until the user hits the enter key.
Problem 12: Write a program to fill the entire screen with diamond and heart alternatively. The ASCII value for heart is 3 and that of diamond is 4.
ASCII stands for "American Standard Codes for Information Interchange". Every character has an ASCII code. For example: 'A' has ASCII code of 65, 'B' is 66, etc. Similarly a character that looks like a heart has an ASCII code of 3 and diamond has ASCII code of 4. To tackle this problem, we will store 3 and 4 in two char and print them on screen. Let us C the code now,
#include <stdio.h> #include <conio.h> void main() { char heart = 3, diamond = 4; int i; for(i=1; i<2000; i++) printf("%c%c",heart,diamond); getch(); }
The logic of the program is quite straightforward. The loop is run 2000 times as a general text screen has a resolution of 80x25 that makes 2000 characters to be printed at one time. In this way, entire screen is filled with diamonds and hearts alternatively. The getch() function is optional and is used just to halt the screen until the user hits the enter key. To get any other character printed, just change the value of heart and diamond.
Problem 13: Write a program to calculate the factorial value of a integer using loop.
The factorial of an integer can be calculated by the formula:
Factorial of x = x * (x - 1) * (x - 2) * (x - 3) * ... * 1
For example: factorial of 5 is 5*4*3*2*1 = 120. The logic is to keep multiplying the numbers from 1 up to the number of which factorial is to be calculated. Let us C the code now,
#include <stdio.h> #include <conio.h> void main() { int i, factorial = 1, number = 5; for(i = 1; i<=number; i++) Factorial = factorial * i; printf("Factorial is %d", factorial); getch(); }
Now, we have taken three int, i for running the loop, number for storing the input number of which factorial is to be calculated and factorial for storing the factorial of the number.
The main loop runs number times. It starts from 1 and keeps on running until i becomes number. Each time the value of factorial is multiplied by the value of i and stored in factorial.
In this way the exact value of factorial is printed on the screen. The getch() function is optional and is used just to halt the screen until the user hits the enter key. To get the factorial of a different number, just change the vale of number.
Problem 14: Write a program to obtain the prime factors of a number.
The prime factors of a number are the lowest prime numbers by which a number can be fully divided. For example: prime factor of 24 is 2, 2, 2 and 3 because 2*2*2*3 = 24. Another example is 50 whose prime factors are 2, 5 and 5.
Thus, the problem basically extends the prime numbers generating problem. To understand this problem, please understand the program to print the prime numbers (Problem 11 of this tutorial). To solve this problem, we will generate all the prime numbers less than the input number. Then each prime number will be chacked whether it can divide the input number or not. The number of times it divides the input number are printed on the screen.
Let us C the code now,
#include <stdio.h> #include <conio.h> void main() { int i, j, flag, number=24; for(i=2; i<=number; i++) { flag = 0; for(j=2;j<=i/2;j++) if(i%j == 0) flag = 1; if(flag == 0) while(number % i == 0) { printf("\t%d",i); number = number / i; } } getch(); }
The number whose prime factors are to be calculated is stored in number. The rest of the integers are to calculate the prime numbers. Inside the main loop, if i comes out to be a prime number, then in the while loop it is checked that how many times the number can be divided completely by i. Every time i divides the number, the value of number is reduced by a factor of i.
In this way, the prime factors of a number are printed on the screen. The getch() function is optional and is used just to halt the screen until the user hits the enter key. To get prime factors of a different number, just change the value of number.
Problem 15: For three variables x, y, z, write a function to circularly shift their values to the right.
Right shifting of the values means that if initially the variables hold the value 2, 3 and 4, then after a right shift they must hold the value 3, 4 and 2. To right shift the values, we will write a function in which the address of the three integers will be sent and the values will be interchanged. This is a little bit different problem, so first Let us C the code now,
#include <stdio.h> #include <conio.h> void shift_right(int *a,int *b,int *c) { int temp = *a; *a = *b; *b = *c; *c = temp; } void main() { int x=2, y=3, z=4; printf("Before Shifting: %d %d %d",x,y,z); shift_right(&x,&y,&z); printf("Right Shifted: %d %d %d",x,y,z); }
In the function shift_right, addresses of the three integers are passed. The values are interchanged with the help of a temporary variable temp. As the values are passed by address, the values are changed permanently and we do not need to return the values. The values are initially taken in the variables x, y and z as 2, 3 and 4. After shifting the values are printed 3, 4 and 5.
In this way, the values are right shifted and are printed on the screen. The getch() function is optional and is used just to halt the screen until the user hits the enter key. We can right shift any other set of values by just changing the values of x, y and z.
About the Author:
No further information.
Related Online Articles:
Comments
No comment yet. Be the first to post a comment. |