Results 1 to 4 of 4

Thread: [RESOLVED] Display the Even numbe

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Apr 2008
    Posts
    474

    Resolved [RESOLVED] Display the Even numbe

    Code:
    #include<stdio.h>
    #include<conio.h>
    void main()
    {
    clrscr();
    int i;
    for (i=1;i<=10;i++)
    {
    if (i%2==0)
    printf("\n%d",i "is even number");
    }
    getch();
    }
    I want to print all the Even numbers lying between 1 to 10. My program does that.
    But I want to display in the format:-
    2 is even number
    4 is even number
    and so on......

    Below LINE Do not Display the correct output:-
    Code:
    printf("\n%d",i "is even number");
    Please help me!!!!!

  2. #2
    PowerPoster Arnoutdv's Avatar
    Join Date
    Oct 2013
    Posts
    6,745

    Re: Display the Even numbe

    Shouldn't that be:
    Code:
    printf("\n%d is even number", i);
    http://www.cplusplus.com/reference/cstdio/printf/

  3. #3
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Display the Even numbe

    Yes. the printf in the first post is incorrect... Arnoutdv's code is correct.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  4. #4
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,598

    Re: Display the Even numbe

    Just for information,
    if ( (i&1) == 0) // i Anded with 1

    should be a much faster operation than using the Mod % operator, which requires an integer divide in most processors that I know of.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width