0% found this document useful (0 votes)
16 views18 pages

Assignment 1 in C

C notes

Uploaded by

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

Assignment 1 in C

C notes

Uploaded by

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

1.

Solution :

#include<stdio.h>
#include<conio.h>
int main()
{
int n, i, j;
printf("Enter the number of rows: ");
scanf("%d",&n);
for(i = 1; i <= n; i++)
{
for(j = n; j > i; j--)
{
printf(" ");
}
for(j = 1; j <= i; j++)
{
printf("%d ",j);
}
printf("\n");
}
return 0;
}
2.

#include <stdio.h>

int main()
{
int i, j, N;

printf("Enter N: ");
scanf("%d", &N);

// Iterate through upper half triangle of the pattern


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

printf("\n");
}

// Iterate through lower half triangle of the pattern


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

printf("\n");
}

return 0;
}

3.

#include <stdio.h>

int main()
{
int rows, cols, i, j, k;

/* Input rows and columns from user */


printf("Enter number of rows: ");
scanf("%d", &rows);
printf("Enter number of columns: ");
scanf("%d", &cols);

k = 1;

for(i=1; i<=rows; i++)


{
for(j=1; j<=cols; j++)
{
if(k == 1)
{
printf("1");
}
else
{
printf("0");
}

// If k = 1 then k *= -1 => -1
// If k = -1 then k *= -1 => 1
k *= -1;
}

if(cols % 2 == 0)
{
k *= -1;
}

printf("\n");
}

return 0;
}
4.

#include <stdio.h>

int main()
{
int i, j, N;

printf("Enter rows: ");


scanf("%d", &N);

printf("*\n");
// Print the first upper half
for(i=1; i<=N; i++)
{
printf("*");
for(j=1; j<=i; j++)
{
printf("%d", j);
}

for(j=i-1; j>=1; j--)


{
printf("%d", j);
}
printf("*");

printf("\n");
}

// Print the lower half of the pattern


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

for(j=i-1; j>=1; j--)


{
printf("%d", j);
}
printf("*");

printf("\n");
}
printf("*");

return 0;
}

5.

#include <stdio.h>

int main()
{
int i, j, N;

printf("Enter N: ");
scanf("%d", &N);

// First part of the pattern


for(i=1; i<=N; i++)
{
// Print trailing spaces
for(j=1; j<i; j++)
{
printf(" ");
}

printf("%d", i);

// Print central spacces


for(j=1; j<=((N - i) * 2 - 1); j++)
{
printf(" ");
}

// Don't print for last row


if(i != N)
printf("%d", i);

// Moves on to the next row


printf("\n");
}

// Second part of the pattern


for(i=N-1; i>=1; i--)
{
// Print trailing spaces
for(j=1; j<i; j++)
{
printf(" ");
}

printf("%d", i);

// Print central spaces


for(j=1; j<=((N - i ) * 2 - 1); j++)
{
printf(" ");
}

printf("%d", i);

// Move on to the next line


printf("\n");
}

return 0;
}
6.

#include <stdio.h>

int main()
{
int i, j, N;

printf("Enter rows: ");


scanf("%d", &N);

for(i=1; i<=N; i++)


{
// Prints first part of pattern
for(j=1; j<=i; j++)
{
printf("%d", j);
}

// Prints spaces between two parts


for(j=i*2; j<N*2; j++)
{
printf(" ");
}

// Prints second part of the pattern


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

printf("\n");
}

return 0;
}

7.

#include <stdio.h>

//include space
void space(int size)
{
for (int i = 0; i < size; ++i)
{
printf(" ");
}
}
//Display upper layer of y pattern
void show_v(int size)
{
if (size < 2)
{
return;
}
int side = (size *2) - 3;
/*
In given below loop is display v pattern. for example size 3

* *
**
*
*/
for (int i = 0; i < size; i++)
{
space(i);
printf("A");
space(side - i);
if (i < side)
{
printf("A");
}
side--;
printf("\n");
}
}
//This method are handle the request to print Y pattern
void show_y(int size)
{
if (size < 3 || size % 2 == 0)
{
//Some invalid pattern size
return;
}
//Assuming the size odd number is greater than 3
printf("Size : %d\n\n", size);
//Calculate upper V pattern size
int i = (size / 2) + (size / 4) + 1;
show_v(i);
//Get remaining bottom rows
int j = size - i;
//This loop are print the bottom layer of y pattern
while (j > 0)
{
space(i - 1);
j--;
printf("A\n");
}
printf("\n");
}
int main()
{

show_y(7);

return 0;
}

8.
#include <stdio.h>

int main()

int i, j, k, n=3, x =1;

while(x<=3)
{
for(i=1;i<=n;i++)
{
for(j=1;j<=n-i;j++)
{
printf(" ");
}
for(k=1;k<=2*i-1;k++)
{
printf("*");
}
printf("\n");
}
x++;
}

for(i=1;i<=7;i++)
{
if(i<3)
{
for(j=1;j<=2;j++)
{
printf(" ");
}
printf("*\n");

}
else
printf("*");
}
}
9.

#include <stdio.h>

int main()

int i, j, n;

printf("Enter value of n : ");

scanf("%d", &n);

for(i=n/2; i<=n; i+=2)

for(j=1; j<n-i; j+=2)

printf(" ");
}

for(j=1; j<=i; j++)

printf("+");

for(j=1; j<=n-i; j++)

printf(" ");

for(j=1; j<=i; j++)

printf("+");

printf("\n");

for(i=n; i>=1; i--)

for(j=i; j<n; j++)

printf(" ");

for(j=1; j<=(i*2)-1; j++)

printf("+");

}
printf("\n");

return 0;

18.

#include <stdio.h>

int main()
{
int rows, cols, i, j, k;

/* Input rows and columns from user */


printf("Enter number of rows: ");
scanf("%d", &rows);
printf("Enter number of columns: ");
scanf("%d", &cols);

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

printf("\n");
}

return 0;
}

8.

#include <stdio.h>

int main()
{
int N, i, j;

printf("Enter N: ");
scanf("%d", &N);

// First upper half of the pattern


for(i=N; i>=1; i--)
{
// First inner part of upper half
for(j=N; j>i; j--)
{
printf("%d", j);
}

// Second inner part of upper half


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

// Third inner part of upper half


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

printf("\n");
}

// Second lower half of the pattern


for(i=1; i<N; i++)
{
// First inner part of lower half
for(j=N; j>i; j--)
{
printf("%d", j);
}

// Second inner part of lower half


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

// Third inner part of lower half


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

printf("\n");
}

return 0;
}

You might also like