Programming Questions

Q:

What will be output of following c code?

#include <stdio.h>
int main()

{
    int i;
    for(i=10;i<=15;i++){
         while(i){
             do{
                 printf("%d ",1);
                 if(i>1)
                      continue;
             }while(0);
             break;
         }
    }
    return 0;
}

Answer

Output: 1 1 1 1 1 1
For loop will execute six times.


 


 


 


Note: continue keyword in do-while loop bring the program its while condition (while(0)) which is always false.

Report Error

View answer Workspace Report Error Discuss

Subject: Programming

0 1678
Q:

What would be the output of the following program?

main()

{

  int i=2 ;

  printf ("\n%d%d", ++i, ++i );

}

Answer

Output may vary from compiler to compiler.


The order of evaluation of the arguments to a function call is unspecified.

Report Error

View answer Workspace Report Error Discuss

Subject: Programming

0 1658
Q:

C program to find the factorial of a given number

Answer

 #include<stdio.h>
int main(){
  int i=1,f=1,num;

  printf("Enter a number: ");
  scanf("%d",&num);

  while(i<=num){
      f=f*i;
      i++;
  }

  printf("Factorial of %d is: %d",num,f);
  return 0;
}

Sample output:
Enter a number: 5
Factorial of 5 is: 120

Report Error

View answer Workspace Report Error Discuss

Subject: Programming

0 1649
Q:

Why we use do-while loop in c?

Answer

It is also called as post tested loop. It is used when it is necessary to execute the loop at least one time. Syntax:

do {
Loop body
} while (Expression);

Example:

int main(){
    int num,i=0;  
    do{
         printf("To enter press 1\n");
         printf("To exit press  2");
         scanf("%d",&num);
         ++i;
         switch(num){
             case 1:printf("You are welcome\n");break;
             default : exit(0);
         }
    }
    while(i<=10);
    return 0;
}

Output: 3 3 4 4

Report Error

View answer Workspace Report Error Discuss

Subject: Programming

0 1638
Q:

Write a c program to check whether a number is strong or not.

Answer

#include<stdio.h>
int main(){
  int num,i,f,r,sum=0,temp;

  printf("Enter a number: ");
  scanf("%d",&num);
 
  temp=num;
  while(num){
      i=1,f=1;
      r=num%10;

      while(i<=r){
         f=f*i;
        i++;
      }
      sum=sum+f;
      num=num/10;
  }
  if(sum==temp)
      printf("%d is a strong number",temp);
  else
      printf("%d is not a strong number",temp);

  return 0;
}

Sample output:
Enter a number: 145
145 is a strong number

Report Error

View answer Workspace Report Error Discuss

Subject: Programming

0 1629
Q:

Point out the error, if any, in the following program.

main()

{

    int i = 4, j = 2;

    switch(i)

    {

     case 1 :

       printf (''\n To err is human, to forgive is against company policy.");

        break;

      case j :

       printf (''\n if you have nothing to do, don't do it here.");

       break;

    }

}

Answer

Constant expression required in the second case, we cannot use j.

Report Error

View answer Workspace Report Error Discuss

Subject: Programming

0 1622
Q:

How would you eliminate the warning generated on complaining the following program?

main()

{

  char far *scr;

  scr = 0xB8000000;

  *scr = 'A';

}

Answer

Use the typecast scr = (char far *) 0xB8000000;

Report Error

View answer Workspace Report Error Discuss

Subject: Programming

0 1612
Q:

In HTML, ___________ tag is used to construct drop-down list boxes and scrolling list boxes.

A) <SELECT> B) <OPTION>
C) <TEXTAREA> D) <INPUT>
 
Answer & Explanation Answer: A) <SELECT>

Explanation:
Report Error

View Answer Report Error Discuss

4 1407