Pages

Showing posts with label C. Show all posts
Showing posts with label C. Show all posts

Thursday, 11 May 2017

Print the word with odd letters (Asked in Zoho Interview | Set 1 Question 3)

1. Print the word with odd letters as
P         M
 R      A
   O  R
     G
  O    R
 R       A
P          M 

#include<stdio.h>
#include<string.h>
int main()
{
char s[10]="PROGRAM",s1[10];
int l,i,j;
//scanf("%s",&s);
strcpy(s1,s);
//strrev(s1);
l=strlen(s);
for(i=0;i<l;i++)
{
for(j=0;j<l;j++)
{
if(i==j)
  printf("%c",s[i]);
else if(i+j==l-1)
  printf("%c",s1[i]);
if(j==l-1)
  printf("\n");
else
  printf(" ");
}
}
return 0;
}

input:

PROGRAM


output:


P      P
 R    R 
  O  O  
   G   
  R  R  
 A    A 
M      M

Wednesday, 10 May 2017

Nested printf (printf inside printf) in C (Asked in Zoho Interview)

Predict the output of the following C program with a printf inside printf.
#include<stdio.h>
  
int main()
{
   int x = 1987;
   printf("%d", printf("%d", printf("%d", x)));
   return(0);
}

Output :

198741
Explanation :
1. Firstly, the innermost printf is executed which results in printing 1987
2. This printf returns total number of digits in 1987 i.e 4. printf() returns number of characters successfully printed on screen. The whole statement reduces to :
printf("%d", printf("%d", 4));

3. The second printf then prints 4 and returns the total number of digits in 4 i.e 1 (4 is single digit number ).
4. Finally, the whole statement simply reduces to :

printf("%d", 1);

5. It simply prints 1 and output will be :
Output:

198741
So, when multiple printf’s appear inside another printf, the inner printf prints its output and returns length of the string printed on the screen to the outer printf.                                                                     

Code Review

 SOLID Principles S – Single Responsibility Principle There should never be more than one reason for a class to change. O – Open-Closed Prin...