Want to excel in java coding? Practice with these Java Programs examples with output and write any kind of easy or difficult programs in the java language
Given the number of rows and columns, the task is to print 1 and 0 in alternative columns in C, C++, and Python.
Examples:
Example1:
Input:
given number of rows =4 given number of columns=3
Output:
1 0 1 1 0 1 1 0 1 1 0 1
Example2:
Input:
given number of rows =7 given number of columns=15
Output:
1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1
- Program to Subtract two Matrices in Python & C++ Programming
- Python Program for Multiplication of Two Matrices | How do you do Matrix Multiplication in Python?
- Python Program to Print Hollow Square Star With Diagonals
Program to Print 1 and 0 in alternative Columns in C, C++, and Python
Below are the ways to print 1 and 0 in alternative columns in C, C++, and Python.
If you look closely at the pattern, you’ll see that for all odd columns, 1 is printed and for all even columns, 0 is printed.
As a result, before displaying integers inside the inner loop, you must check for even-odd conditions. If the current column is an odd number, print 1, otherwise, print 0.
The following is a step-by-step description of the logic used to print a 1, 0 number pattern at alternate columns.
Method #1: Using For loop
Approach:
- Give the number of rows and number of columns as static input.
- Store them in two separate variables row numbers and column numbers.
- Run an outer loop from 1 to rows to iterate through the rows using For loop.
- Iterate through the columns from 1 to cols using another For inner loop.
- Before printing any number, we must first check the condition inside the inner loop.
- This means that for every odd column, 1 is displayed, and for every even column, 0 is displayed.
- We check whether the column is odd or not using the if statement.
- If it is true then print 1 else print 0.
- The Exit of the Program.
1) Python Implementation
Below is the implementation:
# Give the number of rows and number of columns as static input. # Store them in two separate variables row numbers and column numbers. rownumbs = 15 colnumbs = 11 # Run an outer loop from 1 to rows to iterate through the rows using For loop. for m in range(1, rownumbs+1): # Iterate through the columns from 1 to cols using another For inner loop. for n in range(1, colnumbs+1): # Before printing any number, we must first check the condition inside the inner loop. # This means that for every odd column, 1 is displayed, and for every even column, 0 is displayed. # We check whether the column is odd or not using the if statement. if(n % 2 == 1): print('1', end=' ') # If it is true then print 1 else print 0. else: print('0', end=' ') print()
Output:
1 0 1 0 1 0 1 0 1 0 1 1 0 1 0 1 0 1 0 1 0 1 1 0 1 0 1 0 1 0 1 0 1 1 0 1 0 1 0 1 0 1 0 1 1 0 1 0 1 0 1 0 1 0 1 1 0 1 0 1 0 1 0 1 0 1 1 0 1 0 1 0 1 0 1 0 1 1 0 1 0 1 0 1 0 1 0 1 1 0 1 0 1 0 1 0 1 0 1 1 0 1 0 1 0 1 0 1 0 1 1 0 1 0 1 0 1 0 1 0 1 1 0 1 0 1 0 1 0 1 0 1 1 0 1 0 1 0 1 0 1 0 1 1 0 1 0 1 0 1 0 1 0 1 1 0 1 0 1 0 1 0 1 0 1
2) C++ Implementation
It is the same as the python approach but just the change in syntax.
Below is the implementation:
#include <iostream> using namespace std; int main() { // Give the number of rows and number of columns as // static input. // Store them in two separate variables row numbers and // column numbers. int rownumbs = 15; int colnumbs = 11; // Run an outer loop from 1 to rows to iterate through // the rows using For loop. for (int i = 1; i <= rownumbs; i++) { // Iterate through the columns from 1 to cols using // another For inner loop. for (int j = 1; j <= colnumbs; j++) { // Before printing any number, we must // first check the condition inside the // inner loop. This means that for every // odd column, 1 is displayed, and for // every even column, 0 is displayed. We // check whether the column is odd or // not using the if statement. if (j % 2 == 1) cout << "1 "; // If it is true then print 1 else print 0. else cout << "0 "; } cout << endl; } return 0; }
Output:
1 0 1 0 1 0 1 0 1 0 1 1 0 1 0 1 0 1 0 1 0 1 1 0 1 0 1 0 1 0 1 0 1 1 0 1 0 1 0 1 0 1 0 1 1 0 1 0 1 0 1 0 1 0 1 1 0 1 0 1 0 1 0 1 0 1 1 0 1 0 1 0 1 0 1 0 1 1 0 1 0 1 0 1 0 1 0 1 1 0 1 0 1 0 1 0 1 0 1 1 0 1 0 1 0 1 0 1 0 1 1 0 1 0 1 0 1 0 1 0 1 1 0 1 0 1 0 1 0 1 0 1 1 0 1 0 1 0 1 0 1 0 1 1 0 1 0 1 0 1 0 1 0 1 1 0 1 0 1 0 1 0 1 0 1
3) C implementation
It is the same as the python approach but just the change in syntax.
Below is the implementation:
#include <stdio.h> int main(void) { // Give the number of rows and number of columns as // static input. // Store them in two separate variables row numbers and // column numbers. int rownumbs = 15; int colnumbs = 11; // Run an outer loop from 1 to rows to iterate through // the rows using For loop. for (int i = 1; i <= rownumbs; i++) { // Iterate through the columns from 1 to cols using // another For inner loop. for (int j = 1; j <= colnumbs; j++) { // Before printing any number, we must // first check the condition inside the // inner loop. This means that for every // odd column, 1 is displayed, and for // every even column, 0 is displayed. We // check whether the column is odd or // not using the if statement. if (j % 2 == 1) printf("1 "); // If it is true then print 1 else print 0. else printf("0 "); } printf("\n"); } return 0; }
Output:
1 0 1 0 1 0 1 0 1 0 1 1 0 1 0 1 0 1 0 1 0 1 1 0 1 0 1 0 1 0 1 0 1 1 0 1 0 1 0 1 0 1 0 1 1 0 1 0 1 0 1 0 1 0 1 1 0 1 0 1 0 1 0 1 0 1 1 0 1 0 1 0 1 0 1 0 1 1 0 1 0 1 0 1 0 1 0 1 1 0 1 0 1 0 1 0 1 0 1 1 0 1 0 1 0 1 0 1 0 1 1 0 1 0 1 0 1 0 1 0 1 1 0 1 0 1 0 1 0 1 0 1 1 0 1 0 1 0 1 0 1 0 1 1 0 1 0 1 0 1 0 1 0 1 1 0 1 0 1 0 1 0 1 0 1
Method #2:Using while loop
1) C Implementation
We just iterate till rownumbs and columumbs using a while loop.
Below is the implementation:
#include <stdio.h> int main(void) { // Give the number of rows and number of columns as // static input. // Store them in two separate variables row numbers and // column numbers. int rownumbs = 4; int colnumbs = 3; // Run an outer loop from 1 to rows to iterate through // the rows using For loop. while (rownumbs > 0) { // Iterate through the columns from 1 to cols using // another For inner loop. int tempcol = 1; while (tempcol <= colnumbs) { // Before printing any // number, we must // first check the condition inside the // inner loop. This means that for every // odd column, 1 is displayed, and for // every even column, 0 is displayed. We // check whether the column is odd or // not using the if statement. if (tempcol % 2 == 1) printf("1 "); // If it is true then print 1 else print 0. else printf("0 "); tempcol++; } rownumbs--; printf("\n"); } return 0; }
2) C++ Implementation:
We just iterate till rownumbs and columumbs using a while loop.
Below is the implementation:
#include <iostream> using namespace std; int main() { // Give the number of rows and number of columns as // static input. // Store them in two separate variables row numbers and // column numbers. int rownumbs = 13; int colnumbs = 20; // Run an outer loop from 1 to rows to iterate through // the rows using For loop. while (rownumbs > 0) { // Iterate through the columns from 1 to cols using // another For inner loop. int tempcol = 1; while (tempcol <= colnumbs) { // Before printing any // number, we must // first check the condition inside the // inner loop. This means that for every // odd column, 1 is displayed, and for // every even column, 0 is displayed. We // check whether the column is odd or // not using the if statement. if (tempcol % 2 == 1) cout << "1 "; // If it is true then print 1 else print 0. else cout << "0 "; tempcol++; } rownumbs--; cout << endl; } return 0; }
Output:
1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0
3) Python Implementation
We just iterate till rownumbs and columumbs using a while loop.
Below is the implementation:
# Give the number of rows and number of columns as static input. # Store them in two separate variables row numbers and column numbers. rownumbs = 6 colnumbs = 11 # Run an outer loop from 1 to rows to iterate through the rows using For loop. while(rownumbs > 0): # Iterate through the columns from 1 to cols using another For inner loop. tempcol = 1 while(tempcol <= colnumbs): # Before printing any number, we must first check the condition inside the inner loop. # This means that for every odd column, 1 is displayed, and for every even column, 0 is displayed. # We check whether the column is odd or not using the if statement. if(tempcol % 2 == 1): print('1', end=' ') # If it is true then print 1 else print 0. else: print('0', end=' ') tempcol += 1 rownumbs -= 1 print()
Output:
1 0 1 0 1 0 1 0 1 0 1 1 0 1 0 1 0 1 0 1 0 1 1 0 1 0 1 0 1 0 1 0 1 1 0 1 0 1 0 1 0 1 0 1 1 0 1 0 1 0 1 0 1 0 1 1 0 1 0 1 0 1 0 1 0 1
Related Programs:
- python program to print 1 and 0 in alternative rows
- python program to print largest even and largest odd number in a list
- program to print collatz conjecture for a given number in cpp and python
- python program to print sum of negative numbers positive even numbers and positive odd numbers in a list
- python program to generate a dictionary that contains numbers between 1 and n in the form x xx
- python program to read a text file and print all the numbers present in the text file
- python program to accept a hyphen separated sequence of words as input and print the words in a hyphen separated sequence after sorting them alphabetically