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

Write the definition for a class called complex that has floating point data members for storing real and imaginary parts. The class has the following member functions: void set(float, float) to set the specified value in object void disp() to display complex number object complex sum(complex) to sum two complex numbers & return complex number 1. Write the definitions for each of the above member functions. 2. Write main function to create three complex number objects. Set the value in two objects and call sum() to calculate sum and assign it in third object. Display all complex numbers.