Write a program to print even or odd numbers in given range.
Using function
Using function
- #include <stdio.h>
- int even(int lowerLimit, int upperLimit)
- {
- int i, r, count=0;
- printf("\nDisplay the even numbers between %d and %d are: ", lowerLimit, upperLimit);
- for(i=lowerLimit; i<=upperLimit; i++){
- r = i%2;
- if(r == 0) {
- printf("%3d", i);
- count++;
- }
- }
- //printf("\n\nTotal even numbers between %d and %d are: %d", lowerLimit, upperLimit, count);
- }
- int odd(int lowerLimit, int upperLimit)
- {
- int i,r,count=0;
- printf("\n\nDisplay the odd numbers between %d and %d are: ",lowerLimit, upperLimit);
- for(i=lowerLimit; i<=upperLimit; i++){
- r=i%2;
- if(r==1) {
- printf("%3d",i);
- count++;
- }
- }
- //printf("\n\nTotal odd numbers between %d and %d are: %d", lowerLimit, upperLimit, count);
- }
- int main()
- {
- int lowerLimit, upperLimit;
- printf("Enter lower limit : ");
- scanf("%d", &lowerLimit);
- printf("Enter upper limit : "); //Enter the second number for range
- scanf("%d", &upperLimit);
- even(lowerLimit, upperLimit);
- odd(lowerLimit, upperLimit);
- return 0;
- }
Recursion
- #include <stdio.h>
- void printEvenOdd(int num, int limit);
- int main()
- {
- int lowerLimit, upperLimit;
- printf("Enter lower limit : ");
- scanf("%d", &lowerLimit);
- printf("Enter upper limit : ");
- scanf("%d", &upperLimit);
- printf("Even or odd numbers from %d to %d given range are : ", lowerLimit, upperLimit);
- printEvenOdd(lowerLimit, upperLimit);
- return 0;
- }
- void printEvenOdd(int num, int limit)//recursive function
- {
- if(num > limit)
- return;
- printf("%4d",num);
- printEvenOdd(num+2, limit);
- }
একটি মন্তব্য পোস্ট করুন