Programs To Print Triangles Using, Numbers and Characters
Programs To Print Triangles Using, Numbers and Characters
Programs To Print Triangles Using, Numbers and Characters
Source Code
#include <stdio.h>
int main()
{
int i, j, rows;
#include <stdio.h>
int main()
{
int i, j, rows;
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);
printf("\n");
}
return 0;
}
Programs to print inverted half pyramid using * and
numbers
Source Code
#include <stdio.h>
int main()
{
int i, j, rows;
return 0;
}
Source Code
#include <stdio.h>
int main()
{
int i, j, rows;
printf("Enter number of rows: ");
scanf("%d",&rows);
return 0;
}
Source Code
#include <stdio.h>
int main()
{
int i, space, rows, k=0;
while(k != 2*i-1)
{
printf("* ");
++k;
}
printf("\n");
}
return 0;
}
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;
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("\n");
}
return 0;
}
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);
printf("%4d", coef);
}
printf("\n");
}
return 0;
}
Source Code
#include <stdio.h>
int main()
{
int rows, i, j, number= 1;
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:
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.
To learn more on test expression (when test expression is evaluated to nonzero (true) and 0
(false)), check out relational and logical operators.
return 0;
}
Output
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
# include <stdio.h>
int main()
{
int i;
double number, sum = 0.0;
printf("Sum = %.2lf",sum);
return 0;
}
Output
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.
continue Statement
The continue statement skips some statements inside the loop. The continue statement is used
with decision making statement such as if...else.
# 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);
printf("Sum = %.2lf",sum);
return 0;
}
Output
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.
Output
nextTerm = t1 + t2;
while(nextTerm <= n)
{
printf("%d, ",nextTerm);
t1 = t2;
t2 = nextTerm;
nextTerm = t1 + t2;
}
return 0;
}
Output
# include <stdio.h>
int main() {
char operator;
double 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;
return 0;
}
Output
Example #1
#include <stdio.h>
int main()
{
double n1, n2, n3;
return 0;
}
Example #2
#include <stdio.h>
int main()
{
double 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;
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.
C Programming Operators
C if...else Statement
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.
#include <stdio.h>
#include <math.h>
int main()
{
double a, b, c, determinant, root1,root2, realPart, imaginaryPart;
determinant = b*b-4*a*c;
return 0;
}
Output
In this program, library function sqrt() is used to find the square root of a number. To learn
more, visit: sqrt() function.
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.
i = 1;
while ( i <=n )
{
sum += i;
++i;
}
printf("Sum = %d",sum);
return 0;
}
Output
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).
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
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.
Output