CrazyEngineers
  • Shortest C++ code to print odd numbers between 1 and 100

    Manish Goyal

    Member

    Updated: Oct 26, 2024
    Views: 1.6K
    write the shortest code you can write to print all the odd no from 1 to 100?


    Note:- Don't use google please
    0
    Replies
Howdy guest!
Dear guest, you must be logged-in to participate on CrazyEngineers. We would love to have you as a member of our community. Consider creating an account or login.
Replies
  • gaurav.bhorkar

    MemberFeb 28, 2011

    main( )
    {
    for (int i = 1; i <= 100; i+=2)
    cout << i;
    }

    I didn't execute it, by the way.
    Are you sure? This action cannot be undone.
    Cancel
  • Manish Goyal

    MemberFeb 28, 2011

    can you think something different

    i mean not using this and any modulus operator?
    Are you sure? This action cannot be undone.
    Cancel
  • Reya

    MemberFeb 28, 2011

    main()
    {
    for(int i=1;i<=100;i++)
    {
    if(i%2!=0)
    cout<<i;
    }}
    Are you sure? This action cannot be undone.
    Cancel
  • Hussanal Faroke

    MemberFeb 28, 2011

    main()
    {
    pirntf("13579111315........99");
    }
    Are you sure? This action cannot be undone.
    Cancel
  • Manish Goyal

    MemberFeb 28, 2011

    Interesting
    Any other way friends
    Are you sure? This action cannot be undone.
    Cancel
  • gaurav.bhorkar

    MemberMar 1, 2011

    Hussanal Faroke
    main()
    {
    pirntf("13579111315........99");
    }
    Haha... but that will hurt your fingers 😀
    Are you sure? This action cannot be undone.
    Cancel
  • rajesh_s

    MemberMar 1, 2011

    main()
    {
    for(i=1;i<100;i++)
    {
    for(j=2;j<i;j++)
    {
    if(i%j!=0)
    cout<<i;
    }
    }
    }
    Are you sure? This action cannot be undone.
    Cancel
  • PraveenKumar Purushothaman

    MemberMar 2, 2011

    Heard of Structures? Using that we can do in this way... 😀

    struct ZeroEven {
        unsigned num : 1;
    }
    
    int main(){
        ZeroEven ze;
        for (i=0; i<100; i++)
        {
            ze.num = i;
            if (ze.num != 0)
                cout<<i;
        }
        return 0;
    }
    Guys, do try this and say! 😀
    Are you sure? This action cannot be undone.
    Cancel
  • Manish Goyal

    MemberMar 3, 2011

    This is what i was looking for Nice logic buddy

    Any other way
    Are you sure? This action cannot be undone.
    Cancel
  • PraveenKumar Purushothaman

    MemberMar 3, 2011

    goyal420
    This is what i was looking for Nice logic buddy

    Any other way
    Thanx... 😀
    Are you sure? This action cannot be undone.
    Cancel
  • rajesh_s

    MemberMar 3, 2011

    is my logic not correct
    Are you sure? This action cannot be undone.
    Cancel
  • gaurav.bhorkar

    MemberMar 3, 2011

    praveenscience
    Heard of Structures? Using that we can do in this way... 😀

    struct ZeroEven {
        unsigned num : 1;
    }
    
    int main(){
        ZeroEven ze;
        for (i=0; i<100; i++)
        {
            ze.num = i;
            if (ze.num != 0)
                cout<<i;
        }
        return 0;
    }
    Guys, do try this and say! 😀
    Nice solution. I never thought of using bit fields.
    Are you sure? This action cannot be undone.
    Cancel
  • PraveenKumar Purushothaman

    MemberMar 4, 2011

    gaurav.bhorkar
    Nice solution. I never thought of using bit fields.
    He he 😛 Thanx... 😁
    Are you sure? This action cannot be undone.
    Cancel
  • Sreekar B V

    MemberSep 18, 2014

    main()
    {
    int i=1;
    while((i<100)&&(i+=2))
    printf("%d",i);
    }
    Are you sure? This action cannot be undone.
    Cancel
  • simplycoder

    MemberSep 28, 2014

    #include<stdio.h>
    //simplycoder
    
    int main(int argc,char**argv)
    {
       int n=1;
      while(n<100)
      {
         if(n & 1 && printf("%d",n));
         n++;
      }
    return 0;
    }
    
    Are you sure? This action cannot be undone.
    Cancel
  • Kaustubh Katdare

    AdministratorSep 28, 2014

    Without wanting to distract this discussion; I think the guys who used print got it all right. Someone gotta beat that!

    Or we need to rephrase the problem statement?
    Are you sure? This action cannot be undone.
    Cancel
  • Tim Rossiter

    MemberSep 29, 2014

    int main(int n)  //init n=-1
    {
         (n<99)?(printf("%d\n",main(n+2))):(n=n); //emote is colon left paren
         return(n);
    }
    Are you sure? This action cannot be undone.
    Cancel
  • Kaustubh Katdare

    AdministratorSep 29, 2014

    #-Link-Snipped-# - You may use [code] .... [/code] tags to enter code without having to deal with emoticons.
    Are you sure? This action cannot be undone.
    Cancel
  • Tim Rossiter

    MemberSep 29, 2014

    Thanks
    Are you sure? This action cannot be undone.
    Cancel
  • Anoop Kumar

    MemberSep 29, 2014

    I don't know C++ but here is java logic

    void printOddNumbers(int maxLimit) {
           for(int i=1; i <= maxLimit; i+=2){
                System.out.println("Odd : " +i);
        }
    }
    
    Number of iterations: maxLimit / 2 ;
    Are you sure? This action cannot be undone.
    Cancel
  • avii

    MemberOct 2, 2014

    well here's in Go, if that interests anyone:

    package main
    
    import "fmt"
    
    func main() {
        for i := 1; i < 100; i+=2 {
            fmt.Println(i)
        }
    }
    Are you sure? This action cannot be undone.
    Cancel
Home Channels Search Login Register