About Even and Odd numbers using switch function ( in C and C++)

Bismillahir Rahmanir Rahim
Solution in C:


#include<stdio.h>
void main()
{
 int n,i,count=0,sum_even=0,sum_odd=0,sum;
 printf("1.To print the Even numbers of 1 to 100 .\n");
 printf("2.To print the Odd numbers of 1 to 100 .\n");
 printf("3.To print The Even and Odd separate.\n");
 printf("Enter your choice (1 or 2 or 3).  ");
 scanf("%d",&n);
 switch(n)
 {
  case 1:
 printf("-----------------------------------------------------------------------------\n");
 printf("The Even numbers are :\n");
  for(i=1;i<=100;i++)
   {
    if(i%2==0)
     {
      printf("%d\t",i);
             sum_even=sum_even+i;
      count++;
     }
   }
  printf("\nThe sum of Even mumbers between 1 to 100 is =%d",sum_even);
  printf("\nTottal numbers = %d\n",count);
 printf("-----------------------------------------------------------------------------\n");
  break;
  case 2:
 printf("-----------------------------------------------------------------------------\n");
 printf("The Odd numbers are :\n");
  for(i=1;i<=100;i++)
   {
    if(i%2==1)
     {
      printf("%d\t",i);
             sum_odd=sum_odd+i;
      count++;
     }
   }
  printf("\nThe sum of Odd mumbers between 1 to 100 is =%d",sum_odd);
  printf("\nTottal numbers = %d\n",count);
  printf("-----------------------------------------------------------------------------\n");
  break;
  case 3:
   printf("-----------------------------------------------------------------------------\n");
 printf("The Even numbers are :\n");
  for(i=1;i<=100;i++)
   {
    if(i%2==0)
     {
      printf("%d\t",i);
             sum_even=sum_even+i;
      count++;
     }
   }
  printf("\nThe sum of Even mumbers between 1 to 100 is =%d",sum_even);
  printf("\nTottal numbers = %d\n",count);
 printf("-----------------------------------------------------------------------------\n");

  printf("-----------------------------------------------------------------------------\n");
 printf("The Odd numbers are :\n");
  for(i=1;i<=100;i++)
   {
    if(i%2==1)
     {
      printf("%d\t",i);
             sum_odd=sum_odd+i;
      count++;
     }
   }
  printf("\nThe sum of Odd mumbers between 1 to 100 is =%d",sum_odd);
  printf("\nTottal numbers = %d\n",count);
  printf("-----------------------------------------------------------------------------\n");
  sum=sum_odd+sum_even;
         printf("The sum of Even and Odd numbers: %d\n",sum);
  printf("-----------------------------------------------------------------------------\n");
  break;
 
  default:
  printf("Your choice is not there.");
  
 }
}
span style='color:#406080; '
Solution in C++:


#include<iostream.h>
void main()
{
 int n,i,count=0,sum_even=0,sum_odd=0,sum;
 cout<<"1.To print the Even numbers of 1 to 100 .\n";
 cout<<"2.To print the Odd numbers of 1 to 100 .\n";
 cout<<"3.To print The Even and Odd separate.\n";
 cout<<"Enter your choice (1 or 2 or 3).  ";
 cin>>n;
 switch(n)
 {
  case 1:
 cout<<"-----------------------------------------------------------------------------\n";
 cout<<"The Even numbers are :\n";
  for(i=1;i<=100;i++)
   {
    if(i%2==0)
     {
      cout<<i<<"\t";
             sum_even=sum_even+i;
      count++;
     }
   }
  cout<<"\nThe sum of Even mumbers between 1 to 100 is ="<<sum_even;
  cout<<"\nTottal numbers = "<<count<<endl;
 cout<<"-----------------------------------------------------------------------------\n";
  break;
  case 2:
 cout<<"-----------------------------------------------------------------------------\n";
 cout<<"The Odd numbers are :\n";
  for(i=1;i<=100;i++)
   {
    if(i%2==1)
     {
      cout<<i<<"\t";
             sum_odd=sum_odd+i;
      count++;
     }
   }
  cout<<"\nThe sum of Odd mumbers between 1 to 100 is ="<<sum_odd;
  cout<<"\nTottal numbers = "<<count<<endl;
  cout<<"-----------------------------------------------------------------------------\n";
  break;
  case 3:
   cout<<"-----------------------------------------------------------------------------\n";
 cout<<"The Even numbers are :\n";
  for(i=1;i<=100;i++)
   {
    if(i%2==0)
     {
      cout<<i<<"\t";
             sum_even=sum_even+i;
      count++;
     }
   }
  cout<<"\nThe sum of Even mumbers between 1 to 100 is ="<<sum_even;
  cout<<"\nTottal numbers = "<<count<<endl;
 cout<<"-----------------------------------------------------------------------------\n";

  cout<<"-----------------------------------------------------------------------------\n";
 cout<<"The Odd numbers are :\n";
  for(i=1;i<=100;i++)
   {
    if(i%2==1)
     {
      cout<<i<<"\t";
             sum_odd=sum_odd+i;
      count++;
     }
   }
  cout<<"\nThe sum of Odd mumbers between 1 to 100 is ="<<sum_odd;
  cout<<"\nTottal numbers = "<<count<<endl;
  cout<<"-----------------------------------------------------------------------------\n";
  sum=sum_odd+sum_even;
         cout<<"The sum of Even and Odd numbers: "<<sum<<endl;
  cout<<"-----------------------------------------------------------------------------\n";
  break;
  
  default:
  cout<<"Your choice is not there.";
 }
}

Socializer Widget By Blogger Yard
SOCIALIZE IT →
FOLLOW US →
SHARE IT →