Walk Lightly on this PLANET and yet leave such a FOOTPRINT that cannot be erased for thousands of Years..!!!
Visit Codstech for Cyber Security related Posts !

Visitors

Showing posts with label C-If-else. Show all posts
Showing posts with label C-If-else. Show all posts

Wednesday, February 9, 2011

C7 : Even-Odd numbers Print/check/sum


Back to Home   &  C


A ))) //Program to print even numbers upto a given limit


Method -1
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int i=2,n,e;
printf("Enter the limit\n");
scanf("%d",&n);
printf("Even nos upto %d\n",n);
for(i=2;i<=n;i=i+2)
         {
printf("%d\n",i);
         }
getch();
}

------------------------------------
Output
Enter the limit (Enter your choice)
8
Even numbers upto 8 
2
4
6
8



Method -2

#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int i,n,
printf("Enter the limit\n");
scanf("%d",&n);
for(i=2;i<=n;i++)
     {
      if(i%2==0)
            {
             printf("%d\n",i)
            }
     }
getch();
}



------------------------------------
Output
Enter the limit (Enter your choice)
8
2

4
6
8

===============================================


B ))) //Check whether the given number is odd or even


#include<stdio.h>
#include<conio.h>
void main()
{
   clrscr();
   int i=1;
   printf("Enter the no\n");
   scanf("%d",&i);
   if(i%2==0)
      {
      printf("%d is even\n");
      }
   else
      {
      printf("%d is odd\n");
      }
   getch();
   }

------------------------------------------------------------------
Output
Enter the number
5
5 is odd

===============================================

C ))) ////Print the total number of odd or even within a given range(1 to 10)


#include<stdio.h>
#include<conio.h>
void main()
{
   clrscr();
   int i=1,o,e;
   printf("Enter the number\n");
   scanf("%d",&i);
   for(i=1;i<=10;i=i+1)
     {
      if(i%2==0)
         {
e=e+1;
         }
     else
         {
o=o+1;
         }
     }
   printf("Total no of even is %d,and odd is %d",e,o);
   getch();
}

------------------------------------------------------------------
Output
Enter the number
9
Total number of even is 4,and odd is 5
===============================================

Back to Home   &  C



C3 : Find largest from given numbers


Back to Home   &  C


A )))//To find largest from 3 numbers

#include<stdio.h>
#include<conio.h>
void main()
   {
   clrscr();
   float a,b,c;
   printf("Enter 3 numbers\n");
   scanf("%f%f%f",&a,&b,&c);
   if(a>b)
{
if(a>c)
              {
               printf("%f is large",a);
               }
else
              {
               printf("%f is large",c);
              }
  }
 else
{
if(b>c)
                  {
                   printf("%f is large",b);
                   }
else
                  {
                   printf("%f is large",c);
                   }
}
 getch();
 }
---------------------------------------------------------
Output 
Enter 3 numbers (Enter any 3 numbers as per your choice)
1
2
3
3 is large.
===============================================
B )))//To find largest from 2 numbers ( If-Else)



#include<stdio.h>
#include<conio.h>
void main()
      {
      clrscr();
     float a,b;
     printf("Enter 2 numbers\n");
     scanf("%f%f",&a,&b);
     if(a>b)
{
printf("Largest no is %f",a);
}
else
{
printf("Largest no is %f",b);
}
getch();
}

---------------------------------------------------------
Output 
Enter 2 numbers ( As per your choice)
5
8
Largest number is 8
===============================================
C )))//To find largest from n numbers 


(Homework...:) )


Back to Home   &  C