0% found this document useful (0 votes)
33 views6 pages

Demo 5

The document contains examples of using for loops to print patterns in C code. It includes nested for loops to print rows and columns with different conditions. Various patterns are demonstrated such as diagonal lines, filled boxes, alternating numbers and more.

Uploaded by

phubase.san
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views6 pages

Demo 5

The document contains examples of using for loops to print patterns in C code. It includes nested for loops to print rows and columns with different conditions. Various patterns are demonstrated such as diagonal lines, filled boxes, alternating numbers and more.

Uploaded by

phubase.san
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 6

#include <stdio.

h>
#include <stdlib.h>
#include <string.h>
#define M 4
#define N 5

int main()
{
//----------demo1------------//
/*
int total=10;

for(int i=0;i<5;i++)
{
total++;
printf("%d %d\n", i , total);
}
*/

/*
//75K 76L 77M 78N 79O 80P
char total = 'P'; //total=79
char i='L'; //76
while(total>(i-1)){ //80>75
total--;
printf("%d %d\n", i , total);
}
*/

/*
int total=10;
do{
total = total-3;
printf("%d\n", total); //total =7 4 1 -2
}while (total >= 0);
*/

//----------demo2------------//
/*
for(int i=0; i<M;i++){
printf("Row %d: ", i);
for(int j=0; j<M; j++){
printf("Col %d ", j);
}
printf("\n");
}
*/

//----------demo3------------//
// Repeat N times
/*
for (int i=0 ; i<N ; i++) { //N=5
for (int j=0 ; j<10 ; j++) { // Print from 0 to 9
if(j%2==0){
printf("%d ", j);
}
}
printf("\n");
}
*/

//----------demo4------------//
/*
int n =5;
for(int i=0; i<n; i++){ //row
for(int j=9;j>=0;j--){ //col
printf("%d ", j);
}
printf("\n");
}
*/

//----------demo5------------//
/*
int n=5;
for(int i=0;i<n;i++){
for(int j=0;j<10;j++) {
printf("A ");
}
printf("\n");
}
*/

printf("\n");
//----------demo6------------//
/*
int n=5;
for(int i=0;i<n;i++){
for(int j=0; j<10;j++){
printf("%d ",i);
}
printf("\n");
}
*/

//----------demo7------------//
/*
int n=5;
for(int i=0;i<n;i++){//0,1,2,3,4
for(int j=0;j<10;j++){
if(j%2==0){
printf("%d ",0);
}else{
printf("%d ",1);
}
}
printf("\n");
}
*/
//----------demo8------------//
/*
for(int i=0;i<10;i++){
for(int j=0; j<=i;j++){
printf("%d ",i);
}
printf("\n");
}
*/

//or
/*
int n=10;
for(int i=1;i<=n;i++){ //row
for(int j=0; j<i; j++){ //col: (j=1, j<=i)
printf("%d ",i-1);
}
printf("\n");
}
*/
/*
//or
int n=10;
for(int i=0;i<n;i++){ //row
for(int j=0; j<i+1; j++){ //col: (j=1, j<=i)
printf("%d ",i);
}
printf("\n");
}
*/

//----------demo9------------//
/*
int n=5;
for(int i =1; i<=n; i++){
for(int j=1;j<=i; j++){
if(i%2==0){
printf("%d ",1);
}else{
printf("%d ",0);
}
}
printf("\n");
}
*/
/*
//or --> give the structure and let students figure out what to put
for(int i =0; i<n; i++){
for(int j=0;j<i+1; j++){
if(i%2 ==0){
printf("0 ");
}else{
printf("1 ");
}
}
printf("\n");
}
*/

//----------demo10------------//
/*
int n=5;
for (int i=1; i<=n; i++){
for(int j=n; j>=i; j--){ //j=5, i=5
if(i%2 == 1){
printf("0 ");
}else{
printf("1 ");
}
}
printf("\n");
}
*/
//or

/*
//int n=5;
for(int i=0; i<n; i++){
for(int j=n; j>i; j--){
if(i%2==0){
printf("0 ");
}else{
printf("1 ");
}
}
printf("\n");
}
*/

//page31-33
/*
int n=5, i, j, k;
for(i=1; i<=n; i++){

// Print sequence numbers


for (j=1 ; j<=i ; j++)
{
printf("%d ", j-1);
}

// Print -
for (k=n-i ; k>=1 ; k--)
{
printf("- ");
}
printf("\n");
}
*/
//----------demo11------------//
// Same structure as the example
/*
int n=5, i, j, k;
for(i=1; i<=n; i++){
// Print _
for (j=1 ; j<=i ; j++) {
printf("_ ");
}

// Print numbers
for (k=n-i ; k>=1 ; k--) {
printf("%d ", i); // Number as a row number
}
printf("\n");
}
*/

//----------demo12------------//
/*
int n=5;
for(int i=1; i<=n; i++){
for(int j=i-1; j>=1; j--){ //j=0, j>=1
printf("- ");
}
for(int k=i; k<=n; k++){ //row1: 3---5
printf("%d ",i);
}
printf("\n");
}
*/

//----------demo13------------//
/*
int n=5;
for(int i=1; i<=n; i++){
for(int j=i-1; j>=1; j--){
printf("- ");
}
for(int k=i; k<=n; k++){ //k=2, 2<=5->T
printf("%d ", k); //1 2
}

printf("\n");
}
*/
return 0;
}

You might also like