The CE Coding Challange

Yeah, it's time to code, here give your tricky questions on Programming, and we will all try to do that.

This would improve your thinking ability and refine your logic.

First we would have some simple questions and then move to more tricky ones.

I would like to roll the dice first.

Q1. Write a Program in C/C++ to print a statement ( say "Hello World") without using a semicolon (in the entire program).

Q2. Write a Program to add two numbers without using "+" .

Q3. Write a Program to multiply two numbers without using "*" .


Now that was a bit kiddish, don't worry, now let's face some questions which are more tricky

-> Write a Program that print it's own source code ie it would then recompile and run itself !

PS :- Though you may find solutions to the above questions googling them, but it would ruin the craziness .

This thread is created so that everyone can learn and improve their programming.


And keep adding more questions to this thread..!!

_______________________________________________________________________
Edit : While answering the questions, please explain the logic also, it would help others in understanding what you have done and others can also learn more stuff.

Replies

Welcome, guest

Join CrazyEngineers to reply, ask questions, and participate in conversations.

CrazyEngineers powered by Jatra Community Platform

  • avii

    @avii-TGGs8o Sep 6, 2013

    Please explain me how does printing Hello World without semi colon improves my logic ?

  • Sanyam Khurana

    @sanyam-Nl7Zqc Sep 7, 2013

    aviiPlease explain me how does printing Hello World without semi colon improves my logic ?

    Is not just about printing hello world but its more than that

    of course for printing something without semi colon you have to think differently which would improve your logic

    try all the questions and choose them

    lets see who does that and if these questions seems kiddish to you don't worry we will move to more complex questions soon.......

  • Vishal Sharma

    @vishal-pysGmK Sep 7, 2013

    -> Write a Program that print it's own source code ie it would then recompile and run itself !

    I think the program will run unconditionally and infinitely if it recompiles itself and runs the exe generated.
    Here's my code there is some problem with process leaving the control of Untitled1.exe and go to recompiled.exe but it works fine.
    (Assuming that the gcc compiler already has a path in variables)


    #include<stdlib.h>
    #include<stdio.h>
    #include<windows.h>
    int main() {
        printf("source code, recompile, display output\n\n");
        system("type Untitled1.cpp");
       
        Sleep(500);
       
        system("g++ -o recompiled Untitled1.cpp");
           
        Sleep(1000);              // stabilizing time
        system("recompiled.exe");
        return 0;
    }
  • Vishal Sharma

    @vishal-pysGmK Sep 7, 2013

    Q1. Write a Program in C/C++ to print a statement ( say "Hello World") without using a semicolon.
    #include<stdio.h>
    int main()
    {
        if(printf("I don't have semi colon"))
        {
              fflush(stdin);
              getchar();
        }   
    }
  • Vishal Sharma

    @vishal-pysGmK Sep 7, 2013

    Q3. Write a Program to multiply two numbers without using "*" .
    #include<stdio.h>
    void multi(int,int);
    int main()
    {
        int a = 40,b = 30;
        if(a>b)
            multi(a,b);
        else
            multi(b,a);
    }
    
    void multi(int greater, int smaller) {
        int ans = 0;
        while(smaller != 0) {
            ans += greater;
            smaller--;
        }
        printf("%d",ans);
    }
  • Vishal Sharma

    @vishal-pysGmK Sep 7, 2013

    Q2. Write a Program to add two numbers without using "+" .


    #include<stdio.h>
    void multi(int,int);
    int main()
    {
        int a = 32,b = 15;
        while( b != 0) {
            int c = a & b;
            a = a ^ b;
            b = c << 1;
        }
        printf("%d",a);
    }
    
  • Vishal Sharma

    @vishal-pysGmK Sep 7, 2013

    Adding another question here!!


    write a program to swap all the occurrences of 0 to the end of the array!
    ex:
    input -> 1,0,2,0,3,0,4
    output -> 1,2,3,4,0,0,0

  • Sanyam Khurana

    @sanyam-Nl7Zqc Sep 8, 2013

    Vishal0203
    #include<stdio.h>
    int main()
    {
        if(printf("I don't have semi colon"))
        {
              fflush(stdin);
              getchar();
        }  
    }


    Actually your code shouldn't use any semi colon.
    So, it would be something like this..

    #include<stdio.h>
    int main()
    {
        if(printf("I don't have semi colon"))
        {
        }  
    }



    Your logic is right, just there wouldn't be any code in between the if statement.

    I would also like to request you to edit your above posts and explain the logic you used too, it would help many newbies, and other people.

  • Vishal Sharma

    @vishal-pysGmK Sep 8, 2013

    Sanyam KhuranaActually your code shouldn't use any semi colon.
    So, it would be something like this..

    #include<stdio.h>
    int main()
    {
        if(printf("I don't have semi colon"))
        {
        }
    }


    Your logic is right, just there wouldn't be any code in between the if statement.

    I would also like to request you to edit your above posts and explain the logic you used too, it would help many newbies, and other people.


    I printed the statement without using semi colon. I don't think it needs to be pointed out
    the code i've written inside if block is just to make it stay
    anyway,
    another way to do that (it satisfies your rules)


    #include<stdio.h>
    int main()
    {
        switch(printf(" hello mere doston :P "))
        {
        }
    }
  • Sanyam Khurana

    @sanyam-Nl7Zqc Sep 8, 2013

    Vishal0203Adding another question here!!


    write a program to swap all the occurrences of 0 to the end of the array!
    ex:
    input -> 1,0,2,0,3,0,4
    output -> 1,2,3,4,0,0,0

    I have designed this code for 10 elements and would work as given.

    #include<iostream.h>
    #include<conio.h>
    void main()
    {
    int a[10],i,j,b[10],count;
    clrscr();
    for(i=0;i<10;i++)
    {
    cin>>a[i];
    }
    for(i=0,j=0;i<10;i++)
    {
    
        if(a[i]!=0)
        {
          b[j]=a[i];
          j++;
        }
        else
        count++;
    }
    for(i=j;i<10;i++)
    b[i]=0;
    for(i=0;i<10;i++)
    {
    cout<<b[i];
    }
    getch();
    }
  • Vishal Sharma

    @vishal-pysGmK Sep 8, 2013

    Sanyam KhuranaI have designed this code for 10 elements and would work as given.

    #include<iostream.h>
    #include<conio.h>
    void main()
    {
    int a[10],i,j,b[10],count;
    clrscr();
    for(i=0;i<10;i++)
    {
    cin>>a[i];
    }
    for(i=0,j=0;i<10;i++)
    {
    
        if(a[i]!=0)
        {
          b[j]=a[i];
          j++;
        }
        else
        count++;
    }
    for(i=j;i<10;i++)
    b[i]=0;
    for(i=0;i<10;i++)
    {
    cout<<b[i];
    }
    getch();
    }

    You need to swap the values instead of taking a separate array!
    I mean the changes are to be made within the array which is used to take input from user

  • Sanyam Khurana

    @sanyam-Nl7Zqc Sep 8, 2013

    Vishal0203I printed the statement without using semi colon. I don't think it needs to be pointed out

    Sorry, the question was actually that you don't have to use semi colon anywhere in the entire program. 😀

    PS:- I know I forgot to mention it clearly that you can't use the semicolon in the entire program , no problem, I'm going to edit that ...!!!

    Sorry for that, but please explain all your programs too...

    Vishal0203You need to swap the values instead of taking a separate array!
    I mean the changes are to be made within the array which is used to take input from user

    Ok, will work on it..

  • Vishal Sharma

    @vishal-pysGmK Sep 8, 2013

    Sorry for that, but please explain all your programs too...

    2 of my programs are self explanatory, i'll add tags to my addition program

  • Sanyam Khurana

    @sanyam-Nl7Zqc Sep 8, 2013

    Vishal0203You need to swap the values instead of taking a separate array!
    I mean the changes are to be made within the array which is used to take input from user

    Ok, will work on it...

    Vishal0203
    #include<stdio.h>
    void multi(int,int);
    int main()
    {
        int a = 32,b = 15;
        while( b != 0) {
            int c = a & b;
            a = a ^ b;
            b = c << 1;
        }
        printf("%d",a);
    }
    


    Can you explain this program, I'm not getting what you have done here....


    basically these three lines of code is what I need to understand..

    I have never seen this before 😔

     int c = a & b;
            a = a ^ b;
            b = c << 1;

    Thanks in advance..!!

  • Vishal Sharma

    @vishal-pysGmK Sep 8, 2013

    Sanyam KhuranaOk, will work on it...




    Can you explain this program, I'm not getting what you have done here....


    basically these three lines of code is what I need to understand..

    I have never seen this before 😔

     int c = a & b;
            a = a ^ b;
            b = c << 1;
    Thanks in advance..!!

    I am just operating on the bits... I'll explain this in detail!

    I'll take a few small numbers to give an explanation..
    Say, a = 9 (1001 in binary) and b = 3 (0011 in binary)
    the variable c here indicates the carry. if I perform AND operation on a & b
    i.e. c = a & b;
    it executes like this,
    1 0 0 1
    0 0 1 1
    """"""""""
    0 0 0 1 (performing AND operation according to truth table)
    hence the carry i.e. c = 1 (0001 in binary)

    the next step is a = a ^ b where we are performing XOR operation on bits to calculating sum without adding the carry
    i.e.
    1 0 0 1
    0 0 1 1
    """"""""""
    1 0 1 0 (performing XOR operation according to #-Link-Snipped-#)
    hence value of a = 10 (1010 in binary)

    the next step is b = c << 1 (left shift of c)
    as we saw above c = 1 (0001) after left shift it becomes (0010) viz stored in b

    so at 1st iteration a = 10 , b = 2 , c = 1

    since it is in loop, we perform the 1st operation again i.e. c = a & b
    at this step, the value of a = 1 0 1 0 and b = 0 0 1 0
    so c becomes
    1 0 1 0
    0 0 1 0
    """"""""""
    0 0 1 0
    hence c = 2 (0010 in binary)
    the next step is a = a ^ b
    i.e.
    1 0 1 0
    0 0 1 0
    """"""""""
    1 0 0 0
    therefore, a = 8 (1000 in binary)
    next step b = c << 1 i.e. b = 0 1 0 0

    so at 2nd iteration a = 8 , b = 4 , c = 2
    again in next iteration, c = a & b
    1 0 0 0
    0 1 0 0
    """"""""""
    0 0 0 0
    i.e. c = 0

    next step, a = a ^ b
    1 0 0 0
    0 1 0 0
    """"""""""
    1 1 0 0
    i.e a = 12

    next step, b = c << 1;
    i.e. b = 0;
    since b = 0, loop breaks and value of a i.e. 12 is printed and that's the answer 😀

  • Vishal Sharma

    @vishal-pysGmK Sep 8, 2013

    @#-Link-Snipped-# take time to read it and keep a paper and pen with you 😉

  • simplycoder

    @simplycoder-NsBEdD Sep 8, 2013

    Sanyam KhuranaOk, will work on it...

    Can you explain this program, I'm not getting what you have done here....


    basically these three lines of code is what I need to understand..

    I have never seen this before 😔

     int c = a & b;
            a = a ^ b;
            b = c << 1;
    Thanks in advance..!!


    Basically this is the same as full adder. where c is the carry.



    Vishal0203Adding another question here!!


    write a program to swap all the occurrences of 0 to the end of the array!
    ex:
    input -> 1,0,2,0,3,0,4
    output -> 1,2,3,4,0,0,0

    I didn't find time to write a code, but here is an algorithm which I would use.
    Keep an counter of non-zero numbers(n), this would either be less or equal to numbers entered(m).
    print all the numbers the way they are and then print (m-n) zeroes.

  • Vishal Sharma

    @vishal-pysGmK Sep 8, 2013

    I didn't find time to write a code, but here is an algorithm which I would use.
    treat 0 as highest number(replace 0 with infinity.. (if its an int array, treat 0 as 4294967295 and so).

    Sort in descending order and once we know the correct place, replace infinity by 0

    I think we can sort it normally!?

  • simplycoder

    @simplycoder-NsBEdD Sep 8, 2013

    Vishal0203I think we can sort it normally!?

    My bad, I have edited my previous post, your problem never said sorting, it was my wrong assumption, refer to above edited post.

  • Vishal Sharma

    @vishal-pysGmK Sep 8, 2013

    simplycoderMy bad, I have edited my previous post, your problem never said sorting, it was my wrong assumption, refer to above edited post.

    hehe but i gave an example 😛 anyway lets see if anyone tries your algorithm!

  • Sanyam Khurana

    @sanyam-Nl7Zqc Sep 8, 2013

    Ok, it's done..!

    😛

    Here it is, just removed the second array and used first array itself..

    #include<iostream.h>
    #include<conio.h>
    void main()
    {
    int a[10],i,j,count;
    clrscr();
    for(i=0;i<10;i++)
    {
    cin>>a[i];
    }
    for(i=0,j=0;i<10;i++)
    {
    
        if(a[i]!=0)
        {
          a[j]=a[i];
          j++;
        }
        else
        count++;
    }
    for(i=j;i<10;i++)
    a[i]=0;
    for(i=0;i<10;i++)
    {
    cout<<a[i];
    }
    getch();
    }
  • Sanyam Khurana

    @sanyam-Nl7Zqc Sep 8, 2013

    I think, count isn't required in this case, so it can be ignored and the else part too..

    The code would work fine without these also...

  • Sanyam Khurana

    @sanyam-Nl7Zqc Sep 8, 2013

    Ok, tell the output of this code

    and reason too for your answer

    #include<stdio.h>
    #include<conio.h>
    void main()
    {
    int a=-29, b=-3;
    printf('%d",a%b);
    getch();
    }

    I know, vishal and crazycoder would immediately answer this.. 😛

  • Vishal Sharma

    @vishal-pysGmK Sep 8, 2013

    Sanyam KhuranaOk, it's done..!

    😛

    Here it is, just removed the second array and used first array itself..

    #include<iostream.h>
    #include<conio.h>
    void main()
    {
    int a[10],i,j,count;
    clrscr();
    for(i=0;i<10;i++)
    {
    cin>>a[i];
    }
    for(i=0,j=0;i<10;i++)
    {
    
        if(a[i]!=0)
        {
          a[j]=a[i];
          j++;
        }
        else
        count++;
    }
    for(i=j;i<10;i++)
    a[i]=0;
    for(i=0;i<10;i++)
    {
    cout<<a[i];
    }
    getch();
    }

    I'm sorry but you need to swap...

  • simplycoder

    @simplycoder-NsBEdD Sep 8, 2013

    @#-Link-Snipped-#

    #include<iostream>
    
    using namespace std;
    #define total_count 10
    int arr_numbers[total_count];
    
    int main(int argc,char**argv)
    {
        int non_zero_count=0; // Count non-zero numbers.
        int index;
       
        // Take the input.
        for(index=0; index<total_count;index++)
        {
            cin>>arr_numbers[index];
           
        }
       
        // Print only the nonzero number.
        for(index=0; index<total_count; index++)
        {
            if(arr_numbers[index])
            {
                cout<<arr_numbers[index];
                non_zero_count++;
            }
        }
       
        // Print the 0s
        for(index=0; index<total_count-non_zero_count; index++)
        {
            cout<<"0";
        }
    
        // Program end.
        cout<<endl;
        return 0;
    }
    
    
  • Vishal Sharma

    @vishal-pysGmK Sep 8, 2013

    Sanyam KhuranaOk, tell the output of this code

    and reason too for your answer

    #include<stdio.h>
    #include<conio.h>
    void main()
    {
    int a=-29, b=-3;
    printf('%d",a%b);
    getch();
    }
    I know, vishal and crazycoder would immediately answer this.. 😛

    So lets wait for others to answer this! 😉

  • Vishal Sharma

    @vishal-pysGmK Sep 8, 2013

    simplycoder@#-Link-Snipped-#
    #include<iostream>
    
    using namespace std;
    #define total_count 10
    int arr_numbers[total_count];
    
    int main(int argc,char**argv)
    {
        int non_zero_count=0; // Count non-zero numbers.
        int index;
      
        // Take the input.
        for(index=0; index<total_count;index++)
        {
            cin>>arr_numbers[index];
          
        }
      
        // Print only the nonzero number.
        for(index=0; index<total_count; index++)
        {
            if(arr_numbers[index])
            {
                cout<<arr_numbers[index];
                non_zero_count++;
            }
        }
      
        // Print the 0s
        for(index=0; index<total_count-non_zero_count; index++)
        {
            cout<<"0";
        }
    
        // Program end.
        cout<<endl;
        return 0;
    }
    
    

    umm... It gives the output but doesn't change the array... The question says "swap" , means the entire array will be modified
    ex:
    if array is
    1 0 2 0 3 0 4 0 5 0
    then after performing the sort
    the array will become
    1 2 3 4 5 0 0 0 0 0

  • Sanyam Khurana

    @sanyam-Nl7Zqc Sep 8, 2013

    Vishal0203I'm sorry but you need to swap...

    oops..

    Sorry, my bad..

    just forgot that it says swap..

    but what's the use of sort here..?

    Why do we have to sort..?


    Can't we just swap the element which is equal to zero?😐

  • Vishal Sharma

    @vishal-pysGmK Sep 8, 2013

    Sanyam Khuranaoops..

    Sorry, my bad..

    just forgot that it says swap..

    but what's the use of sort here..?

    Why do we have to sort..?


    Can't we just swap the element which is equal to zero?😐

    means the same!
    actually changes need to be made in array itself and the final result of array will be the array itself after the sort

    if array is
    1 0 2 0 3 0 4 0 5 0
    then after performing the sort
    the array will become
    1 2 3 4 5 0 0 0 0 0

  • simplycoder

    @simplycoder-NsBEdD Sep 8, 2013

    Sanyam KhuranaOk, tell the output of this code

    and reason too for your answer

    #include<stdio.h>
    #include<conio.h>
    void main()
    {
    int a=-29, b=-3;
    printf('%d",a%b);
    getch();
    }
    I know, vishal and crazycoder would immediately answer this.. 😛

    Its pretty simple if we think this as math question.

    1) '/' operator would give quotient.
    2) '%' operator would give remainder.
    3) Dividend=Divisor*Quotient+remainder
    4) a=b*(a/b)+(a%b)
    Don't discard b's in b*(a/b)!!
    => a/b gives -29/-3 = 9
    b*(a/b) gives -3*9=-27
    hence a%b=-2.

  • avii

    @avii-TGGs8o Sep 8, 2013

    Sanyam KhuranaIs not just about printing hello world but its more than that

    of course for printing something without semi colon you have to think differently which would improve your logic

    try all the questions and choose them

    lets see who does that and if these questions seems kiddish to you don't worry we will move to more complex questions soon.......

    Did I say these are kiddish ?

    Printing hello world in C without semi colons just shows how good your knowledge in C. It does not improve your logic. If you are targeting to end up working in companies like Infosys, Wipro, continue doing these. These will help you clear the placements.

    But if you think beyond them, for companies like Microsoft, Google etc, then start solving actual problem which will just f*ck your brains out. Something like this :

    The prime 41, can be written as the sum of six consecutive primes:

    41 = 2 + 3 + 5 + 7 + 11 + 13
    This is the longest sum of consecutive primes that adds to a prime below one-hundred.

    The longest sum of consecutive primes below one-thousand that adds to a prime, contains 21 terms, and is equal to 953.

    Which prime, below one-million, can be written as the sum of the most consecutive primes?

    This is 50th problem from project euler.

    Just today I finished interviewing bunch of people who probably were same like you guys. I am like, man, I don't give two f*cks about whether you do it in C or Java, I just want to know how you do it. They happily answer how to swap two numbers without using temp variable and all that stuff. Fact is, I don't care. And good companies or start ups don't care.

    Don't worry about languages. Think in terms of algorithms. Stop solving 'Tricky C questions' etc. Start doing, 'tricky questions'. Which are agonistic to any language. Your solution shouldn't be dependent on language.

    Anyways, these are my two cents. You may absolutely ignore my advice. Best of luck.

  • Abhishek Rawal

    @abhishek-fg9tRh Sep 8, 2013

    See, Knowledge of language is equally important as logic. Many people in world are good at logic, but they can't program.
    How would be the situation if a man can solve most trickiest query on paper, but couldn't program the shit ?
    All I mean is, both are equally important.

    And BTW, no need to flame on eachother, guys. Calm down.
    Don't like the thread ? Ignore it & move further. There is no need to give free suggestions to anyone until it's asked.
    Peace!

  • Vishal Sharma

    @vishal-pysGmK Sep 8, 2013

    The prime 41, can be written as the sum of six consecutive primes:

    41 = 2 + 3 + 5 + 7 + 11 + 13
    This is the longest sum of consecutive primes that adds to a prime below one-hundred.

    The longest sum of consecutive primes below one-thousand that adds to a prime, contains 21 terms, and is equal to 953.

    Which prime, below one-million, can be written as the sum of the most consecutive primes?

    the prime number is 9384209 ?????

    can anyone confirm this answer??

  • Abhishek Rawal

    @abhishek-fg9tRh Sep 8, 2013

    Nope. Because 9,384,209 > 1,000,000.

  • Vishal Sharma

    @vishal-pysGmK Sep 8, 2013

    Abhishek RawalNope. Because 9,384,209 > 1,000,000.

    oh shit i took it for 10,000,000 😛 my bad

    then is it 958577 ???

  • Sanyam Khurana

    @sanyam-Nl7Zqc Sep 8, 2013

    aviiDid I say these are kiddish ?

    Printing hello world in C without semi colons just shows how good your knowledge in C. It does not improve your logic. If you are targeting to end up working in companies like Infosys, Wipro, continue doing these. These will help you clear the placements.

    But if you think beyond them, for companies like Microsoft, Google etc, then start solving actual problem which will just f*ck your brains out. Something like this :



    This is 50th problem from project euler.

    Just today I finished interviewing bunch of people who probably were same like you guys. I am like, man, I don't give two f*cks about whether you do it in C or Java, I just want to know how you do it. They happily answer how to swap two numbers without using temp variable and all that stuff. Fact is, I don't care. And good companies or start ups don't care.

    Don't worry about languages. Think in terms of algorithms. Stop solving 'Tricky C questions' etc. Start doing, 'tricky questions'. Which are agonistic to any language. Your solution shouldn't be dependent on language.

    Anyways, these are my two cents. You may absolutely ignore my advice. Best of luck.

    What I wanted to say is, I'm not a good programmer, not I'm a pro at forming algorithms, but we all have to start somewhere. So, I started this thread (as a learning opportunity), so that people like me, could grab the basic things first. I can't really code a bigger thing in one shot, if I'm even not clear with my basics.

    Sorry, if you got offended.

  • Vishal Sharma

    @vishal-pysGmK Sep 8, 2013

    Sanyam KhuranaWhat I wanted to say is, I'm not a good programmer, not I'm a pro at forming algorithms, but we all have to start somewhere. So, I started this thread (as a learning opportunity), so that people like me, could grab the basic things first. I can't really code a bigger thing in one shot, if I'm even not clear with my basics.

    Sorry, if you got offended.

    it's okay guys forget about it!
    and please confirm the answer i got? 😲

  • Vishal Sharma

    @vishal-pysGmK Sep 8, 2013

    Vishal0203Adding another question here!!


    write a program to swap all the occurrences of 0 to the end of the array!
    ex:
    input -> 1,0,2,0,3,0,4
    output -> 1,2,3,4,0,0,0

    Answer to the above question!!


    #include<stdio.h>
    int n,a[15];
    void sorter();
    int main()
    {
        printf("Enter the no. : ");
        scanf("%d",&n);
        printf("Enter the numbers : ");
        for(int i = 0 ; i < n ; i++) {
          scanf("%d",&a[i]);
        }
        sorter();
        printf("sorted! \n");
        for(int i = 0 ; i < n ; i++) {
            printf("%d",a[i]);
        }
        fflush(stdin);
        getchar();
        return 0;
    }
    
    void sorter() {
        for(int i = 0 ; i < n ; i++) {
        //swapping only when 2 adjacent elements are not zero
            if(a[i] == 0 && a[i+1] != 0) {    
                  int temp = a[i];
                  a[i] = a[i+1];
                  a[i+1] = temp;
                  if(i == 0)
                      i -= 1;
                  else
                      i -= 2;     // a proper decrement of index to reduce the no of comp
            }
        //if 2 adjacent elements are zero, toggle through array until two adj elements 
        // are non-zero
            if(a[i] == 0 && a[i+1] == 0) {
                continue;
            }
      }
    }
  • Sanyam Khurana

    @sanyam-Nl7Zqc Sep 8, 2013

    Here's one more..

    Write a Program to print all Pythagorean Triplets that occur between 100 and 900.

    PS: Pythagorean Triplets are the numbers which satisfy Pythagoras Theorem, Example is 3,4,5.

  • Vishal Sharma

    @vishal-pysGmK Sep 8, 2013

    Sanyam KhuranaHere's one more..

    Write a Program to print all Pythagorean Triplets that occur between 100 and 900.

    PS: Pythagorean Triplets are the numbers which satisfy Pythagoras Theorem, Example is 3,4,5.

    Do you mean all a,b,c values above 100 and below 900 ?

  • Sanyam Khurana

    @sanyam-Nl7Zqc Sep 8, 2013

    Vishal0203Do you mean all a,b,c values above 100 and below 900 ?

    Yes, we want Pythagorean Triplets..

    Which means you have to print all the three values between 100 to 900 ...

  • Vishal Sharma

    @vishal-pysGmK Sep 9, 2013

    Sanyam KhuranaYes, we want Pythagorean Triplets..

    Which means you have to print all the three values between 100 to 900 ...

    I dont think we have any triplet with all 3 numbers between 100 to 900
    any way here's my code that gives you triplets up to 900


    #include<stdio.h>
    #include<stdlib.h>
    int main() {
        int a,b,c;
        for(int i = 1 ; i <= 200 ; i++) {
            int m = i;
            int n = i+1;
            a= n*n - m*m;
            b = 2*m*n;
            c = m*m + n*n;
            if(c > 900)
                break;
            printf("%d, %d, %d\n",a,b,c);
        }
        return 0;
    }
  • Sanyam Khurana

    @sanyam-Nl7Zqc Sep 9, 2013

    Find the product of Pythagorean Triplet whose sum is 1000.
    It's just unique.

    PS: I just finished doing this one..!

  • Sanyam Khurana

    @sanyam-Nl7Zqc Sep 9, 2013

    Sanyam KhuranaQ3. Write a Program to multiply two numbers without using "*" .

    What I did for this one is

    #include<iostream.h>
    #include<conio.h>
    void main()
    {
    int a,b,c;
    clrscr();
    cout<<"Enter two numbers";
    cin>>a>>b;
    for(i=0;i<b;i++)             //Edited...
    {
    c+=a;
    }
    cout<<c;
    getch();
    }
  • Jeffrey Arulraj

    @jeffrey-xA7lUP Sep 9, 2013

    @#-Link-Snipped-#


    #include<iostream.h>
    #include<conio.h>
    void main()
    {
    int a,b,c;
    clrscr();
    cout<<"Enter two numbers";
    cin>>a>>b;
    for(i=0;i<b;i++)    // Condition has to be this
    {
    c+=a;
    }
    cout<<c;
    getch();
    }

    Am I not right

    Cos it has to be added from 0 to N-1 or from 1 to N

  • Sanyam Khurana

    @sanyam-Nl7Zqc Sep 9, 2013

    Jeffrey Samuel@#-Link-Snipped-#


    #include<iostream.h>
    #include<conio.h>
    void main()
    {
    int a,b,c;
    clrscr();
    cout<<"Enter two numbers";
    cin>>a>>b;
    for(i=0;i<b;i++)    // Condition has to be this
    {
    c+=a;
    }
    cout<<c;
    getch();
    }
    Am I not right

    Cos it has to be added from 0 to N-1 or from 1 to N

    Yup you are right ...

    This time didn't copied code, but just wrote it, and by mistake I wrote that equality sign.

    If we do add equality sign, then the loop will run one more time, so if we are multiplying 5*3, it would give 5*4.

  • Sanyam Khurana

    @sanyam-Nl7Zqc Sep 9, 2013

    Adding one more...

    Although I have done this, but my code was inefficient if the value is large, I Want to know a better way of doing this..(can't find a way to have a loop)

    Ok, question is..

    Find the smallest positive number which is divisible by numbers from 1 to 20.

  • Sanyam Khurana

    @sanyam-Nl7Zqc Sep 9, 2013

    Vishal0203I dont think we have any triplet with all 3 numbers between 100 to 900
    any way here's my code that gives you triplets up to 900


    #include<stdio.h>
    #include<stdlib.h>
    int main() {
        int a,b,c;
        for(int i = 1 ; i <= 200 ; i++) {
            int m = i;
            int n = i+1;
            a= n*n - m*m;
            b = 2*m*n;
            c = m*m + n*n;
            if(c > 900)
                break;
            printf("%d, %d, %d\n",a,b,c);
        }
        return 0;
    }

    It is printing triplets even like 3,4,5 and 5,12,13...

    We wanted from 100 to 900...

  • rahul69

    @rahul69-97fAOs Sep 9, 2013

    Sanyam KhuranaHere's one more..

    Write a Program to print all Pythagorean Triplets that occur between 100 and 900.

    PS: Pythagorean Triplets are the numbers which satisfy Pythagoras Theorem, Example is 3,4,5.
    int checkint(int f)
    {
    for(int i=100;i<900;i++)
    if((i*i)==f)
    return i;
    return 0;
    }
    main()
    {
      int a,b,c;
      int f,sq;  
    for(int i=100;i<700;i++)
    {
      a=i;
              for(int j=i+1;j<700;j++)
              {
                    b=j;
                    f= ((a*a)+(b*b));
                      sq=checkint(f);
                      if(sq)
                        c=sq;
                      else
                        c=999;
                      if(c<900)
                        cout<<"\n"<<a<<"\t"<<b<<"\t"<<c;
                          
              }
      }
    }
  • rahul69

    @rahul69-97fAOs Sep 9, 2013

    Sanyam KhuranaFind the product of Pythagorean Triplet whose sum is 1000.
    It's just unique.

    PS: I just finished doing this one..!

    31875000?