Tuesday, April 10, 2012

Leap Year Problem.....

1-A year is called leap year if it is divisible by 400
2-If year is not divisible by 400 as well as 100 but it is divisible by 4 then that year are also leap year.

To determine a year is leap year or not...


#include<stdio.h>
int main(){
    int year;

    printf("Enter any year: ");
    scanf("%d",&year);

    if(((year%4==0)&&(year%100!=0))||(year%400==0))
         printf("%d is a leap year",year);
    else
         printf("%d is not a leap year",year);
 
    return 0;
}

To find leap year in between a range of year....



#include<stdio.h>
int main(){
    int year;
    int min_year,max_year;

    printf("Enter the lowest year: ");
    scanf("%d",&min_year);

    printf("Enter the heighest year: ");
    scanf("%d",&max_year);

    printf("Leap years in given range are: ");
    for(year = min_year;year <= max_year; year++){
         if(((year%4==0)&&(year%100!=0))||(year%400==0))
             printf("%d ",year);
    }
 
    return 0;
}

Reverse a number using c


1-Simple method  of reversing an integer number with the help of while loop....

#include<stdio.h>
int main(){
    int num,r,reverse=0;

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

    while(num){
         r=num%10;
         reverse=reverse*10+r;
         num=num/10;
    }

    printf("Reversed of number: %d",reverse);
    return 0;
}

2-With help of for loop....

#include<stdio.h>
int main(){
    int num,r,reverse=0;

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

    for(;num!=0;num=num/10){
         r=num%10;
         reverse=reverse*10+r;
    }

    printf("Reversed of number: %d",reverse);
    return 0;
}


3-With help of recursive function....
#include<stdio.h>
int main(){
    int num,reverse;

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

    reverse=rev(num);
    printf("Reverse of number: %d",reverse);
    return 0;
}

int rev(int num){
    static sum,r;

    if(num){
         r=num%10;
         sum=sum*10+r;
         rev(num/10);
    }
    else
         return 0;

    return sum;
}

4-And finally method for reversing a very large number which is beyond the range of int and long int....

#include<stdio.h>
#define MAX 1000

int main(){

    char num[MAX];
    int i=0,j,flag=0;

    printf("Enter any positive integer: ");
    scanf("%s",num);

    while(num[i]){
         if(num[i] < 48 || num[i] > 57){
             printf("Invalid integer number");
             return 0;
         }
         i++;
    }

    printf("Reverse: ");
    for(j=i-1;j>=0;j--)
         if(flag==0 &&  num[j] ==48){
         }
         else{
             printf("%c",num[j]);
             flag =1;
         }

    return 0;


Wednesday, March 21, 2012

preprocessor


1) What will be output of following code?

#define max 10
void main()
{
    int i;
    i=++max;
    clrscr();
    printf("%d",i);
    getch();
}

output: compiler error.
Explanation:  max is preprocessor macro which process first before the actual compilation. Preprocessor paste the symbol to its constant value in entire the program before the compilation. so in this program max will be replaced by 10 before compilation. Thus program will be converted as:


void main()
{
     int i;
     i=++10;
     clrscr();
     printf("%d",i);
     getch();
}

To know how we can see intermediate file click here

In this program we are trying to increment a constant symbol.

Meaning of ++10 IS:-
10=10+1
or 10=11

Which is error because we cannot assign constant value to another constant value .Hence compiler will give error.



(2) What will be output of following code?

#define max 10+2
void main()
{
    int i;
    i=max*max;
    clrscr();
    printf("%d",i);
    getch();
}


Output:32

Explanation: 

max is preprocessor macro which process first before the actual compilation. Preprocessor paste the symbol to the its constant value without any calculation in entire the program before the compilation. So in this program max will be replaced by 10+2 before compilation. Thus program will be converted as:


void main()
{
    int i;
    i=10+2*10+2;
    clrscr();
    printf("%d",i);
    getch();
}

now i=10+2*10+2
i=10+20+2
i=32


(3) What will be output of following code?

#define A 4-2
#define B 3-1
void main()
{
     int ratio=A/B;
     printf("%d ",ratio);
     getch();
}


Output:3
Explanation: A and B preprocessor macro which process first before the actual compilation. Preprocessor paste the symbol to its constant value without any calculation in entire the program before the compilation. So in this program A and B will be replaced by 4-2 and 3-1 respectively before compilation. Thus program will be converted as:


void main()
{
    int ratio=4-2/3-1;
    printf("%d ",ratio);
    getch();
}


Here ratio=4-2/3-1
ratio=4-0-1
ratio=3


(4) What will be output of following code?

#define MAN(x,y) (x)>(y)?(x):(y)
void main()
{
       int i=10,j=9,k=0;
       k=MAN(i++,++j);
       printf("%d %d %d",i,j,k);
       getch();
}


Output: 11 11 11

Explanation: Preprocessor’s macro which process first before the actual compilation. Preprocessor paste the symbol to its constant value without any calculation in entire the program before the compilation. Thus program will be converted as:


void main()
{
    int i=10,j=9,k=0;
    k=(i++)>(++j)?(i++):(++j);
    printf("%d %d %d",i,j,k);
    getch();
}

now k=(i++)>(++j)?(i++):(++j);
first it will check the condition
(i++)>(++j)

i++ i.e. when postfix is used with variable in expression then expression is evaluated first with original value then variable is incremented

Or 10>10

This condition is false.
Now i = 10+1 = 11
There is rule, only false part will execute after? i.e. ++j, i++ will be not execute.
So after ++j
j=10+1=11;

And k will assign value of j .so k=11;


(5) What will be output of following code?

#define START main() {
#define PRINT printf("*******");
#define END }
START
PRINT
END


Output: *******
Explanation: 

Preprocessor macro which process first before the actual compilation. Preprocessor paste the symbol to its constant value i.e. symbols without any calculation in entire the program before the compilation. Thus program will be converted as:


main()
{
    printf("*******");
}


(6) What will be output of following code?
#define CUBE(x) (x*x*x)
#define M 5
#define N M+1
#define PRINT printf("RITESH");
void main()
{
      int volume =CUBE(3+2);
      clrscr();
      printf("%d %d ",volume,N);
      PRINT
      getch();
}


Output: 17 6
Explanation:

Preprocessor macro which process first before the actual compilation. Preprocessor paste the symbol to its constant value without any calculation in entire the program before the compilation. Thus program will be converted as:

void main()
{
    int volume =(3+2*3+2*3+2);
    clrscr();
    printf("%d %d ",volume,5+1);
    PRINT
    getch();
}




Wednesday, March 7, 2012

Technical set 2

1.
What will be output when you will execute following c code?
#include<stdio.h>
void main(){
switch(5/2*6+3.0){
case 3:printf("David Beckham");
break;
case 15:printf("Ronaldinho");
break;
case 0:printf("Lionel Messi");
break;
default:printf("Ronaldo");
}
}
Choose all that apply:

(A) David Beckham
(B) Ronaldinho
(C) Lionel Messi
(D) Ronaldo
(E) Compilation error

Explanation:e
2.
What will be output when you will execute following c code?
#include<stdio.h>
void main(){
unsigned char c=280;
switch(c){
printf("Start\t");
case 280:printf("David Beckham\t");
case 24: printf("Ronaldinho\t");
default: printf("Ronaldo\t");
printf("End");
}
}
Choose all that apply:

(A)Start David Beckham Ronaldinho Ronaldo End
(B) Start David Beckham Ronaldinho Ronaldo
(C) Start Ronaldinho Ronaldo End
(D)Ronaldinho Ronaldo End

(E) Compilation error
Explanation:d
3.
What will be output when you will execute following c code?
#include<stdio.h>
#define TRUE 1
void main(){
switch(TRUE){
printf("cquestionbank.blogspot.com");
}
}

Choose all that apply:
(A) cquestionbank.blogspot.com
(B) It will print nothing
(C) Runtime error
(D) Compilation error
(E) None of the above

Explanation:b
4.
What will be output when you will execute following c code?
#include<stdio.h>
void main(){
static int i;
int j=1;
int arr[5]={1,2,3,4};
switch(arr[j]){
case 1: i++;break;
case 2: i+=2;j=3;continue;
case 3: i%=2;j=4;continue;
default: --i;
}
printf("%d",i);
}

Choose all that apply:
(A) 0
(B) 1
(C) 2
(D) Compilation error
(E) None of the above

Explanation:d
5.
What will be output when you will execute following c code?
#include<stdio.h>
void main(){
static int i;
int j;
for(j=0;j<=5;j+=2)
switch(j){
case 1: i++;break;
case 2: i+=2;
case 4: i%=2;j=-1;continue;
default: --i;continue;
}
printf("%d",i);
}

Choose all that apply:
(A) 0
(B) 1
(C) 2
(D) Compilation error

(E) None of the above
Explanation:a
6.
What will be output when you will execute following c code?
#include<stdio.h>
void main(){
int x=3;
while(1){
switch(x){
case 5/2: x+=x+++x;
case 3%4: x+=x---x;continue;
case 3>=3: x+=!!!x;break;
case 5&&0:x+=~~~x;continue;
default: x+=-x--;
}
break;
}
printf("%d",x);
}

Choose all that apply:
(A) 3
(B) -1
(C) 5
(D) Compilation error
(E) None of the above

Explanation:b
7.
What will be output when you will execute following c code?
#include<stdio.h>
void main(){
char *str="cquestionbank.blogspot.com";
int a=2;
switch('A'){
case 97:
switch(97){
default: str+=1;
}
case 65:
switch(97){
case 'A':str+=2;
case 'a':str+=4;
}
default:
for(;a;a--)
str+=8;
}
printf("%s",str);
}

Choose all that apply:
(A) cquestionbank.blogspot.com
(B) blogspot.com
(C) com
(D) Compilation error

(E) None of the above
Explanation:e
8.
What will be output when you will execute following c code?
#include<stdio.h>
void main(){
switch(2){
case 1L:printf("No");
case 2L:printf("%s","I");
goto Love;
case 3L:printf("Please");
case 4L:Love:printf("Hi");
}
}

Choose all that apply:
(A) I
(B) IPleaseHi
(C) IHi
(D) Compilation error
(E) None of the above

Explanation:c
9.
What will be output when you will execute following c code?
#include<stdio.h>
void main(){
int a=5;
a=a>=4;
switch(2){
case 0:int a=8;
case 1:int a=10;
case 2:++a;
case 3:printf("%d",a);
}
}
Choose all that apply:

(A) 8
(B) 11
(C) 10
(D) Compilation error
(E) None of the above

Explanation:e
10.
What will be output when you will execute following c code?
#include<stdio.h>
void main(){
int a=3,b=2;
a=a==b==0;
switch(1){
a=a+10;
}
sizeof(a++);
printf("%d",a);
}

Choose all that apply:
(A) 10
(B) 11
(C) 12

(D)1
(E) Compilation error
Explanation:d

Technical set 1

1.
What will be output when you will execute following c code?#include<stdio.h>
void main(){
     int check=2;
     switch(check){
        case 1: printf("D.W.Steyn");
        case 2: printf(" M.G.Johnson");
        case 3: printf(" Mohammad Asif");
        default: printf(" M.Muralidaran");
     }
}
Choose all that apply:
(A)    M.G.Johnson   
(B)    M.Muralidaran   
(C)   
M.G.Johnson Mohammad Asif M.Muralidaran
(D)    Compilation error   
(E)    None of the above   
Explanation:c
2.
What will be output when you will execute following c code?#include<stdio.h>
void main(){
     int movie=1;
     switch(movie<<2+movie){
        default:printf("3 Idiots");
        case 4: printf(" Ghajini");
        case 5: printf(" Krrish");
        case 8: printf(" Race");
     }
}
Choose all that apply:
(A)    3 Idiots Ghajini Krrish Race   
(B)    Race   
(C)    Krrish   
(D)    Ghajini Krrish Race   
(E)    Compilation error   
Explanation:b
3.
What will be output when you will execute following c code?#include<stdio.h>
#define L 10
void main(){
     auto money=10;
     switch(money,money*2){
        case L:  printf("Willian");
                  break;
        case L*2:printf("Warren");
                  break;
        case L*3:printf("Carlos");
                  break;
        default: printf("Lawrence");
        case L*4:printf("Inqvar");
                  break;
     } 
}
Choose all that apply:
(A)    Willian   
(B)    Warren   
(C)    Lawrence Inqvar   
(D)    Compilation error: Misplaced default   
(E)    None of the above   
Explanation:b
4.
What will be output when you will execute following c code?#include<stdio.h>
void main(){
     int const X=0;
     switch(5/4/3){
        case X:  printf("Clinton");
                  break;
        case X+1:printf("Gandhi");
                  break;
        case X+2:printf("Gates");
                  break;
        default: printf("Brown");
     }
}
Choose all that apply:
(A)    Clinton   
(B)    Gandhi   
(C)    Gates   
(D)    Brown   
(E)    Compilation error   
Explanation:e
5.
What will be output when you will execute following c code?#include<stdio.h>
enum actor{
    SeanPenn=5,
    AlPacino=-2,
    GaryOldman,
    EdNorton
};
void main(){
     enum actor a=0;
     switch(a){
        case SeanPenn:  printf("Kevin Spacey");
                         break;
        case AlPacino:  printf("Paul Giamatti");
                         break;
        case GaryOldman:printf("Donald Shuterland");
                         break;
        case EdNorton:  printf("Johnny Depp");
     } 
}
Choose all that apply:
(A)    Kevin Spacey   
(B)    Paul Giamatti   
(C)    Donald Shuterland   
(D)    Johnny Depp   
(E)    Compilation error   
Explanation:d
6.
What will be output when you will execute following c code?
#include<stdio.h>
void main(){
     switch(*(1+"AB" "CD"+1)){
        case 'A':printf("Pulp Fiction");
                  break;
        case 'B':printf("12 Angry Man");
                  break;
        case 'C':printf("Casabance");
                  break;
        case 'D':printf("Blood Diamond");
     }
}
Choose all that apply:
(A)    Pulp Fiction   
(B)    12 Angry Man   
(C)    Casabance   
(D)    Blood Diamond   
(E)    Compilation error   
Explanation:c
7.
What will be output when you will execute following c code?#include<stdio.h>
void main(){
    char *str="ONE"
    str++;
    switch(str){
        case "ONE":printf("Brazil");
                break;
        case "NE": printf("Toy story");
                break;
        case "N":  printf("His Girl Friday");
                break;
        case "E":  printf("Casino Royale");
     } 
}
Choose all that apply:
(A)    Brazil   
(B)    Toy story   
(C)    His Girl Friday   
(D)    Casino Royale   
(E)    Compilation error   
Explanation:e
8.
What will be output when you will execute following c code?#include<stdio.h>
void main(){
    switch(5||2|1){
        case 3&2:printf("Anatomy of a Murder");
              break;
        case -~11:printf("Planet of Apes");
               break;
        case 6-3<<2:printf("The conversation");
               break;
    case 5>=5:printf("Shaun of the Dead");
     }
}
Choose all that apply:
(A)    Anatomy of a Murder   
(B)    Planet of Apes   
(C)    The conversation   
(D)    Shaun of the Dead   
(E)    Compilation error   
Explanation:e
9.
What will be output when you will execute following c code?#include<stdio.h>
void main(){
    switch(6){
        case 6.0f:printf("Sangakkara");
               break;
        case 6.0: printf("Sehwag");
               break;
        case 6.0L:printf("Steyn");
               break;
        default:  printf("Smith");
    }
}
Choose all that apply:
(A)    Sangakkara   
(B)    Sehwag   
(C)    Steyn   
(D)    Smith   
(E)    Compilation error   
Explanation:e
10.
What will be output when you will execute following c code?
#include<stdio.h>
void main(){
    switch(0X0){
        case NULL:printf("Thierry Henry");
             break;
        case '\0':printf("Steven Gerrend");
             break;
        case 0: printf("Kaka");
             break;
        default: printf("Michael Ballack");
    }
}
Choose all that apply:
(A)    Thierry Henry   
(B)    Steven Gerrend   
(C)    Kaka   
(D)    Michael Ballack   
(E)    Compilation error   
Explanation:e

Sunday, March 4, 2012

C Placement Question

printf

(1)
#include<stdio.h>
#include<conio.h>
void main()
{
int a=5,b=6,c=11;
clrscr();
printf("%d %d %d");
getch();
}
What will output when you compile and run the above code?
(a)Garbage value garbage value garbage value
(b)5 6 11
(c)11 6 5
(d)Compiler error
Answer: (c)

(2)
#include<stdio.h>
void main()
{
char *str="CQUESTIONBANK";
clrscr();
printf(str+9);
getch();
}
What will output when you compile and run the above code?
(a)CQESTIONBANK
(b)CQUESTION
(c)BANK
(d)Compiler error
Answer: (c)
(3)
#include<stdio.h>
void main()
{
clrscr();
printf("%d",printf("CQUESTIONBANK"));
getch();
}
What will output when you compile and run the above code?
(a)13CQUESTIONBANK
(b)CQUESTIONBANK13
(c)Garbage CQUESTIONBANK
(d)Compiler error
Answer: (b)
(4)#include<stdio.h>
#include<conio.h>
void main()
{
short int a=5;
clrscr();
printf("%d"+1,a);
getch();
}
What will output when you compile and run the above code?(a)6
(b)51
(c)d
(d)Compiler error
Answer: (c)
(5)
#include<stdio.h>
void main()
{
int i=85;
clrscr();
printf("%p %Fp",i,i);
getch();
}
What will output when you compile and run the above code?
(a)85 85
(b)0055 034E:0055
(c)0055 FFFF:0055
(d)Compiler error
Answer: (b)

(6)#include<stdio.h>
static struct student
{
int a;
    int b;
    int c;
int d;
}s1={6,7,8,9},s2={4,3,2,1},s3;
void main()
{
s3=s1+s2;
clrscr();
printf("%d %d %d %d",s3.a,s3.b,s3.c,s3.d);
getch();
}
What will output when you compile and run the above code?(a)6789
(b)4321
(c)10101010
(d)Compiler error
Answer: (d)
(7)#include<stdio.h>
extern struct student
{
int a;
    int b;
    int c;
int d;
}s={6,7,8,9};
void main()
{
clrscr();
printf("%d %d %d %d",s.a,s.b,s.c,s.d);
getch();
}
What will output when you compile and run the above code?(a)6789
(b)9876
(c)0000
(d)Compiler error
Answer: (a)
(8)#include<stdio.h>
struct student
{
static int a;
register int b;
auto int c;
extern int d;
}s={6,7,8,9};
void main()
{
printf("%d %d % %d",s.a,s.b,s.c,s.d);
}
What will output when you compile and run the above code?(a)6789
(b)9876
(c)0000
(d)Compiler error
Answer: (d)
(9)#include<stdio.h>
struct student
{
int roll;
int cgpa;
int sgpa[8];
};
void main()
{
struct student s={12,8,7,2,5,9};
int *ptr;
ptr=(int *)&s;
clrscr();
printf("%d",*(ptr+3));
getch();
}
What will output when you compile and run the above code?(a)8
(b)7
(c)2
(d)Compiler error
Answer: (c)
(10)#include<stdio.h>
struct game
{
int level;
int score;
struct player
{
char *name;
}g2={"anil"};
}g3={10,200};
void main()
{
struct game g1=g3;
clrscr();
printf("%d  %d  %s",g1.level,g1.score,g1.g2.name);
getch();
}
What will output when you compile and run the above code?(a)10 200 anil
(b)200 10 anil
(c)10 200 null
(d)Compiler error
Answer: (d)
(11)#include<stdio.h>
struct game
{
int level;
int score;
struct player
{
char *name;
}g2;
}g1;
void main()
{
clrscr();
printf("%d  %d  %s",g1.level,g1.score,g1.g2.name);
getch();
}
What will output when you compile and run the above code?(a)Garbage_value garbage_value garbage_value
(b)0 0 (null)
(c)Run time error
(d)Compiler error
Answer: (b)