wap to print all odd and even numbers from 1 to n , and also print sum of all even and odd numbers by using for loop .

 





#include<stdio.h>
#include<conio.h>
void main()
{
   int i,odd,even,n,sum1=0,sum2=0;
   
   printf("enter the value of n :");
   scanf("%d",&n);
   

   printf("\neven no. are\n ");
   for(i=0;i<=n;i++)
   {
     if(i%2==0)
    {
     
     sum1=sum1+i;
     printf(" %d",i);
    }
   }
     printf("\nsum of all even no. is %d",sum1);
    
  
    
   printf("\n\nodd no. are \n");
   for(i=1;i<=n;i++)
   {
     if(i%2!=0)
    {
    
     sum2=sum2+i;
      printf(" %d",i);
    }
   }
     printf("\nsum of all odd no. is %d",sum2);


}

output :
enter the value of n :15 even no. are 0 2 4 6 8 10 12 14 sum of all even no. is 56 odd no. are 1 3 5 7 9 11 13 15 sum of all odd no. is 64

Comments

Popular Post

Create a base class called shape .Use this class to store two double type values that could be used to compute the area of figures, Derive two specific classes called triangle and rectangle from the base shape .Add to the base class, a member function get_data() to initialise base class data members and another member function display_area() to compute and display the area of figures. Make display_area () as a virtual function and redefine this function in the derived class to suit their requirements. Using these three classes, design a program that will accept dimensions of a triangle or a rectangle interactively and display the area. Area of rectangle = x*y Area of triangle = ½*x*y