Programs To Print Triangles Using, Numbers and Characters

Download as pdf or txt
Download as pdf or txt
You are on page 1of 28

List of Source Code

Code to print triangles using *, digits and characters


Code to print inverted triangles using * and digits
Code to print full pyramids
Code to print Pascal's triangle
Code to print Floyd's triangle

Programs to print triangles using *, numbers and characters

Example 1: Program to print half pyramid using *


*
* *
* * *
* * * *
* * * * *

Source Code

#include <stdio.h>
int main()
{
int i, j, rows;

printf("Enter number of rows: ");


scanf("%d",&rows);

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


{
for(j=1; j<=i; ++j)
{
printf("* ");
}
printf("\n");
}
return 0;
}

Example 2: Program to print half pyramid a using numbers


1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
Source Code

#include <stdio.h>
int main()
{
int i, j, rows;

printf("Enter number of rows: ");


scanf("%d",&rows);

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


{
for(j=1; j<=i; ++j)
{
printf("%d ",j);
}
printf("\n");
}
return 0;
}

Example 3: Program to print half pyramid using alphabets


A
B B
C C C
D D D D
E E E E E

Source Code

#include <stdio.h>
int main()
{
int i, j;
char input, alphabet = 'A';

printf("Enter the uppercase character you want to print in last row: ");
scanf("%c",&input);

for(i=1; i <= (input-'A'+1); ++i)


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

printf("\n");
}
return 0;
}
Programs to print inverted half pyramid using * and
numbers

Example 4: Inverted half pyramid using *


* * * * *
* * * *
* * *
* *
*

Source Code

#include <stdio.h>
int main()
{
int i, j, rows;

printf("Enter number of rows: ");


scanf("%d",&rows);

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


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

return 0;
}

Example 5: Inverted half pyramid using numbers


1 2 3 4 5
1 2 3 4
1 2 3
1 2
1

Source Code

#include <stdio.h>
int main()
{
int i, j, rows;
printf("Enter number of rows: ");
scanf("%d",&rows);

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


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

return 0;
}

Programs to display pyramid and inverted pyramid using *


and digits

Example 6: Program to print full pyramid using *


*
* * *
* * * * *
* * * * * * *
* * * * * * * * *

Source Code

#include <stdio.h>
int main()
{
int i, space, rows, k=0;

printf("Enter number of rows: ");


scanf("%d",&rows);

for(i=1; i<=rows; ++i, k=0)


{
for(space=1; space<=rows-i; ++space)
{
printf(" ");
}

while(k != 2*i-1)
{
printf("* ");
++k;
}

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

Example 7: Program to print pyramid using numbers

1
2 3 2
3 4 5 4 3
4 5 6 7 6 5 4
5 6 7 8 9 8 7 6 5

Source Code

#include <stdio.h>
int main()
{
int i, space, rows, k=0, count = 0, count1 = 0;

printf("Enter number of rows: ");


scanf("%d",&rows);

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


{
for(space=1; space <= rows-i; ++space)
{
printf(" ");
++count;
}

while(k != 2*i-1)
{
if (count <= rows-1)
{
printf("%d ", i+k);
++count;
}
else
{
++count1;
printf("%d ", (i+k-2*count1));
}
++k;
}
count1 = count = k = 0;

printf("\n");
}
return 0;
}
Example 8: Inverted full pyramid using *

* * * * * * * * *
* * * * * * *
* * * * *
* * *
*

Source Code

#include<stdio.h>
int main()
{
int rows, i, j, space;

printf("Enter number of rows: ");


scanf("%d",&rows);

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


{
for(space=0; space < rows-i; ++space)
printf(" ");

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


printf("* ");

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


printf("* ");

printf("\n");
}

return 0;
}

Example 9: Print Pascal's triangle

1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1

Source Code

#include <stdio.h>
int main()
{
int rows, coef = 1, space, i, j;
printf("Enter number of rows: ");
scanf("%d",&rows);

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


{
for(space=1; space <= rows-i; space++)
printf(" ");

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


{
if (j==0 || i==0)
coef = 1;
else
coef = coef*(i-j+1)/j;

printf("%4d", coef);
}
printf("\n");
}

return 0;
}

Example 10: Print Floyd's Triangle.


1
2 3
4 5 6
7 8 9 10

Source Code

#include <stdio.h>
int main()
{
int rows, i, j, number= 1;

printf("Enter number of rows: ");


scanf("%d",&rows);

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


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

printf("\n");
}

return 0;
}
Example #2: C if...else statement
// Program to check whether an integer entered by the user is odd or even

#include <stdio.h>
int main()
{
int number;
printf("Enter an integer: ");
scanf("%d",&number);

// True if remainder is 0
if( number%2 == 0 )
printf("%d is an even integer.",number);
else
printf("%d is an odd integer.",number);
return 0;
}

Output

Enter an integer: 7
7 is an odd integer.

for Loop
The syntax of for loop is:

for (initializationStatement; testExpression; updateStatement)


{
// codes
}

How for loop works?

The initialization statement is executed only once.

Then, the test expression is evaluated. If the test expression is false (0), for loop is terminated.
But if the test expression is true (nonzero), codes inside the body of for loop is executed and the
update expression is updated.

This process repeats until the test expression is false.


The for loop is commonly used when the number of iterations is known.

To learn more on test expression (when test expression is evaluated to nonzero (true) and 0
(false)), check out relational and logical operators.

for loop Flowchart

Example: for loop


// Program to calculate the sum of first n natural numbers
// Positive integers 1,2,3...n are known as natural numbers
#include <stdio.h>
int main()
{
int num, count, sum = 0;

printf("Enter a positive integer: ");


scanf("%d", &num);

// for loop terminates when n is less than count


for(count = 1; count <= num; ++count)
{
sum += count;
}

printf("Sum = %d", sum);

return 0;
}

Output

Enter a positive integer: 10


Sum = 55

The factorial of a positive number n is given by:

factorial of n (n!) = 1*2*3*4....n

The factorial of a negative number doesn't exist. And, the factorial of 0 is 1, 0! = 1

Example: Factorial of a Number


#include <stdio.h>
int main()
{
int n, i;
unsigned long long factorial = 1;

printf("Enter an integer: ");


scanf("%d",&n);

// show error if the user enters a negative integer


if (n < 0)
printf("Error! Factorial of a negative number doesn't exist.");

else
{
for(i=1; i<=n; ++i)
{
factorial *= i; // factorial = factorial*i;
}
printf("Factorial of %d = %llu", n, factorial);
}

return 0;
}

Output

Enter an integer: 10
Factorial of 10 = 3628800

Syntax of break statement


break;

The simple code above is the syntax for break statement.

Flowchart of break statement


How break statement works?
Example #1: break statement
// Program to calculate the sum of maximum of 10 numbers
// Calculates sum until user enters positive number

# include <stdio.h>
int main()
{
int i;
double number, sum = 0.0;

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


{
printf("Enter a n%d: ",i);
scanf("%lf",&number);

// If user enters negative number, loop is terminated


if(number < 0.0)
{
break;
}

sum += number; // sum = sum + number;


}

printf("Sum = %.2lf",sum);

return 0;
}

Output

Enter a n1: 2.4


Enter a n2: 4.5
Enter a n3: 3.4
Enter a n4: -3
Sum = 10.30

This program calculates the sum of maximum of 10 numbers. It's because, when the user enters
negative number, the break statement is executed and loop is terminated.

In C programming, break statement is also used with switch...case statement.

continue Statement
The continue statement skips some statements inside the loop. The continue statement is used
with decision making statement such as if...else.

Syntax of continue Statement


continue;

Flowchart of continue Statement


How continue statement works?
Example #2: continue statement
// Program to calculate sum of maximum of 10 numbers
// Negative numbers are skipped from calculation

# include <stdio.h>
int main()
{
int i;
double number, sum = 0.0;
for(i=1; i <= 10; ++i)
{
printf("Enter a n%d: ",i);
scanf("%lf",&number);

// If user enters negative number, loop is terminated


if(number < 0.0)
{
continue;
}

sum += number; // sum = sum + number;


}

printf("Sum = %.2lf",sum);

return 0;
}

Output

Enter a n1: 1.1


Enter a n2: 2.2
Enter a n3: 5.5
Enter a n4: 4.4
Enter a n5: -3.4
Enter a n6: -45.5
Enter a n7: 34.5
Enter a n8: -4.2
Enter a n9: -1000
Enter a n10: 12
Sum = 59.70

In the program, when the user enters positive number, the sum is calculated using sum +=
number; statement.

When the user enters negative number, the continue statement is executed and skips the
negative number from calculation.

Example #1: Fibonacci Series up to n number of terms


#include <stdio.h>
int main()
{
int i, n, t1 = 0, t2 = 1, nextTerm;

printf("Enter the number of terms: ");


scanf("%d", &n);

printf("Fibonacci Series: ");


for (i = 1; i <= n; ++i)
{
printf("%d, ", t1);
nextTerm = t1 + t2;
t1 = t2;
t2 = nextTerm;
}
return 0;
}

Output

Enter the number of terms: 10


Fibonacci Series: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34,

Example #2: Fibonacci Sequence Up to a Certain Number


#include <stdio.h>
int main()
{
int t1 = 0, t2 = 1, nextTerm = 0, n;

printf("Enter a positive number: ");


scanf("%d", &n);

// displays the first two terms which is always 0 and 1


printf("Fibonacci Series: %d, %d, ", t1, t2);

nextTerm = t1 + t2;

while(nextTerm <= n)
{
printf("%d, ",nextTerm);
t1 = t2;
t2 = nextTerm;
nextTerm = t1 + t2;
}

return 0;
}

Output

Enter a positive integer: 100


Fibonacci Series: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89,
Example: Simple Calculator using switch Statement

// Performs addition, subtraction, multiplication or division depending the


input from user

# include <stdio.h>

int main() {

char operator;
double firstNumber,secondNumber;

printf("Enter an operator (+, -, *,): ");


scanf("%c", &operator);

printf("Enter two operands: ");


scanf("%lf %lf",&firstNumber, &secondNumber);

switch(operator)
{
case '+':
printf("%.1lf + %.1lf = %.1lf",firstNumber, secondNumber,
firstNumber + secondNumber);
break;

case '-':
printf("%.1lf - %.1lf = %.1lf",firstNumber, secondNumber,
firstNumber - secondNumber);
break;

case '*':
printf("%.1lf * %.1lf = %.1lf",firstNumber, secondNumber,
firstNumber * secondNumber);
break;

case '/':
printf("%.1lf / %.1lf = %.1lf",firstNumber, secondNumber,
firstNumber / secondNumber);
break;

// operator doesn't match any case constant (+, -, *, /)


default:
printf("Error! operator is not correct");
}

return 0;
}

Output

Enter an operator (+, -, *,): *


Enter two operands: 1.5
4.5
1.5 * 4.5 = 6.8
 C Programming Operators
 C if...else Statement

This program uses only if statement to find the largest number.

Example #1
#include <stdio.h>
int main()
{
double n1, n2, n3;

printf("Enter three different numbers: ");


scanf("%lf %lf %lf", &n1, &n2, &n3);

if( n1>=n2 && n1>=n3 )


printf("%.2f is the largest number.", n1);

if( n2>=n1 && n2>=n3 )


printf("%.2f is the largest number.", n2);

if( n3>=n1 && n3>=n2 )


printf("%.2f is the largest number.", n3);

return 0;
}

This program uses if...else statement to find the largest number.

Example #2
#include <stdio.h>
int main()
{
double n1, n2, n3;

printf("Enter three numbers: ");


scanf("%lf %lf %lf", &n1, &n2, &n3);

if (n1>=n2)
{
if(n1>=n3)
printf("%.2lf is the largest number.", n1);
else
printf("%.2lf is the largest number.", n3);
}
else
{
if(n2>=n3)
printf("%.2lf is the largest number.", n2);
else
printf("%.2lf is the largest number.",n3);
}
return 0;
}

This program uses nested if...else statement to find the largest number.

Example #3
#include <stdio.h>
int main()
{
double n1, n2, n3;

printf("Enter three numbers: ");


scanf("%lf %lf %lf", &n1, &n2, &n3);

if( n1>=n2 && n1>=n3)


printf("%.2lf is the largest number.", n1);

else if (n2>=n1 && n2>=n3)


printf("%.2lf is the largest number.", n2);

else
printf("%.2lf is the largest number.", n3);

return 0;
}

Though, the largest number among three numbers is found using multiple ways, the output of all
these program will be same.

Enter three numbers: -4.5


3.9
5.6
5.60 is the largest number.

C Program to Find all Roots of a Quadratic Equation


This program accepts coefficients of a quadratic equation from the user and displays the roots (both real
and complex roots depending upon the determinant).
To understand this example, you should have the knowledge of following C programming topics:

 C Programming Operators
 C if...else Statement

The standard form of a quadratic equation is:

ax2 + bx + c = 0, where
a, b and c are real numbers and
a ≠ 0

The term b2-4ac is known as the determinant of a quadratic equation. The determinant tells the
nature of the roots.

 If determinant is greater than 0, the roots are real and different.


 If determinant is equal to 0, the roots are real and equal.
 If determinant is less than 0, the roots are complex and different.
Example: Program to Find Roots of a Quadratic Equation

#include <stdio.h>
#include <math.h>

int main()
{
double a, b, c, determinant, root1,root2, realPart, imaginaryPart;

printf("Enter coefficients a, b and c: ");


scanf("%lf %lf %lf",&a, &b, &c);

determinant = b*b-4*a*c;

// condition for real and different roots


if (determinant > 0)
{
// sqrt() function returns square root
root1 = (-b+sqrt(determinant))/(2*a);
root2 = (-b-sqrt(determinant))/(2*a);

printf("root1 = %.2lf and root2 = %.2lf",root1 , root2);


}

//condition for real and equal roots


else if (determinant == 0)
{
root1 = root2 = -b/(2*a);

printf("root1 = root2 = %.2lf;", root1);


}

// if roots are not real


else
{
realPart = -b/(2*a);
imaginaryPart = sqrt(-determinant)/(2*a);
printf("root1 = %.2lf+%.2lfi and root2 = %.2f-%.2fi", realPart,
imaginaryPart, realPart, imaginaryPart);
}

return 0;
}

Output

Enter coefficients a, b and c: 2.3


4
5.6
Roots are: -0.87+1.30i and -0.87-1.30i

In this program, library function sqrt() is used to find the square root of a number. To learn
more, visit: sqrt() function.

Example #1: Sum of Natural Numbers Using for Loop


#include <stdio.h>
int main()
{
int n, i, sum = 0;

printf("Enter a positive integer: ");


scanf("%d",&n);

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


{
sum += i; // sum = sum+i;
}

printf("Sum = %d",sum);

return 0;
}
The above program takes the input from the user and stores in variable n. Then, for loop is used
to calculate the sum upto the given number.

Example #2: Sum of Natural Numbers Using while Loop


#include <stdio.h>
int main()
{
int n, i, sum = 0;

printf("Enter a positive integer: ");


scanf("%d",&n);

i = 1;
while ( i <=n )
{
sum += i;
++i;
}

printf("Sum = %d",sum);

return 0;
}

Output

Enter a positive integer: 100


Sum = 5050

Example #1: Multiplication Table Up to 10


#include <stdio.h>
int main()
{
int n, i;

printf("Enter an integer: ");


scanf("%d",&n);

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


{
printf("%d * %d = %d \n", n, i, n*i);
}

return 0;
}

Output

Enter an integer: 9
9 * 1 = 9
9 * 2 = 18
9 * 3 = 27
9 * 4 = 36
9 * 5 = 45
9 * 6 = 54
9 * 7 = 63
9 * 8 = 72
9 * 9 = 81
9 * 10 = 90

Here's a little modification of the above program to generate the multiplication table up to a
range (where range is also a positive integer entered by the user).

Example #2: Multiplication Table Up to a range (entered by


the user)
#include <stdio.h>
int main()
{
int n, i, range;

printf("Enter an integer: ");


scanf("%d",&n);

printf("Enter the range: ");


scanf("%d", &range);

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


{
printf("%d * %d = %d \n", n, i, n*i);
}

return 0;
}

Output

Enter an integer: 12
Enter the range: 8
12 * 1 = 12
12 * 2 = 24
12 * 3 = 36
12 * 4 = 48
12 * 5 = 60
12 * 6 = 72
12 * 7 = 84
12 * 8 = 96

Example #1: Program to Display English Alphabets


#include <stdio.h>
int main()
{
char c;

for(c = 'A'; c <= 'Z'; ++c)


printf("%c ", c);

return 0;
}

Output

A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

In this program, the for loop is used to display the English alphabets in uppercase.

Here's a little modification of the above program to display the English alphabets in either
uppercase or lowercase depending upon the input from the user.

Example #2: Program to Display English Alphabets in


Uppercase and Lowercase
#include <stdio.h>
int main()
{
char c;

printf("Enter u to display alphabets in uppercase. And enter l to display


alphabets in lowercase: ");
scanf("%c", &c);

if(c== 'U' || c== 'u')


{
for(c = 'A'; c <= 'Z'; ++c)
printf("%c ", c);
}
else if (c == 'L' || c == 'l')
{
for(c = 'a'; c <= 'z'; ++c)
printf("%c ", c);
}
else
printf("Error! You entered invalid character.");
return 0;
}

Output

Enter u to display alphabets in uppercase. And enter l to display alphabets


in lowercase: l
a b c d e f g h i j k l m n o p q r s t u v w x y z

You might also like