Write a program to print even or odd numbers in given range

Write a program to print even or odd numbers in given range.
Using function

  1. #include <stdio.h>

  2. int even(int lowerLimit, int upperLimit)
  3. {
  4.     int i, r, count=0;
  5.     printf("\nDisplay the even numbers between %d and %d are: ", lowerLimit, upperLimit);
  6.     for(i=lowerLimit; i<=upperLimit; i++){
  7.         r = i%2;
  8.         if(r == 0) {
  9.            printf("%3d", i);
  10.            count++;
  11.         }
  12.     }
  13.     //printf("\n\nTotal even numbers between %d and %d are: %d", lowerLimit, upperLimit, count);
  14. }

  15. int odd(int lowerLimit, int upperLimit)
  16. {
  17.     int i,r,count=0;
  18.       printf("\n\nDisplay the odd numbers between %d and %d are: ",lowerLimit, upperLimit);
  19.     for(i=lowerLimit; i<=upperLimit; i++){
  20.         r=i%2;
  21.         if(r==1) {
  22.             printf("%3d",i);
  23.             count++;
  24.             }
  25.     }
  26.     //printf("\n\nTotal odd numbers between %d and %d are:  %d", lowerLimit, upperLimit, count);
  27. }
  28. int main()
  29. {
  30.     int lowerLimit, upperLimit;
  31.     printf("Enter lower limit : ");
  32.     scanf("%d", &lowerLimit);
  33.     
  34.     printf("Enter upper limit : "); //Enter the second number for range
  35.     scanf("%d", &upperLimit);
  36.     
  37.     even(lowerLimit, upperLimit);
  38.     odd(lowerLimit, upperLimit);
  39.     return 0;
  40. }


Recursion

  1. #include <stdio.h>

  2. void printEvenOdd(int num, int limit);
  3. int main()
  4. {
  5.     int lowerLimit, upperLimit;
  6.     printf("Enter lower limit : ");
  7.     scanf("%d", &lowerLimit);  

  8.     printf("Enter upper limit : ");
  9.     scanf("%d", &upperLimit);

  10.     printf("Even or odd numbers from %d to %d given range are : ", lowerLimit, upperLimit);
  11.     printEvenOdd(lowerLimit, upperLimit);

  12.     return 0;
  13. }

  14. void printEvenOdd(int num, int limit)//recursive function
  15. {
  16.     if(num > limit)
  17.         return;
  18.     printf("%4d",num);

  19.     printEvenOdd(num+2, limit);
  20. }

একটি মন্তব্য পোস্ট করুন