To print the trapezium pattern.
for example , we have num=4
the output should be like
1*2*3*4*17*18*19*20
- -5*6*7*14*15*16
- - - -8*9*12*13
- - - - - -10*11
the solution in c++ is:
#include<iostream>
using namespace std;
int main(){
int n=4,num=1,i=1,space=0,k=1,number=n;
for(i=0;i<n;i++)
{
for(int j=1;j<=space;j++)
{
cout<<"-";
}
for(int m=1;m<2*n-space;m++)
{
if(m%2==0)
cout<<"*";
else
cout<<num++;
}
cout<<"*";
for(int l=1;l<2*n-space;l++)
{
if(l%2==0)
cout<<"*";
else
{
cout<<k+number*number;
k++;
}
}
number--;
space=space+2;
cout<<endl;
}
return 0;
}
for example , we have num=4
the output should be like
1*2*3*4*17*18*19*20
- -5*6*7*14*15*16
- - - -8*9*12*13
- - - - - -10*11
the solution in c++ is:
#include<iostream>
using namespace std;
int main(){
int n=4,num=1,i=1,space=0,k=1,number=n;
for(i=0;i<n;i++)
{
for(int j=1;j<=space;j++)
{
cout<<"-";
}
for(int m=1;m<2*n-space;m++)
{
if(m%2==0)
cout<<"*";
else
cout<<num++;
}
cout<<"*";
for(int l=1;l<2*n-space;l++)
{
if(l%2==0)
cout<<"*";
else
{
cout<<k+number*number;
k++;
}
}
number--;
space=space+2;
cout<<endl;
}
return 0;
}
can u provide this in c
ReplyDeleteyeah sure..here is the solution.
Delete#include
int main(){
int n=4,num=1,i=1,space=0,k=1,number=n;
for(i=0;i<n;i++)
{
for(int j=1;j<=space;j++)
{
printf("-");
}
for(int m=1;m<2*n-space;m++)
{
if(m%2==0)
printf("%s","*");
else
printf("%d",num++);
}
printf("%s","*");
for(int l=1;l<2*n-space;l++)
{
if(l%2==0)
printf("%s","*");
else
{
printf("%d",k+number*number);
k++;
}
}
number--;
space=space+2;
printf("\n");
}
return 0;
}
Can you please explain the logic?
DeleteCan you provide the same in java
ReplyDeletepublic class TrapeziumPattern {
ReplyDeletepublic static void Trapezium(){
int n=4, i=1,k=1,s=0,num=1,number=n;
for(i=0;i<n;i++){
System.out.println();
for(int j=1;j<=s;j++){
System.out.print("-");
}
for(int m=1;m<2*n-s;m++)
{
if(m%2==0)
System.out.print("*");
else
System.out.print(num++);
}
System.out.print("*");
for(int l=1;l<2*n-s;l++)
{
if(l%2==0)
System.out.print("*");
else
{
System.out.print(k+number*number);
k++;
}
}
number--;
s=s+2;
//System.out.println();
}
//return 0;
}
public static void main(String[] args) {
TrapeziumPattern.Trapezium();
}
}
C++ code to print any Number trapezium
ReplyDelete#include
using namespace std;
int main()
{
int rows;
cout<<"Enter the no of rows";
cin>>rows;
int number=0;
int number1=0;
int temp=rows;
while(temp>0)
{
number1+=temp;
temp--;
}
number1=number1*2;
for(int i=1;i<=rows;i++)
{
for(int j=i;j>1;j--)
cout<<" ";
for( int k=rows;k>=i;k--)
{
cout<<++number;
if(k!=0)
cout<<"*";
}
int get=number1-(rows-i);
for(int l=i;l<=rows;l++)
{
cout<<get++;
if(l!=rows)
{
cout<<"*";
}
if(l==rows)
{
number1=number1-(rows-i);
number1--;
}
}
cout<<"\n";
}
return 0;
}
pls give this by using function in c++
ReplyDeleteCan u plz provide the same in python
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteCan u provide for inverted trapezium in c
ReplyDeleteLike
---10 11
--8 9 12 13
-5 6 7 14 15 16
1 2 3 4 17 18 19 20
please any one give explanation about this program execution
ReplyDelete