ALLInterview.com :: Home Page KalAajKal.com
 Advertise your Business Here     
Browse  |   Placement Papers  |   Company  |   Code Snippets  |   Certifications  |   Visa Questions
Post Question  |   Post Answer  |   My Panel  |   Search  |   Articles  |   Topics  |   ERRORS new
   Refer this Site  Refer This Site to Your Friends  Site Map  Bookmark this Site  Set it as your HomePage  Contact Us     Login  |  Sign Up                      
info       Did you received any Funny E-Mails from your Friends and like to share with rest of our friends? Yeah!! you can post that stuff   HERE
Google
 
Categories  >>  Software  >>  Programming Languages  >>  C
 
 


 

 
 C interview questions  C Interview Questions
 C++ interview questions  C++ Interview Questions
 VC++ interview questions  VC++ Interview Questions
 Delphi interview questions  Delphi Interview Questions
 Programming Languages AllOther interview questions  Programming Languages AllOther Interview Questions
Question
print the following using nested for loop.
5 4 3 2 1
1 2 3 4
3 2 1
1 2
1
2 1
1 2 3
4 3 2 1
1 2 3 4 5
 Question Submitted By :: Nithya
I also faced this Question!!     Rank Answer Posted By  
 
  Re: print the following using nested for loop. 5 4 3 2 1 1 2 3 4 3 2 1 1 2 1 2 1 1 2 3 4 3 2 1 1 2 3 4 5
Answer
# 1
i have used functions.... i got this logic.

#include<stdio.h>
#include<conio.h>
void logic1(int);
void logic2();
void logic3();
void logic4();
int a=0;
void main()
{
int m;
printf("enter the number of lines :");
scanf("%d",&m);
for(int i=1;i<=(m/2+1);i++)
{
printf("\n");
if(i%2!=0)
logic1(i);
else
logic2();
}
for(i=1;i<=m/2;i++)
{
printf("\n");
if(i%2==0)
logic3();
else
logic4();
}
getch();
}
void logic1(int p)
{
a=(m/2+2)-p;
for(int i=a;i>=1;i--)
printf("%d",i);
}
void logic2();
{
a--;
for(int i=1;i<=a;i++)
printf("%d",i);
}
void logic3()
{
a++;
for(int i=a;i>=1;i++)
printf("%d",i);
}
void logic4()
{
a++;
for(int i=1;i<=a;i++)
printf("%d",i);
}
 
Is This Answer Correct ?    0 Yes 1 No
Vignesh1988i
 
  Re: print the following using nested for loop. 5 4 3 2 1 1 2 3 4 3 2 1 1 2 1 2 1 1 2 3 4 3 2 1 1 2 3 4 5
Answer
# 2
this is the correct logic..... some syntax mistakes was done
befoe ... nowq it's correct.... thank you

#include<stdio.h>
#include<conio.h>
void logic1(int,int);
void logic2();
void logic3();
void logic4();
int a=0;
void main()
{
int m;
printf("enter the number of lines :");
scanf("%d",&m);
for(int i=1;i<=(m/2+1);i++)
{
printf("\n");
if(i%2!=0)
logic1(i,m);
else
logic2();
}
for(i=1;i<=m/2;i++)
{
printf("\n");
if(i%2!=0)
logic3();
else
logic4();
}
getch();
}
void logic1(int p,int m)
{
a=(m/2+2)-p;
for(int i=a;i>=1;i--)
printf("%d",i);
}
void logic2();
{
a--;
for(int i=1;i<=a;i++)
printf("%d",i);
}
void logic3()
{
a++;
for(int i=a;i>=1;i++)
printf("%d",i);
}
void logic4()
{
a++;
for(int i=1;i<=a;i++)
printf("%d",i);
}
 
Is This Answer Correct ?    0 Yes 2 No
Vignesh1988i
 
 
 
  Re: print the following using nested for loop. 5 4 3 2 1 1 2 3 4 3 2 1 1 2 1 2 1 1 2 3 4 3 2 1 1 2 3 4 5
Answer
# 3
#include<stdio.h>
#include<conio.h>

void main()
{
	for(int i=1; i<=2; i++)
		for(int j=(i==1)?1:2;j<=5;j++)
		{
			for(int k = (j%2==1 && i==1)? 5 + 1 -j: (j%2==0 && i==2)?
j:1; (j%2==1 && i==1)? k>=1: (j%2==0 && i==2)? k>=1:(j%2==0
&& i==1)?k<=5 + 1 -j:k<=j ;(j%2==1 && i==1)? k--: (j%2==0 &&
i==2)? k--:k++)
				printf("%d", k);
			printf("\n");
		}
}
 
Is This Answer Correct ?    3 Yes 1 No
Anand.dayalan@gmail.com
 
  Re: print the following using nested for loop. 5 4 3 2 1 1 2 3 4 3 2 1 1 2 1 2 1 1 2 3 4 3 2 1 1 2 3 4 5
Answer
# 4
DIFFERENT LOGIC

#include<stdio.h>
#include<conio.h>
void main()
{
int m,n,p;
printf("enter the limit value");
scanf("%d",&m);
n=m+1;
for(int i=1;i<2*m-1;i++)
{
   if(i<=m)
{
n--;
p=0;
}
else
{
n++
p=1;
}
if(i%2==p)
{
for(int j=1;j<n;j++)
printf("%d",j);
}
else
{
for(j=n;j>=1;j--)
printf("%d",j);
}
}
getch();
}
 
Is This Answer Correct ?    0 Yes 3 No
Vignesh1988i
 
  Re: print the following using nested for loop. 5 4 3 2 1 1 2 3 4 3 2 1 1 2 1 2 1 1 2 3 4 3 2 1 1 2 3 4 5
Answer
# 5
#include<stdio.h>

int main()
{
  int i,j,n=5;

 for(i=n;i>=1;i--)
 {
  if(i%2 == 0){
  for(j=1;j<=i;j++)
  printf("%d",j);
  printf("\n");
  }
  else
  {
   for(j=i;j>=1;j--)
   printf("%d",j);
   printf("\n");
  }
 }
for(i=2;i<=5;i++)
{
  if(i%2 != 0) {
  for(j=1;j<=i;j++)
  printf("%d",j);
  printf("\n");
  }
  else
  {
  for(j=i;j>=1;j--)
  printf("%d",j);
  printf("\n");
  }
}
}
 
Is This Answer Correct ?    2 Yes 0 No
Dally
 

 
 
 
Other C Interview Questions
 
  Question Asked @ Answers
 
fun(int x) { if(x > 0) fun(x/2); printf("%d", x); } above function is called as: fun(10); what will it print? } NDS13
size maximum allocated by calloc() DELL1
write a program to display the array elements in reverse order in c language  6
design and implement a program that reads floating-points numbers in a sentinel-controlled loop until the user terminates the program by entering zero.your program should determinate and print the smallest,largest and average of the supplied numbers.  1
program to find the roots of a quardratic equation  1
int arr[] = {1,2,3,4} int *ptr=arr; *(arr+3) = *++ptr + *ptr++; Final contents of arr[] Hughes4
How do I declare a pointer to an array?  5
What is an object?  3
HOW CAN ADD OUR FUNCTION IN LIBRARY.  5
#include main() { int i=1,j=2; switch(i) { case 1: printf("GOOD"); break; case j: printf("BAD"); break; } } ME5
18)struct base {int a,b; base(); int virtual function1(); } struct derv1:base{ int b,c,d; derv1() int virtual function1(); } struct derv2 : base {int a,e; } base::base() { a=2;b=3; } derv1::derv1(){ b=5; c=10;d=11;} base::function1() {return(100); } derv1::function1() { return(200); } main() base ba; derv1 d1,d2; printf("%d %d",d1.a,d1.b) o/p is a)a=2;b=3; b)a=3; b=2; c)a=5; b=10; d)none 19) for the above program answer the following q's main() base da; derv1 d1; derv2 d2; printf("%d %d %d",da.function1(),d1.function1(),d2.function1 ()); o/p is a)100,200,200; b)200,100,200; c)200,200,100; d)none 20)struct { int x; int y; }abc; you can not access x by the following 1)abc-->x; 2)abc[0]-->x; abc.x; (abc)-->x; a)1,2,3 b)2&3 c)1&2 d)1,3,4  1
hat is a pointer? Assurgent3
how to implement stack work as a queue?  2
what is the difference between #include<stdio.h> and #include "stdio.h" ?  2
main() { int x=10,y=15; x=x++; y=++y; printf("%d %d\n",x,y); } output?? Ramco13
What are advantages and disadvantages of recursive calling ? HP11
what is the output for the code : main() { int i,j; printf("%d %d ",scanf("%d%d",&i,&j)); } Infosys15
If we give two names then this displays the connection between the two people. It is nothing but flames game  1
1.write a program to merge the arrays 2.write efficient code for extracting unique elements from a sorted list of array? Qualcomm2
When is an interface "good"?  1
 
For more C Interview Questions Click Here 
 
 
 
 
 
   
Copyright Policy  |  Terms of Service  |  Help  |  Site Map 1  |  Articles  |  Site Map  |   Site Map  |  Contact Us interview questions urls   External Links 
   
Copyright © 2007  ALLInterview.com.  All Rights Reserved.

ALLInterview.com   ::  Forum9.com   ::  KalAajKal.com