Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Matrix in snake pattern #115

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions Q41_Snake_pattern.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#include<iostream>
using namespace std;
// Print matrix in snake pattern
int main()
{
int n,m,i,j;
cout<<"Enter the count of row and column matrix: ";
cin>>m>>n;
int arr[m][n];
for(i=0;i<m;i++) //Rows
{
for(j=0;j<n;j++) //columns
{
cin>>arr[i][j];
}
}
cout<<"Snake pattern: ";
for(i=0;i<m;i++)
{
if(i%2==0) //If row is even then print as it is.
for(j=0;j<n;j++)
cout<<" "<<arr[i][j];
else
for(j=n-1;j>=0;j--) //If row is odd then print it in reverse format.
cout<<" "<<arr[i][j];
}
return 0;
}

29 changes: 29 additions & 0 deletions Q42_Decimal_to_Binary.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#include <iostream>
using namespace std;

// Program to convert decimal to binary
void dectobin(int n)
{
// array to store binary number
int binaryNum[64];
// counter for binary array
int i = 0;
while (n > 0)
{
// storing remainder in binary array
binaryNum[i] = n % 2;
n = n / 2;
i++;
}
// printing binary array in reverse order
for (int j = i - 1; j >= 0; j--)
cout << binaryNum[j];
}
int main()
{
int n;
cout<<"Enter the Decimal value: ";
cin>>n;
cout<<"Binary value : ";
dectobin(n);
}