The CE Coding Challange

Sanyam Khurana
@sanyam-Nl7Zqc โ€ข Oct 26, 2024

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

  • Sanyam Khurana

    @sanyam-Nl7Zqc • Sep 9, 2013

    rahul6931875000?

    Yes, that's correct..๐Ÿ‘

    and the triplets are

    200, 375 and 425...

  • Sanyam Khurana

    @sanyam-Nl7Zqc • Sep 10, 2013

    Adding one more...

    Write a Program to first search the value of 2^1000 and then print the sum of it's digits.

    Eg. if we have to find value of 2^5= 32 then sum of it's digit is 3+2 = 5 , so 5 is the answer.

    PS: I was designing the algorithm for this, but just got stuck on how to save the large value of 2^1000 as there's no data type which can store that much large number.

    If you can help me, please head to the thread to discuss more about this problem..

    #-Link-Snipped-#

  • Sanyam Khurana

    @sanyam-Nl7Zqc • Sep 11, 2013

    One more question..

    Write a Program to find the largest palindrome is a string.

    PS:- We need active participation, we are here to learn. Let's have an awesome discussion, learn and improve ourselves. ๐Ÿ‘

  • Jeffrey Arulraj

    @jeffrey-xA7lUP • Sep 11, 2013

    Sanyam KhuranaWrite a Program to find the largest palindrome is a string.

    Explain this mate

    Largest palindrome is a string or in a string

    A little confused here

  • Sanyam Khurana

    @sanyam-Nl7Zqc • Sep 11, 2013

    Jeffrey SamuelExplain this mate

    Largest palindrome is a string or in a string

    A little confused here

    If you are given a string, which would contain a palindrome inside it ..you need to spot the biggest palindrome..

    Eg. Let's say we have a string "ABCDEDCFGTU"
    here "DED" is a sub string which is a palindrome, but "CDEDC" is the biggest palindrome in the entire string, so we need to spot this and display it.

    Hope you got it..

  • Sanyam Khurana

    @sanyam-Nl7Zqc • Sep 11, 2013

    or consider this...

    A string like
    "ABCDEDCGFATTAFGLU"
    Here, "CDEDC" is there and "GFATTAFG" is also there, so the second one would be the right answer.

    If it happens that two strings are of same length then you have to display both of the palindromes.

  • Sanyam Khurana

    @sanyam-Nl7Zqc • Sep 12, 2013

    Here's another one to go...

    Write a Program to find largest and smallest from five numbers without using any array and loop, or conditional statements.

    PS: Keep adding more questions, and let's solve them..!! ๐Ÿ‘

  • Shailaja Tiwari

    @shailaja-tiwari-lKhGjd • Sep 15, 2013

    What value should be printed for x?
    #include <iostream>

    int main()
    {
    int x = int() = 3;

    std::cout << x << std::endl;

    return 0;
    }โ€‹
    options:
    a)0
    b)3
    c)undefined
    d)won't compile

    P.S. Do mention the logic for your answer.
    This question was asked to us in viva today.

  • simplycoder

    @simplycoder-NsBEdD • Sep 15, 2013

    Shailaja TiwariWhat value should be printed for x?
    #include <iostream>

    int main()
    {
    int x = int() = 3;

    std::cout << x << std::endl;

    return 0;
    }

    options:
    a)0
    b)3
    c)undefined
    d)won't compile

    P.S. Do mention the logic for your answer.
    This question was asked to us in viva today.

    d) Won't compile,
    code would expect an object int x = int() = 3;
    since this is not done, it would fail to compile throwing lvalue required error.

  • Shailaja Tiwari

    @shailaja-tiwari-lKhGjd • Sep 15, 2013

    Right answer .
    Any way when this question was being asked to us by external most of my classmates ended up either by being mum or by suggesting the question as wrong.

  • rahul69

    @rahul69-97fAOs • Sep 16, 2013

    Sanyam KhuranaHere's another one to go...
    Write a Program to find largest and smallest from five numbers without using any array and loop, or conditional statements.

    PS: Keep adding more questions, and let's solve them..!! ๐Ÿ‘

    Can we, however use conditional operators??

  • Sanyam Khurana

    @sanyam-Nl7Zqc • Sep 16, 2013

    simplycoderd) Won't compile,
    code would expect an object int x = int() = 3;
    since this is not done, it would fail to compile throwing lvalue required error.

    Can you please tell me, how this would expect an object, and what's the meaning of this

    rahul69Can we, however use conditional operators??

    Conditional Operators as in " ? : " ?

  • simplycoder

    @simplycoder-NsBEdD • Sep 16, 2013

    Sanyam KhuranaCan you please tell me, how this would expect an object, and what's the meaning of this


    Conditional Operators as in " ? : " ?

    #include<stdio.h>
    intย main(){
    ย ย 
    intย num1,num2,num3,num4,num5,largest,smallest;
    ย ย 
    printf("\n\n
    ย ย ย ย ย ย ย ย Enterย 5ย numbers:\n\n"
    );
    ย ย 
    scanf("%dย %dย %dย %dย %d",&num1,&num2,&num3,&num4,&num5);

    ย ย 
    largest=(num1>num2?num1:num2);
    ย ย 
    largest=(largest>num3?largest:num3);
    ย ย 
    largest=(largest>num4?largest:num4);
    ย ย 
    largest=(largest>num5?largest:num5);

    ย ย 
    smallest=(num1<num2?num1:num2);
    ย ย 
    smallest=(smallest<num3?smallest:num3);
    ย ย 
    smallest=(smallest<num4?smallest:num4);
    ย ย 
    smallest=(smallest<num5?smallest:num5);


    ย ย 
    printf("\n\nTheย largestย numberย is:ย %d\nย smallestย isย :%d\n",largest,smallest);

    ย ย returnย 
    0;
    }

  • Sanyam Khurana

    @sanyam-Nl7Zqc • Sep 16, 2013

    simplycoder #include<stdio.h>
    intย main(){
    ย ย 
    intย num1,num2,num3,num4,num5,largest,smallest;
    ย ย 
    printf("\n\n
    ย ย ย ย ย ย ย ย Enterย 5ย numbers:\n\n"
    );
    ย ย 
    scanf("%dย %dย %dย %dย %d",&num1,&num2,&num3,&num4,&num5);

    ย ย 
    largest=(num1>num2?num1:num2);
    ย ย 
    largest=(largest>num3?largest:num3);
    ย ย 
    largest=(largest>num4?largest:num4);
    ย ย 
    largest=(largest>num5?largest:num5);

    ย ย 
    smallest=(num1<num2?num1:num2);
    ย ย 
    smallest=(smallest<num3?smallest:num3);
    ย ย 
    smallest=(smallest<num4?smallest:num4);
    ย ย 
    smallest=(smallest<num5?smallest:num5);


    ย ย 
    printf("\n\nTheย largestย numberย is:ย %d\nย smallestย isย :%d\n",largest,smallest);

    ย ย returnย 
    0;
    }

    Oh, I was just asking him about if he meant " ? : " as conditional operators ...

    No, you can't do with conditional operators too...

    Remember, you have to do this using no loop, no array, no conditional statements and no conditional operator too...

    BDW, could you please explain me what does it mean by int() ?

    And also the meaning of this statement int x = int() = 3;

  • Nikumbh

    @nikumbh-lmTCgS • Sep 16, 2013

    Solution for the problem
    a[]={1,0,2,0,3,0}
    then, it should print
    1,2,3,0,0,0


    solution :

    #include<iostream>
    using namespace std;
    int main()
    {
        int a[15],i,j,k,n;
        cout<<"Enter the array size\n";
        cin>>n;
        cout<<"Enter the nos\n";
        for(i=0;i<n;i++)
            cin>>a[i];
        for (i = 0 ; i < (n-1); i++)
          {
                for (j = 0 ; j < (n-i-1); j++)
                {
            //if zero is followed by a non-zero, swap the zero and non-zero numbers
             
                      if ((a[j]==0) && ( a[j+1]!=0))
                      {
                    k      = a[j];
                        a[j]  = a[j+1];
                        a[j+1] = k;
                      }
                }
        }
     
        for(i=0;i<n;i++)
            cout<<a[i]<<" ";
        return 0;
    }
    



    It's just a bubble sort........

  • Nikumbh

    @nikumbh-lmTCgS • Sep 16, 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


    #include<iostream>
    using namespace std;
    int main()
    {
        int a[15],i,j,k,n;
        cout<<"Enter the array size\n";
        cin>>n;
        cout<<"Enter the nos\n";
        for(i=0;i<n;i++)
            cin>>a[i];
        for (i = 0 ; i < (n-1); i++)
          {
                for (j = 0 ; j < (n-i-1); j++)
                {
            //if zero is followed by a non-zero, swap the zero and non-zero numbers
               
                      if ((a[j]==0) && ( a[j+1]!=0))
                      {
                    k      = a[j];
                        a[j]  = a[j+1];
                        a[j+1] = k;
                      }
                }
        }
       
        for(i=0;i<n;i++)
            cout<<a[i]<<" ";
        return 0;
    }
    


    It's a kinda bubble sort technique........

  • Nikumbh

    @nikumbh-lmTCgS • Sep 16, 2013

    Write a program to reverse the words in the string ie., if "you are a boy" is a string then should be changed like, "uoy era a yob"

  • Nikumbh

    @nikumbh-lmTCgS • Sep 16, 2013

    Another question : Write a program to sort the elements of an array, the first half of the array should be sorted ascending, the second half of the array should be sorted descending..

    ie., a[]={9,2,3,7,4,5,6,3,8,2}

    the resultant a[]={2,3,4,7,9,8,6,5,3,2}

  • rahul69

    @rahul69-97fAOs • Sep 17, 2013

    Sanyam KhuranaCan you please tell me, how this would expect an object, and what's the meaning of this


    Conditional Operators as in " ? : " ?

    Yes I meant that (?๐Ÿ˜€ only.. so I guess we can't use those. I tried to write a program, but sadly it works only for natural numbers ๐Ÿ˜”
    Still sharing it, as I think it is an awesome Logic that I built.
    Do check it out: ๐Ÿ˜›๐Ÿ˜๐Ÿ˜

    #include<iostream>
    #include<stdlib.h>
    using namespace std;
    
    void check()
    {
      int n1,n2,n3,n4,n5,a1,a2,a3,a4,a5;
    cout<<"\nEnter the 5 numbers:";
    cin>>n1>>n2>>n3>>n4>>n5;
    a1=(((n1/n2)*(n1/n3)*(n1/n4)*(n1/n5))&&1)*n1;
    a2=(((n2/n1)*(n2/n3)*(n2/n4)*(n2/n5))&&1)*n2;
    a3=(((n3/n2)*(n3/n1)*(n3/n4)*(n3/n5))&&1)*n3;
    a4=(((n4/n2)*(n4/n3)*(n4/n1)*(n4/n5))&&1)*n4;
    a5=(((n5/n2)*(n5/n3)*(n5/n4)*(n5/n1))&&1)*n5;
    cout<<"\nThe highest number is:"<<(a1+a2+a3+a4+a5);
    a1=(((n1/n2)+(n1/n3)+(n1/n4)+(n1/n5))&&1);
    a2=(((n2/n1)+(n2/n3)+(n2/n4)+(n2/n5))&&1);
    a3=(((n3/n2)+(n3/n1)+(n3/n4)+(n3/n5))&&1);
    a4=(((n4/n2)+(n4/n3)+(n4/n1)+(n4/n5))&&1);
    a5=(((n5/n2)+(n5/n3)+(n5/n4)+(n5/n1))&&1);
    n1=(a2*a3*a4*a5)*n1;
    n2=(a1*a3*a4*a5)*n2;
    n3=(a1*a2*a4*a5)*n3;
    n4=(a1*a2*a3*a5)*n4;
    n5=(a1*a2*a3*a4)*n5;
    cout<<"\nThe lowest number is:"<<(n1+n2+n3+n4+n5)<<"\n"; 
    }
    
    main()
    {
    check();
    system("Pause");
    return 0;   
    }
    

    ๐Ÿ˜

  • Vishal Sharma

    @vishal-pysGmK • Sep 17, 2013

    NikumbhWrite a program to reverse the words in the string ie., if "you are a boy" is a string then should be changed like, "uoy era a yob"

    I did this program using data structures long back! Here it is!


    #include<iostream>
    #include<cstdlib>
    using namespace std;
    
    struct list
    {
        char c;
        struct list *next;
    };
    
    list *reverse(list *);
    
    int main()
    {
        string input = "you are a boy";
        list *curr = new list;
        list *head = NULL;
    
        for(int i = 0 ; i < input.size() ; i++)
        {
            if(i == 0)
            {
                curr -> c = input[i];
                curr -> next = NULL;
                head = curr;
            }
            else
            {
                list *temp = new list;
                temp -> c = input[i];
                temp -> next = NULL;
                curr -> next = temp;
                curr = temp;
            }
        }
    
        list *temp;
        temp = head;
        while(temp != NULL)
        {
            cout<<temp->c;
            temp = temp->next;
        }
    
        temp = reverse(head);
    
        cout<<endl;
        while(temp != NULL)
        {
            cout<<temp->c;
            temp = temp->next;
        }
    }
    
    
    list * reverse(list *head)
    {
        list *curr = head;
        list *prev = NULL;
        list *last = head;
    
        while(curr!= NULL && curr->c != ' ')
        {
            list *nextnode = curr->next;
            curr->next = prev;
            prev = curr;
            curr = nextnode;
        }
    
        last->next = curr;
    
        if(curr != NULL && curr->next != NULL)
        {
            curr -> next = reverse(curr -> next);
        }
    
        return prev;
    }
  • Vishal Sharma

    @vishal-pysGmK • Sep 17, 2013

    NikumbhAnother question : Write a program to sort the elements of an array, the first half of the array should be sorted ascending, the second half of the array should be sorted descending..

    ie., a[]={9,2,3,7,4,5,6,3,8,2}

    the resultant a[]={2,3,4,7,9,8,6,5,3,2}
    #include<stdio.h>
    int main() {
        int a[10];
        int n,mid;
        printf("Enter num : ");
        scanf("%d",&n);
        mid = n/2;
        for(int i = 0 ; i < n ; i++) {
            scanf("%d",&a[i]);
        }
        for(int i = 0 ; i < mid ; i++) {
            for(int j = 0 ; j < mid ; j++) {
                if(a[i] < a[j]) {
                    int temp = a[i];
                    a[i] = a[j];
                    a[j] = temp;
                }
            }
        }
       
       
        for(int i = mid ; i < n ; i++) {
            for(int j = mid ; j < n ; j++) {
                if(a[i] > a[j]) {
                    int temp = a[i];
                    a[i] = a[j];
                    a[j] = temp;
                }
            }
        }
       
        for(int i = 0 ; i < n ; i++) {
            printf("%d ",a[i]);
        }
    }
  • Nikumbh

    @nikumbh-lmTCgS • Sep 17, 2013

    Vishal0203I did this program using data structures long back! Here it is!


    #include<iostream>
    #include<cstdlib>
    using namespace std;
    
    struct list
    {
        char c;
        struct list *next;
    };
    
    list *reverse(list *);
    
    int main()
    {
        string input = "you are a boy";
        list *curr = new list;
        list *head = NULL;
    
        for(int i = 0 ; i < input.size() ; i++)
        {
            if(i == 0)
            {
                curr -> c = input[i];
                curr -> next = NULL;
                head = curr;
            }
            else
            {
                list *temp = new list;
                temp -> c = input[i];
                temp -> next = NULL;
                curr -> next = temp;
                curr = temp;
            }
        }
    
        list *temp;
        temp = head;
        while(temp != NULL)
        {
            cout<<temp->c;
            temp = temp->next;
        }
    
        temp = reverse(head);
    
        cout<<endl;
        while(temp != NULL)
        {
            cout<<temp->c;
            temp = temp->next;
        }
    }
    
    
    list * reverse(list *head)
    {
        list *curr = head;
        list *prev = NULL;
        list *last = head;
    
        while(curr!= NULL && curr->c != ' ')
        {
            list *nextnode = curr->next;
            curr->next = prev;
            prev = curr;
            curr = nextnode;
        }
    
        last->next = curr;
    
        if(curr != NULL && curr->next != NULL)
        {
            curr -> next = reverse(curr -> next);
        }
    
        return prev;
    }


    @#-Link-Snipped-# Great work buddy !

  • Vishal Sharma

    @vishal-pysGmK • Sep 17, 2013

    Nikumbh@#-Link-Snipped-# Great work buddy !

    thanks! ๐Ÿ˜›๐Ÿ˜

  • Sanyam Khurana

    @sanyam-Nl7Zqc • Sep 22, 2013

    Ok, answer what will be the output of the below mentioned code

    //assume all necessary header files added..
    void main()
    {
    char i=0;
    for(;i>=0;i++);
    printf("i=%d",i);
    getch();
    }
    
  • Jeffrey Arulraj

    @jeffrey-xA7lUP • Sep 22, 2013

    I think we will have output in the form of
    i=0
    i=1
    till
    i=255

    Sanyam KhuranaOk, answer what will be the output of the below mentioned code

    //assume all necessary header files added..
    void main()
    {
    char i=0;
    for(;i>=0;i++);
    printf("i=%d",i);
    getch();
    }
    
  • Sanyam Khurana

    @sanyam-Nl7Zqc • Sep 22, 2013

    Jeffrey SamuelI think we will have output in the form of
    i=0
    i=1
    till
    i=255


    You're wrong...

    Look at the question carefully, and if I say carefully, I mean it..

    In programming even a little thing have tremendous powers.. ๐Ÿ˜›

  • simplycoder

    @simplycoder-NsBEdD • Sep 22, 2013

    According to me, the output for this is specific to compiler.

    Case 1:
    When char is treated as signed char.
    It will print i=-128.
    For loop there has a semi-colon (๐Ÿ˜‰
    loop would terminate after i=127
    thus when it would try to increment, cyclic property would then make it -128
    [Signed chars range from -128 to 127 ].

    Case 2:
    When char is treated as unsigned char,
    the for loop would never terminate causing an infinite loop.
    In this case, I would increment from 0 to 255 and then get reset to 0.
    thus the i>=0 condition would never fail.
    [Unsigned chars range from 0 to 255 ].

  • Sanyam Khurana

    @sanyam-Nl7Zqc • Sep 23, 2013

    simplycoderAccording to me, the output for this is specific to compiler.

    Case 1:
    When char is treated as signed char.
    It will print i=-128.
    For loop there has a semi-colon (๐Ÿ˜‰
    loop would terminate after i=127
    thus when it would try to increment, cyclic property would then make it -128
    [Signed chars range from -128 to 127 ].

    Case 2:
    When char is treated as unsigned char,
    the for loop would never terminate causing an infinite loop.
    In this case, I would increment from 0 to 255 and then get reset to 0.
    thus the i>=0 condition would never fail.
    [Unsigned chars range from 0 to 255 ].


    Yeah, you got it right mate..!! ๐Ÿ˜

  • Jeffrey Arulraj

    @jeffrey-xA7lUP • Sep 23, 2013

    Sanyam KhuranaYou're wrong...

    Look at the question carefully, and if I say carefully, I mean it..

    In programming even a little thing have tremendous powers.. ๐Ÿ˜›

    Mate thank you for telling my error Now I understood it clearly

    I was wrong @#-Link-Snipped-# Awesome mate

    But will not the loop get terminated by the semicolon
    Then how will it be repeated that many times

  • Sanyam Khurana

    @sanyam-Nl7Zqc • Sep 23, 2013

    Jeffrey SamuelBut will not the loop get terminated by the semicolon
    Then how will it be repeated that many times

    This is an empty loop, so the loop would execute till the conditional statement is true, and as soon as it becomes false, as in case 1 told by simplycoder, the loop will end, and the last value of i would be printed.

    Ok, tell which one is better and also explain the reason.

    //1)version of program
    
    for(i=1;i<=1000;i++)
    {
    for(j=1;j<=100;j++)
    {
    printf("hi");
    }
    }
    
    
    // 2) version of Program
    
    for(i=1;i<=100;i++)
    {
    for(j=1;j<=1000;j++)
    {
    printf("Hi");
    }
    }

    Tell the output of the program with explanation.

    what will be d output of the following code??
    
    #include<stdio.h>
    int main()
    {
    int a=3;
    a=a++ * a++ * a++;
    
    printf("a=%d",a);
    
    return 0;
    }

    Although these are not really tough and tricky one, but just for beginning sharing these..

    void main()
    {
    int a=1;
    while (a<=5)
    {
    printf("%d",a);
    if (a>2)
    goto here;
    a++;
    }
    }
    void fun()
    {
    here:
    printf("OMG");
    }

    Explain output with explanation of the above mentioned code..

  • rahul69

    @rahul69-97fAOs • Sep 24, 2013

    Sanyam KhuranaOk, tell which one is better and also explain the reason.

    //1)version of program
    
    for(i=1;i<=1000;i++)
    {
    for(j=1;j<=100;j++)
    {
    printf("hi");
    }
    }
    
    
    // 2) version of Program
    
    for(i=1;i<=100;i++)
    {
    for(j=1;j<=1000;j++)
    {
    printf("Hi");
    }
    }

    IMO second code is better, as it will have less time complexity
    For first one, in the inner loop 100 statements for entering inside the loop and one statement for exiting the inner loop, so total inner statements= 100+1=101, now these multiplied by outer loop ie 101 * 1000 = 101000
    Similarly For second one, 1001 * 100 =100100
    so second one is better!

    Sanyam KhuranaTell the output of the program with explanation.

    what will be d output of the following code??
    
    #include<stdio.h>
    int main()
    {
    int a=3;
    a=a++ * a++ * a++;
    
    printf("a=%d",a);
    
    return 0;
    }

    Post Increment ! ie use then change, so used as 3*3*3=27 ๐Ÿ‘

  • Sanyam Khurana

    @sanyam-Nl7Zqc • Sep 26, 2013

    Adding some more output questions..

    Explain the output of the program

    void main()
    {
    char *p;
    p="Hello";
    printf("%c\n",*&*p);
    getch();
    }




    Show the output and also explain the reason behind your output

    #include<stdio.h>
    int main()
    {
    char a,b;
    printf("%d ",12);
    printf("%d ",143);
    printf("%d ",scanf("%d%d",&a,&b));
    }

    Adding one more tricky question....

    Write a PROGRAM to find a number is EVEN OR ODD without using any control construct(if,switch,loop) , relational operators and ternary operators.

  • Vishal Sharma

    @vishal-pysGmK • Sep 27, 2013

    Sanyam KhuranaAdding one more tricky question....

    Write a PROGRAM to find a number is EVEN OR ODD without using any control construct(if,switch,loop) , relational operators and ternary operators.
    #include<stdio.h>
    
    int main() {
        int x;
        scanf("%d",&x);
        ((x%2)&1 && printf("odd"))||printf("even");
    }
  • Aadit Kapoor

    @aadit-kapoor-EBxnXT • Sep 28, 2013

    #include <stdio.h>
    int main()
    {
    char print[100] = "HelloWorld";
    int i;
    for (i=0;i<99;i++)
    {
    printf("%c\n",print);
    }
    }

  • Aadit Kapoor

    @aadit-kapoor-EBxnXT • Sep 28, 2013

    #include <stdio.h>
    int main()
    {
    char print[100] = "HelloWorld";
    int i;
    for (i=0;i<99;i++)
    {
    printf("%c\n",print);
    }
    }

    ITS PRINT

  • Aadit Kapoor

    @aadit-kapoor-EBxnXT • Sep 28, 2013

    Why is this website not accepting array sign?

  • Aadit Kapoor

    @aadit-kapoor-EBxnXT • Sep 28, 2013

    Sanyam KhuranaShow the output and also explain the reason behind your output

    #include<stdio.h>
    int main()
    {
    char a,b;
    printf("%d ",12);
    printf("%d ",143);
    printf("%d ",scanf("%d%d",&a,&b));
    }

    The output will be 12 and 143.
    then if we type two char's then it will give the value accordingly.I think it will give the asci value of the char's

  • Aadit Kapoor

    @aadit-kapoor-EBxnXT • Sep 28, 2013

    Sanyam KhuranaAdding one more tricky question....

    Write a PROGRAM to find a number is EVEN OR ODD without using any control construct(if,switch,loop) , relational operators and ternary operators.

    #include <stdio.h>
    int main()
    {
    int i;
    printf("Enter a number:\n");
    scanf("%d",&i);

    int y = i % 2;
    printf("%d\n",y);
    }
    if y = 1 then it is odd.
    if y = 0 then it is even.

  • Vishal Sharma

    @vishal-pysGmK • Sep 28, 2013

    Aadit KapoorThe output will be 12 and 143.
    then if we type two char's then it will give the value accordingly.I think it will give the asci value of the char's

    it doesn't give the ascii value.. if you look at the declaration, a and b are characters but in scanf() we ask for an integer (presence of %d) hence if you enter 2 characters there, the printf() will print zero as it expects an integer not character! and if you enter two integer values, it'll print 2 because scanf returns the number of inputs it takes!

  • Aadit Kapoor

    @aadit-kapoor-EBxnXT • Sep 28, 2013

    Thank you for correcting me

  • Aadit Kapoor

    @aadit-kapoor-EBxnXT • Sep 28, 2013

    Sanyam KhuranaAlthough these are not really tough and tricky one, but just for beginning sharing these..

    void main()
    {
    int a=1;
    while (a<=5)
    {
    printf("%d",a);
    if (a>2)
    goto here;
    a++;
    }
    }
    void fun()
    {
    here:
    printf("OMG");
    }
    Explain output with explanation of the above mentioned code..

    There will be no output. An error would be generated

  • Sanyam Khurana

    @sanyam-Nl7Zqc • Sep 28, 2013

    Aadit KapoorThe output will be 12 and 143.
    then if we type two char's then it will give the value accordingly.I think it will give the asci value of the char's

    It would print 12143 and then something else, and that's what the tricky part is here..

    So think carefully and try again...

    Aadit KapoorThere will be no output. An error would be generated

    What kind of error, explain it in detail, so that others can also learn from this..

    If there's an output, explain how you came to that conclusion and if there's an error, explain why error is there.

    Aadit Kapoor#include <stdio.h>
    int main()
    {
    char print[100] = "HelloWorld";
    int i;
    for (i=0;i<99;i++)
    {
    printf("%c\n",print);
    }
    }

    I think this would print only H 99 times, as you have given print in the statement so array is always taken as a pointer to the first element, so only H will be printed.

    Ok, here's another one..

    What will the output of the following C++ code be?

    #include<iostream>
    using namespace std;
    
    void check_arr_size(int array[20]); //Function Prototype
    
    int main()
    {
    int array[20];
    cout << "In main: size of array - " << sizeof(array) << endl;
    check_arr_size(array);
    return 0;
    }
    
    void check_arr_size(int array[20]) //Function definition
    {
    cout << "Inside function: size of array - " << sizeof(array) << endl;
    }
  • Aadit Kapoor

    @aadit-kapoor-EBxnXT • Sep 29, 2013

    Aadit KapoorThere will be no output. An error would be generated

    The error generated will be that no label has been defined.

  • Aadit Kapoor

    @aadit-kapoor-EBxnXT • Sep 29, 2013

    Sanyam KhuranaIt would print 12143 and then something else, and that's what the tricky part is here..

    So think carefully and try again...


    What kind of error, explain it in detail, so that others can also learn from this..

    If there's an output, explain how you came to that conclusion and if there's an error, explain why error is there.


    I think this would print only H 99 times, as you have given print in the statement so array is always taken as a pointer to the first element, so only H will be printed.

    Ok, here's another one..

    What will the output of the following C++ code be?

    #include<iostream>
    using namespace std;
    
    void check_arr_size(int array[20]); //Function Prototype
    
    int main()
    {
    int array[20];
    cout << "In main: size of array - " << sizeof(array) << endl;
    check_arr_size(array);
    return 0;
    }
    
    void check_arr_size(int array[20]) //Function definition
    {
    cout << "Inside function: size of array - " << sizeof(array) << endl;
    }

    I know the output but i can't understand.Can you tell me please?

  • Sanyam Khurana

    @sanyam-Nl7Zqc • Sep 29, 2013

    #define int char
    void main()
    {
    int i=65;
    printf("sizeof(i)=%d",sizeof(i));
    getch();
    }


    write output with explaination.

  • Aadit Kapoor

    @aadit-kapoor-EBxnXT • Sep 29, 2013

    Sanyam Khurana
    #define int char
    void main()
    {
    int i=65;
    printf("sizeof(i)=%d",sizeof(i));
    getch();
    }

    write output with explaination.

    There wil be no output because in '#define int char' we cannot use use int and char because it is a keyword

  • Aadit Kapoor

    @aadit-kapoor-EBxnXT • Sep 29, 2013

    #include <stdio.h>
    #include <stdlib.h>
    void add(int *);
    int main()
    {
    int x = 12;
    int *p = &x;
    int **c = &p;
    int ***f  = &c;
    int ****d = &p;
    x++;
    printf("**f = *p + x\n");
    ***f = *p + x;
    printf("Now printing **c\n");
    printf("%d\n",**c);
    printf("Allocting memory!\n");
    int *ptr = (int *)malloc(sizeof(***f));
    if (*ptr == NULL)
    {
       printf("Error!\n");
    }
       int i;
       for (i=0;i<12;i++)
       {
       printf("Using add(int*) function\n");
       printf("Changing a value with 10-:\n");
       int a = 10;
       add(&a);
       printf("%d\n",a);
       }
    
       return 0;
    }
    void add(int *a)
    {
       *a = *a + 10;  
    }
    
    

    what is the ouput?

  • vaibhav bhanawat

    @vaibhav-bhanawat-Bk2Joo • Sep 29, 2013

    #include<stdio.h>
    #include<conio.h>
    void main()
    {
    clrscr();
    int large[5]={4,2,1,3,5};
    int i,j,p,q;
    p=large[0];
    for(i=1;i<5;i++)
    {
    if(p<large)
    p=large;
    }
    q=large[0];
    for(i=0;i<5;i++)
    {
    if(q>large)
    q=large;
    }
    printf("biggest number in given array is: %d\nsmallest number is%d",p,q);
    getch();
    }
    answer of ur question is.......!!!!!!!!
    here is an code for largest and smallest number from an array.....

  • vaibhav bhanawat

    @vaibhav-bhanawat-Bk2Joo • Sep 29, 2013

    write a program to print 1 to 100 number ......!
    without using loops and also without goto statement......!

  • vaibhav bhanawat

    @vaibhav-bhanawat-Bk2Joo • Sep 29, 2013

    FIND FRACTIONAL POWER OF A NUMBER WITHOUT USING ANY PREDEFINED FUNCTION IN C
    don't use pow function.......