You are on page 1of 8

1.

Program to count number of alphabets, digits and special characters in string


Program:
#include <stdio.h>
#define MAX_SIZE 100 // Maximum string size
int main()
{
char str[MAX_SIZE];
int alphabets, digits, others, i;
alphabets = digits = others = i = 0;
/* Input string from user */
printf("Enter any string : ");
gets(str);
/*
* Check each character of string for alphabet, digit or special character
*/
while(str[i]!='\0')
{
if((str[i]>='a' && str[i]<='z') || (str[i]>='A' && str[i]<='Z'))
{
alphabets++;
}
else if(str[i]>='0' && str[i]<='9')
{
digits++;
}
else
{
others++;
}
i++;
}
printf("Alphabets = %d\n", alphabets);
printf("Digits = %d\n", digits);
printf("Special characters = %d", others);
return 0;
}
Output:

Enter any string:


Welcome@123.

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;

/* Input rows to print from user */


printf("Enter number of rows : ");
scanf("%d", &rows);
for(i=1; i<=rows; i++)
{
/* Print trailing spaces */
for(j=i; j<rows; j++)
{
printf(" ");
}
/* Print hollow pyramid */
for(j=1; j<=(2*i-1); j++)
{
/*
* Print star for last row (i==rows),
* first column(j==1) and for
* last column (j==(2*i-1)).
*/
if(i==rows || j==1 || j==(2*i-1))
{
printf("*");
}
else
{
printf(" ");
}
}
/* Move to next line */
printf("\n");
}
return 0;
}

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;

/* Input unit consumed from user */


printf("Enter total units consumed: ");
scanf("%d", &unit);
/* Calculate electricity bill according to given conditions */
if(unit <= 50)
{
amt = unit * 0.50;
}
else if(unit <= 150)
{
amt = 25 + ((unit-50) * 0.75);
}
else if(unit <= 250)
{
amt = 100 + ((unit-150) * 1.20);
}
else
{
amt = 220 + ((unit-250) * 1.50);
}
/*
* Calculate total electricity bill
* after adding surcharge
*/
sur_charge = amt * 0.20;
total_amt = amt + sur_charge;
printf("Electricity Bill = Rs. %.2f", total_amt);
return 0;
}
Note: %.2f is used to print fractional values only up to 2 decimal places. You can also
use %f to print fractional values normally up to six decimal places.

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

5. Write a recursive function in C programming to print all natural numbers between 1 to


n.
Program:
#include <stdio.h>
/* Function declaration */
void printNaturalNumbers(int lowerLimit, int upperLimit);
int main()
{
int lowerLimit, upperLimit;
/* Input lower and upper limit from user */
printf("Enter lower limit: ");
scanf("%d", &lowerLimit);
printf("Enter upper limit: ");
scanf("%d", &upperLimit);
printf("All natural numbers from %d to %d are: ", lowerLimit, upperLimit);
printNaturalNumbers(lowerLimit, upperLimit);
return 0;
}
/**
* Recursively prints all natural number between the given range.
*/
void printNaturalNumbers(int lowerLimit, int upperLimit)
{
if(lowerLimit > upperLimit)
return;
printf("%d, ", lowerLimit);
// Recursively call the function to print next number
printNaturalNumbers(lowerLimit + 1, upperLimit);
}
Output:
Enter lower limit: 1
Enter upper limit: 100
All natural numbers from 1 to 100 are: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38,
39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61,
62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84,
85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100,

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

You might also like