C

Odd Or Even Triangle Pattern

57
0
Home C Odd Or Even Triangle Pattern

The program must accept an integer N as the input. The program must print the desired pattern as shown in the Example Input/Output section

Boundary Condition(s):
3 < N < 100

Input Format:
The first line contains N.

Output Format:
The lines containing the desired pattern as shown in the Example Input/Output section.

Example Input/Output 1:
Input:

5
Output:
1*3*5
-7*9
–11

Example Input/Output 2:
Input

8
Output
2*4*6*8

-10*12*14

–16*18
—20

#include<stdio.h> 
#include<stdlib.h>

int main()
{
	int n,k,t,l=0; 
	scanf("%d",&n); 
	int g;
	if(n%2==0)
	{
		k=t=2; 
		while(t<=n)
		{
			l++; 
			t+=2;
		}
		g=l;
		for(int i=0;i<g;i++)
		{
			for (int j=0;j<i;j++)
				printf("-");
			for (int f=0;f<l-i;f++)
			{
				if(f<l-1-i) 
					printf("%d*",k);
				else 
					printf("%d",k);
					k+=2;
			}	
			printf("\n");
		}
	}
	else
	{
		k=t=1;
		while(t<=n)
		{
			t+=2;
			l++;
		}
		g=l;
		for (int i=0;i<g;i++)
		{
			for (int j=0;j<i;j++)
				printf("-");
			for (int f=0;f<l-i;f++)
			{
				if(f<l-i-1) 
					printf("%d*",k); 
				else
					printf("%d",k); 
				k+=2;
			}
			printf("\n");
		}
	}
}
Hephzibai
WRITTEN BY

Hephzibai

Hephzibai is a driven third-year student at St. Joseph's Institute of Technology, specializing in Computer Science and Engineering. With a keen interest in data science and also in fullstack.
One of my greatest strengths lies in my programming skills, which I've honed through countless hours of practice and participation in coding challenges. Platforms like Skillrack, HackerRank, and others have been my playgrounds, where I've tackled a wide range of problems with creativity and determination.

Leave a Reply

Your email address will not be published. Required fields are marked *