C Prog

This is a discussion on C Prog within the C Programming forums, part of the General Programming Boards category; Pz help..! How can we get a result such 2 if the user enters 1 , 4 if 2, 6 ...

  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    1

    Unhappy C Prog

    Pz help..!

    How can we get a result such 2 if the user enters 1 , 4 if 2, 6 if 3......
    without using +,-,/,%,*,pow(),sqrt(),^ these operators..?

    Wat is the logic?

    Plz help me with this.

  2. #2
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Bitwise?

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    399

  4. #4
    Registered User rpbear's Avatar
    Join Date
    Nov 2009
    Posts
    18

    show a example

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    
    int
    main(){
    	
    	int i = 0x1;
    	int n = 0,input = 0,output = 0;
    	scanf("%d",&n);
    	input = n;
    	while(n--)
    		output = input<<1;
    	
    	printf("%d\n",output);
    	return EXIT_SUCCESS;
    }

  5. #5
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Fair enough, I though this was most certainly homework, and if you get a question like this you must have at some point gone over bitwise operators and bitshifts. I don't see how you can give anything else than hint without giving the whole answer away. It's no special logic involved.

    You can do this without the while loop and only one variable btw,

    Code:
    scanf("%d", &n);
    printf("%d\n", n<<1);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Assembly amature question: prog errors
    By geek@02 in forum Tech Board
    Replies: 1
    Last Post: 10-03-2008, 01:35 PM
  2. YangHui triangle print test prog?
    By toysoldier in forum C++ Programming
    Replies: 8
    Last Post: 08-20-2004, 08:46 AM
  3. Replies: 3
    Last Post: 06-29-2003, 05:50 PM
  4. Idea: Elevator Prog
    By gamer4life687 in forum Contests Board
    Replies: 0
    Last Post: 11-15-2002, 09:01 PM
  5. password prog
    By ihsir in forum A Brief History of Cprogramming.com
    Replies: 10
    Last Post: 01-06-2002, 05:39 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21