Thread: Logic for Binary Sequence N=16,32,64,128,256,512,1024,2048,5096 In C

  1. #1
    Registered User
    Join Date
    Mar 2017
    Posts
    52

    Logic for Binary Sequence N=16,32,64,128,256,512,1024,2048,5096 In C

    I need this code to be executed in C

    The below code is working
    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
        int i = 1;
        
    
        for(i=4;  i <= 16384;i*=2)
            cout << i << "  " << endl;    
        return 0;
    }
    but my requirement that

    Code:
    #include <iostream>
    {
    int N;
    unsigned result =1;
    unsigned char = 16384;
    for (int i=0;i<N;i++)
    {
    //Logic
    
    }
    if i add code (result*=2) , its showing wrong result.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > unsigned char = 16384;
    How many values do you think you can store in a char?

    Do you know any C, or do you just google things and type in random keywords to see what happens?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Mar 2017
    Posts
    52
    Hi,

    I have changed my code. I am getting result. Is it my logic is right ?



    Quote Originally Posted by Salem View Post
    > unsigned char = 16384;
    How many values do you think you can store in a char?

    Do you know any C, or do you just google things and type in random keywords to see what happens?
    Attached Images Attached Images Logic for Binary Sequence N=16,32,64,128,256,512,1024,2048,5096 In C-binaryseries-png 

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    I think you are trying to write C code; but, your code is mixed C and C++.

    I suggest finding a C Compiler if you are trying to write C code!

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  5. #5
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,106
    Several comments:

    Make sure your code is in a .c file, and make sure you are compiling with gcc.

    Now remove the following two lines:
    Code:
    #include <iostream>
    using namespace std;
    When posting code, please use "Code Tags".

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 05-10-2017, 09:46 AM
  2. fastest way to NOT a binary sequence in decimal form
    By tianshiz in forum C Programming
    Replies: 7
    Last Post: 09-13-2011, 03:43 PM
  3. Binary sequence in lexicographic order
    By confuser008 in forum C++ Programming
    Replies: 5
    Last Post: 10-16-2009, 01:10 PM
  4. Binary Sequence
    By royshh in forum C++ Programming
    Replies: 3
    Last Post: 03-31-2006, 02:56 PM
  5. Logic behind Binary Search?
    By aspand in forum C Programming
    Replies: 3
    Last Post: 05-21-2002, 10:46 AM

Tags for this Thread