C Pgms
C Pgms
C Pgms
Alphabets = 7
Digits = 3
Special characters = 2
2. C program to print hollow pyramid (Equilateral triangle) star pattern.
Enter number of rows: 5
*
* *
* *
* *
*********
Program:
#include <stdio.h>
int main()
{
int i, j, rows;
Output:
Enter number of rows: 5
*
* *
* *
* *
*********
3. Write a C program to input electricity unit charge and calculate the total electricity
bill according to the given condition:
For first 50 units Rs. 0.50/unit
For next 100 units Rs. 0.75/unit
For next 100 units Rs. 1.20/unit
For unit above 250 Rs. 1.50/unit
An additional surcharge of 20% is added to the bill.
Program:
#include <stdio.h>
int main()
{
int unit;
float amt, total_amt, sur_charge;
Output:
Enter total units consumed: 150
Electricity Bill = Rs. 125.00
4. Write a C program to print the Floyd's triangle number pattern using loop.
Enter N: 5
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
Program:
#include <stdio.h>
int main()
{
int i, j, k, N;
printf("Enter N: ");
scanf("%d", &N);
k = 1;
for(i=1; i<=N; i++)
{
// Logic to print numbers
for(j=1; j<=i; j++, k++)
{
printf("%3d", k);
}
printf("\n");
}
return 0;
}
Output:
Enter N: 5
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
6. Write a C program to read elements in an array and sort elements of the array in
ascending order.
Program:
#include <stdio.h>
#define MAX_SIZE 100
int main()
{
int array[MAX_SIZE];
int size;
int i, j, temp;
/* Input size of array */
printf("Enter size of array: ");
scanf("%d", &size);
/* Input elements in array */
printf("Enter elements in array: ");
for(i=0; i<size; i++)
{
scanf("%d", &array[i]);
}
for(i=0; i<size; i++)
{
/*
* Place the currently selected element array[i]
* to its correct place.
*/
for(j=i+1; j<size; j++)
{
/*
* Swap if currently selected array element
* is not at its correct position.
*/
if(array[i] > array[j])
{
temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
}
/* Print the sorted array */
printf("\nElements of array in sorted ascending order: ");
for(i=0; i<size; i++)
{
printf("%d\t", array[i]);
}
return 0;
}
Output:
Enter size of array: 10
Enter elements in array: 20 2 10 6 52 31 0 45 79 40
Elements of array in sorted ascending order: 0 2 6 10 20 31 40
45 52 79
7. Write a C program to create menu driven calculator that performs basic arithmetic
operations (add, subtract, multiply and divide) using switch case and functions.
Program:
#include <stdio.h>
int main()
{
char op;
float num1, num2, result=0.0f;
/* Print welcome message */
printf("WELCOME TO SIMPLE CALCULATOR\n");
printf("----------------------------\n");
printf("Enter [number 1] [+ - * /] [number 2]\n");
/* Input two number and operator from user */
scanf("%f %c %f", &num1, &op, &num2);
/* Switch the value and perform action based on operator*/
switch(op)
{
case '+':
result = num1 + num2;
break;
case '-':
result = num1 - num2;
break;
case '*':
result = num1 * num2;
break;
case '/':
result = num1 / num2;
break;
default:
printf("Invalid operator");
}
/* Prints the result */
printf("%.2f %c %.2f = %.2f", num1, op, num2, result);
return 0;
}
}
Note: %.2f is used to print fractional values only up to two decimal places. You can
also use %f to print fractional values normally up to six decimal places.
Output:
WELCOME TO SIMPLE CALCULATOR
----------------------------
Enter [number 1] [+ - * /] [number 2]
22 * 6
22.00 * 6.00 = 132.00