To print the pattern like
for n=3
the program should print
1 1 1 2
3 2 2 2
3 3 3 4
The solution in c++:
#include <iostream>
using namespace std;
int main()
{
int n=3,c=n-1;
for(int i=1;i<=n;i++)
{
if(i%2==0)
cout<<c++;
for(int j=1;j<=n;j++)
{
cout<<i;
}
if(i%2!=0)
cout<<c++;
cout<<"\n";
}
return 0;
}
for n=3
the program should print
1 1 1 2
3 2 2 2
3 3 3 4
The solution in c++:
#include <iostream>
using namespace std;
int main()
{
int n=3,c=n-1;
for(int i=1;i<=n;i++)
{
if(i%2==0)
cout<<c++;
for(int j=1;j<=n;j++)
{
cout<<i;
}
if(i%2!=0)
cout<<c++;
cout<<"\n";
}
return 0;
}
can u pls give the c code with this program
ReplyDelete#include
ReplyDeleteint main(void) {
int i,j,n=3,c=n-1;
for(i=1;i<=n;i++)
{
if(i%2==0)
printf("%d",c++);
for(j=1;j<=n;j++)
{
printf("%d",i);
}
if(i%2!=0)
printf("%d",c++);
printf("\n");
}
return 0;
}
Is this program is for printing 1112
Delete3222
3334
In C LANGUAGE??
/******************************************************************************
DeleteOnline C Compiler.
Code, Compile, Run and Debug C program online.
Write your code in this editor and press "Run" button to compile and execute it.
*******************************************************************************/
#include
int main()
{
int i,j,n;
scanf("%d",&n);
for(i=1;i<=n;i++)
{
if(i%2!=0)
{
for(j=1;j<=n+1;j++)
{
if(j<=n)
printf("%d",i);
else
printf("%d",i+1);
}
}
else if(i%2==0)
{
for(j=1;j<=n+1;j++)
{
if(j==1)
printf("%d",i+1);
else
printf("%d",i);
}
}
printf("\n");
}
return 0;
}
thanks for the code
Deletethis program works only for n=3
ReplyDeleteif you want to work for n numbers put c=2
this program is showing incorrect o/p
ReplyDelete#include
ReplyDeleteint main(void) {
int i,j,n=3,c=n-1,rows;
printf("Enter the number of rows");
scanf("%d",&rows);
for(i=1;i<=rows;i++)
{
if(i%2==0)
printf("%d",c++);
for(j=1;j<=n;j++)
{
printf("%d",i);
}
if(i%2!=0)
printf("%d",c++);
printf("\n");
}
return 0;
}
For more similar coding questions of amcat visit http://www.qsoln.com/amcat-coding/
ReplyDelete#include
ReplyDeletevoid main()
{
int i,j,n,k;
printf("Enter the nos line u want to print :");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
for(j=1;j<=4;j++)
{
if(i%2)
{
if(j<4)
printf("%d",i);
else{
k=i;
printf("%d",++k);
}
}
else{
if(j==1)
{
k=i;
printf("%d",++k);
}
else
printf("%d",i);
}
}
printf("\n");
}
}
That was a easy logic to solve but someone is saying to put c=2 its not 2 sir its ihe value just befire the value of n thatis n-1 thatswhy c=n-1 if u will put c=2 the the output will be same for every input
ReplyDelete