0% found this document useful (0 votes)
1 views

C Program Manual- Third Semster

The document is a C Programming Lab Manual containing multiple programming tasks including finding roots of quadratic equations, sum of digits and reverse of a number, factorial calculation, pyramid printing, leap year checking, counting positive, negative, and zero elements in an array, finding prime numbers, calculating LCM and HCF, and printing Armstrong numbers. Each task includes an aim, algorithm, program code, and sample output. The manual demonstrates various C programming concepts and techniques through practical examples.

Uploaded by

rithusreekj005
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

C Program Manual- Third Semster

The document is a C Programming Lab Manual containing multiple programming tasks including finding roots of quadratic equations, sum of digits and reverse of a number, factorial calculation, pyramid printing, leap year checking, counting positive, negative, and zero elements in an array, finding prime numbers, calculating LCM and HCF, and printing Armstrong numbers. Each task includes an aim, algorithm, program code, and sample output. The manual demonstrates various C programming concepts and techniques through practical examples.

Uploaded by

rithusreekj005
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 28

C PROGRAMMING LAB MANUAL

1. Find roots of a quadratic equation


AIM:
Write a C Program to find roots of a quadratic equation,

1. Input coefficients of quadratic equation from user. Store it in some variable say a, b and c.
2. Find discriminant of the given equation, using formula discriminant = (b*b) - (4*a*c).
3. Compute roots based on the nature of discriminant.
4. If discriminant > 0 then,
root1 = (-b + sqrt(discriminant)) / (2*a) and
root2 = (-b - sqrt(discriminant)) / (2*a).
5. If discriminant == 0 then, root1 = root2 = -b / (2*a).
6. Else if discriminant < 0 then, there are two distinct complex roots where
root1 = -b / (2*a) and root2 = -b / (2*a).
Imaginary part of the root is given by imaginary = sqrt(-discriminant) / (2*a).
Result:
Program executed successfully without any error.
Program
#include <stdio.h>
#include <math.h>

int main()
{
float a, b, c;
float root1, root2, imaginary;
float discriminant;

printf("Enter values of a, b, c of quadratic equation: ");


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

discriminant = (b * b) - (4 * a * c);

if(discriminant > 0)
{
root1 = (-b + sqrt(discriminant)) / (2*a);
root2 = (-b - sqrt(discriminant)) / (2*a);
printf("Two distinct and real roots: %.2f and %.2f", root1, root2);
}
else if(discriminant == 0)
{
root1 = root2 = -b / (2 * a);
printf("Two equal and real roots: %.2f and %.2f", root1, root2);
}
else if(discriminant < 0)
{
root1 = root2 = -b / (2 * a);
imaginary = sqrt(-discriminant) / (2 * a);
printf("Two distinct complex roots: %.2f + i%.2f and %.2f - i%.2f",
root1, imaginary, root2, imaginary);
}

return 0;
}

Output
Enter values of a, b, c of quadratic equation: 8 -4 -2
Two distinct and real roots exists: 0.81 and -0.31
2. Find the sum of digits and reverse of a number
AIM:
Write a C Program to find sum of digits and reverse of a number.

Algorithm
1. Input a number from user to find reverse. Store it in variable say num.
2. Declare and initialize another variable to store reverse and sum of num, say reverse = 0
and sum=0.
3. Extract last digit of the given number by performing modulo division. Store the last digit
to some variable say Digit = num % 10.
4. sum=sum+Digit
5. reverse = ( reverse * 10)+Digit
6. num = num / 10.
7. Repeat step 3 to 6 till num is not equal to (or greater than) zero.
8. Multiply the current loop counter value i.e. i with fact. Which is fact = fact * i.
Result:
Program executed successfully without any error.
Program
#include <stdio.h>

int main()
{
int num,sum=0, reverse = 0;

/* Input a number from user */


printf("Enter any number to find reverse: ");
scanf("%d", &num);

/* Repeat the till 'num' becomes 0 */


while(num != 0)
{
digit= num % 10;
sum= sum + digit;
reverse = (reverse * 10) + digit;
num /= 10;
}
printf("Sum = %d", sum);
printf("Reverse = %d", reverse);

return 0;
}

Output
Enter any number to find reverse: 123
Sum = 6
Reverse = 321

3. Find the factorial of a number


AIM:
Write a C Program to find factorial of a number.

Algorithm
1. Input a number from user. Store it in some variable say num.
2. Initialize another variable that will store factorial say fact = 1.
3. Why initialize fact with 1 not with 0? This is because you need to perform multiplication
operation not summation. Multiplying 1 by any number results same, same as summation
of 0 and any other number results same.
4. Run a loop from 1 to num, increment 1 in each iteration. The loop structure should look
like for(i=1; i<=num; i++).
5. Multiply the current loop counter value i.e. i with fact. Which is fact = fact * i.
Result:
Program executed successfully without any error.
Program
#include <stdio.h>
int main()
{
int i, num;
long int fact=1;

/* Input number from user */


printf("Enter any number to calculate factorial: ");
scanf("%d", &num);

/* Run loop from 1 to num */


for(i=1; i<=num; i++)
{
fact = fact * i;
}

printf("Factorial of %d = %lu", num, fact);

return 0;
}
Output
Enter any number to calculate factorial: 3
Factorial of 3 = 6

4. Display pyramid using ‘*’


AIM:
Write a C Program to print pyramid using *

*
* *
* * *
* * * *
Algorithm
1. Start
2. Declare integer variable i, j, k, N;
3. Enter N: N
4. Print pattern:
for(i=1; i<=N; i++)
{
for(j=1; j<=i; j++)
{
printf("*\t");
}

printf("\n");
}
5. Stop
Result:
Program executed successfully without any error.
Program
#include <stdio.h>

int main()
{
int i, j, k, N;

printf("Enter N: ");
scanf("%d", &N);

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


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

printf("\n");
}

return 0;
}
Output
Enter N: 4
*
* *
* * *
* * * *

5. Check for leap year


AIM:
Write a C Program to check leap year.

Logic to check leap year


leap year as a special year containing one extra day i.e. total 366 days in a year. A year is said
to be leap year, if the year is exactly divisible by 4 but and not divisible by 100. Year is also a
leap year if it is exactly divisible by 400.
Algorithm
1. Input year from user. Store it in some variable say year.
2. If year is exactly divisible by 4 and not divisible by 100, then it is leap year. Or if year is
exactly divisible by 400 then it is leap year.
3. Otherwise it is not leap year
Result:
Program executed successfully without any error.
Program to check leap year
#include <stdio.h>

int main()
{
int year;

/* Input year from user */


printf("Enter year : ");
scanf("%d", &year);

if(((year % 4 == 0) && (year % 100 !=0)) || (year % 400==0))


printf("LEAP YEAR");
else
printf("COMMON YEAR");

return 0;
}

Output
Enter year : 2004
LEAP YEAR

6.To display count of +ves, -ves and zeros in a set of N


numbers
AIM:
Write a C Program to display count of +ves, -ves and zeros in a set of N numbers

Algorithm
1. Declare array arr[MAX_SIZE]
2. Declare variable size, countpos=0, countneg=0, countzero=0
3. Input size of the array to variable size
4. Enter elements into array elements from user
5. Iterate through each element in array, run a loop from i=0 to size. Each time increment 1
6. if(arr[i] < 0)then countneg++
7. else if (arr[i] > 0)then countpos++
8. else countzero++
9. goto step 5
10. Print negative, positive and zero element count.
Result:
Program executed successfully without any error.
Program
#include <stdio.h>
#define MAX_SIZE 100 // Maximum array size

int main()
{
int arr[MAX_SIZE]; // Declares array of size 100
int i, size, countpos=0, countneg=0, countzero=0;

printf("Enter size of the array : ");


scanf("%d", &size);

printf("Enter elements in array : ");


for(i=0; i<size; i++)
{
scanf("%d", &arr[i]);
}

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


{
if(arr[i] < 0)
{
countneg++;
}
else if (arr[i] > 0)
{
countpos++;
}
else
{
countzero++;
}

printf("\nTotal negative elements in array = %d", countneg);


printf("\nTotal positive elements in array = %d", countpos);
printf("\nTotal zeros in array = %d", countzero);

return 0;
}

Output
Enter size of the array :5
Enter elements in array :10
-6
0
-9
9
Total negative elements in array = 2
Total positive elements in array = 2
Total zeros in array = 0

7. Find first n prime numbers


AIM:
Write a C Program to find first n prime numbers

What is Prime number?


Prime number is a positive integer greater than 1 that is only divisible by 1 and itself. For
example: 2, 3 , 5, 7, 11 are the first five prime numbers.

Algorithm
1. Declare integer variable i, j, n, isPrime (isPrime is used as flag
variable)
2. Enter value of n
3. Print massage "All prime numbers upto n are:

4. Set i=2

5. Check i<=n if true Set isPrime = 1 else goto step 11

6. Set j=2

7. check ( j<=i/2) if true goto step 8 else goto step 9

8. if(i%j==0) then isPrime = 0 and jump to step 9

9. if(isPrime==1) then print i

10. increment i and goto step 5

11. Stop
Result:
Program executed successfully without any error.
Program
#include <stdio.h>

int main()
{
int i, j, n, isPrime; // isPrime is used as flag variable

printf("Enter value of n: ");


scanf("%d", &n);

printf("All prime numbers upto %d are:\n", n);

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


{
isPrime = 1;

for(j=2; j<=i/2; j++)


{
if(i%j==0)
{
isPrime = 0;
break;
}
}

/* If the number is prime then print */


if(isPrime==1)
{
printf("%d, ", i);
}
}

return 0;
}
Output
Enter value of n : 100
All prime numbers upto 50 are:
2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47,

8.Find LCM and HCF of 2 numbers


AIM:
Write a C Program to find LCM and HCF of 2 numbers.
What is HCF?

HCF (Highest Common Factor) is the greatest number that divides exactly two or
more numbers. HCF is also known as GCD (Greatest Common Divisor) or GCF
(Greatest Common Factor).

What is LCM?

LCM is a smallest positive integer that exactly divides two or more numbers.
For Example

Algorithm
1. Start
2. Declare integer variables i, num1, num2, min, hcf=1
3. Enter any two numbers to find HCF. Store it in num1 and num 2
4. min = (num1<num2) ? num1 : num2;
5. for(i=1; i<=min; i++)
if(num1%i==0 && num2%i==0)
hcf = i
6. Print hcf
7. max = (num1 > num2) ? num1 : num2
8. i = max
9. while(1)
{
if(i%num1==0 && i%num2==0)
{
lcm = i
break
}
i += max;
}
10. print lcm
11. End
Result:
Program executed successfully without any error.
Program
#include <stdio.h>

int main()
{
int i, num1, num2, min, max, lcm, hcf=1;

printf("Enter any two numbers to find HCF: ");


scanf("%d%d", &num1, &num2);

min = (num1<num2) ? num1 : num2;

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


{
/* If i is factor of both number */
if(num1%i==0 && num2%i==0)
{
hcf = i;
}
}

printf("HCF of %d and %d = %d\n", num1, num2, hcf);

max = (num1 > num2) ? num1 : num2;

i = max;

while(1)
{
if(i%num1==0 && i%num2==0)
{
lcm = i;
break;
}
i += max;
}
printf("LCM of %d and %d = %d", num1, num2, lcm);

return 0;

Output
Enter any two numbers to find HCF: 4
8
HCF of 4 and 8 = 4
LCM of 4 and 8 = 8

9.To print Armstrong numbers within range


AIM:
Write a C Program to print Armstrong numbers within range

What is Armstrong number?

An Armstrong number is a n-digit number that is equal to the sum of nth power
of its digits. For example,
6 = 61 = 6
371 = 33 + 73 + 13 = 371

Algorithm
1. Ddeclare n, num, count, lastDigit, sum, i,j, p;
2. Enter value of n
3. Print "Armstrong number between 1 to n”

4. For i=1 to n
5. Begin for
num = i;
count=0;

6. while(num > 0)
begin while
count++;
num = num / 10;
end while

sum = 0;
num = i;
7. while(num > 0)
begin while
lastDigit = num % 10;
8. for(j=p=1;j<=count;j++)
begin for
p=p*lastDigit;
end for
sum = sum + p;
num = num / 10;
end while
9. if(i == sum)
print value of i
10. End for

Result:
Program executed successfully without any error.

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

int main()
{
int n, num,count, lastDigit, digits, sum, i,j, p;

printf("Enter value of n: ");


scanf("%d", &n);

printf("Armstrong number between 1 to %d are: \n", n);

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


{
num = i;
count=0;
while(num > 0)
{
count++;
num = num / 10;
}
sum = 0;
num = i;
while(num > 0)
{
lastDigit = num % 10;
for(j=p=1;j<=count;j++)
{
p=p*lastDigit;
}
sum = sum + p;
num = num / 10;
}
if(i == sum)
{
printf("%d \t ", i);
}
}
return 0;
}
Output
Enter value of n: 500

Armstrong number between 1 to 500 are:

1 2 3 4 5 6 7 8 9 153 370 371 407

10. Convert a decimal number to a new base


AIM:
Write a C Program to convert decimal numbers to binary number.

Algorithm
1. Start
2. Declare num, n, remainder, binary = 0, place = 1
3. Enter a number num
4. n = num
5. Convert decimal to binary
while (n > 0)
begin
remainder = n % 2;
binary += remainder * place;
place *= 10;
n /= 2;
end
6. print value of binary
7. Stop

Result:
Program executed successfully without any error.
Program
#include <stdio.h>
int main()
{
int num, n, remainder, binary = 0, place = 1;

printf("Enter a number :");


scanf("%d", &num);
n = num;
while (n > 0)
{
remainder = n % 2;
binary += remainder * place;
place *= 10;
n /= 2;
}
printf("Binary equivalent of %d is %d", num, binary);
return 0;
}
Output
Enter a number :12
Binary equivalent of 12 is 1100

11. Calculate the standard deviation of N numbers


AIM:
Write a C Program to find standard deviation of N numbers.

Algorithm

1. Start
2. Declare int i, n;
3. Declare float num[25], sum = 0.0, mean, sd;
4. Enter total number of elements to n
5. Enter the value of n element to array num[i]
6. Calculate Mean of array elements
for (i = 0; i < n; i++)
sum = sum+ num[i];
mean = sum / n;
7. Calculate Standard Deviation
sum = 0.0;
for (i = 0; i < n; i++)
sum += (num[i] - mean) * (num[i] - mean);
sd = sqrt(sum / n);
8. print mean
9. print Standard Deviation:sd
10. Stop

Result:
Program executed successfully without any error.
Program

#include <stdio.h>
#include <math.h>
int main()
{
int i, n;
float num[25], sum = 0.0, mean, sd;
printf("Enter total number of elements: ");
scanf("%d", &n);
printf("Enter the value of elements: \n");
for (i = 0; i < n; i++ )
scanf("%f", &num[i]);
// Calculating Mean
for (i = 0; i < n; i++)
sum = sum+ num[i];
mean = sum / n;
// Calculating Standard Deviation
sum = 0.0;
for (i = 0; i < n; i++)
sum += (num[i] - mean) * (num[i] - mean);
sd = sqrt(sum / n);
printf("Mean: %6.3f \n", mean);
printf("Standard Deviation: %.6f", sd);
return 0;
}

Output
Enter total number of elements: 4
Enter value of elements: 32
35
31
33
34
Mean: 33.000000
Standard Deviation: 1.414214

12.Find the decimal equivalent of number (base other


than 10)
AIM:
Write a C Program to find decimal equivalent of octal.

Logic to convert from octal to decimal

Algorithm
1. Start
2. Declare int octal, tempOctal, decimal, rem, place
3. Enter any octal number: octal
4. Set tempOctal = octal;
5. Set decimal = 0;
6. Set place = 0;
7. Covert octal to decimal
while(tempOctal > 0)
{
rem = tempOctal % 10;
decimal += pow(8, place) * rem;
tempOctal /= 10;
place++;
}
8. Print Octal number = octal
9. Print Decimal number = decimal
10. Stop
Result:
Program executed successfully without any error.
Program
#include <stdio.h>
#include <math.h>

int main()
{
int octal, tempOctal, decimal, rem, place;

/* Input octal number from user */


printf("Enter any octal number: ");
scanf("%d", &octal);

tempOctal = octal;

decimal = 0;
place = 0;

while(tempOctal > 0)
{
rem = tempOctal % 10;
decimal += pow(8, place) * rem;
tempOctal /= 10;
place++;
}

printf("Octal number = %d\n", octal);


printf("Decimal number = %d", decimal);

return 0;
}
Output
Enter any octal number: 172
Octal number = 172
Decimal number = 122

13.To merge two arrays


AIM:
Write a C Program to merge two arrays.

Algorithm

1. Start
2. Declare two arrays.
3. Initialize these two arrays.
4. Declare another array that will store the merged arrays.
5. The size of the merged array should be equal to the sum of the other
two arrays.
6. For loop will help to iterate every element present in the first
array.
7. Condition inside the for loop (i < Size) will ensure the compiler,
not exceed the array limit.
8. Inside the second for loop assign each and every array element to the
Merged array.
9. Now, print the merged array.
10. Stop

Result:
Program executed successfully without any error.
Program
#include<stdio.h>
int main()
{
int a[100], b[100], c[200], n1, n2, n3, i, j;

printf("\n Enter the number of elements for First Array : ");


scanf("%d", &n1);
printf("\nEnter the elements for First Array : ");
for(i = 0; i < n1; i++)
scanf("%d", &a[i]);

printf("\n Enter the number of elements for Second Array : ");


scanf("%d", &n2);
printf("\nEnter the elements for Second Array : ");
for(i = 0; i < n2; i++)
scanf("%d", &b[i]);

n3 = n1 + n2;
for(i = 0, j=0; i < n3; i++)
{
if (i<n1)
c[i] = a[i];
else
{
c[i] = b[j];
j++;
}
}

printf("Array Elements After Merging \n");


for(i = 0; i < n3; i++)
{
printf(" %d \t ",c[i]);
}
return 0;
}

14. C Program to print fibonacci series upto n terms


AIM:
Write a C Program to print Fibonacci series upto n terms.

What is Fibonacci series?


Fibonacci series is a series of numbers where the current number is the sum of
previous two terms. For Example: 0, 1, 1, 2, 3, 5, 8, 13, 21, ... , (n-1th + n-2th)
Algorithm
1. Input number of Fibonacci terms to print from user. Store it in a variable
say terms.
2. Declare and initialize three variables a=0, b=1 and c=0.
3. Here c is the current term, b is the n-1th term and a is n-2th term.
4. Run a loop from 1 to terms, increment loop counter by 1. The loop structure
should look like for(i=1; i<=term; i++).
5. Inside the loop copy the value of n-1th term to n-2th term i.e. a = b.
6. Next, copy the value of nth to n-1th term b = c.
7. Finally compute the new term by adding previous two terms i.e. c = a + b.
8. Print the value of current Fibonacci term i.e. c.

Result:
Program executed successfully without any error.
Program
#include <stdio.h>

int main()
{
int a, b, c, i, terms;

printf("Enter number of terms: ");


scanf("%d", &terms);

a = 0;
b = 1;
c = 0;

printf("Fibonacci terms: \n");

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


{
printf("%d \t ", c);

a = b; // Copy n-1 to n-2


b = c; // Copy current to n-1
c = a + b; // New term
}

return 0;
}
Output
Enter number of terms: 10
Fibonacci terms:
0 1 1 2 3 5 8 13 21 34

15.To find the trace of a square matrix


AIM:
Write a C Program to print Fibonacci series upto n terms.
Trace of a matrix
Trace of a n x n square matrix is sum of diagonal elements.

Algorithm

1. Start
2. Declare int A[100][100],m, n, i, j, trace = 0;
3. Enter size of matrix:m ,n
4. Enter elements into Matrix
5. for (i = 0; i < m; ++i)

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

scanf("%d", &A[i][j]);

6. Printf Matrix:

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


{

for (j = 0; j < n; ++j) {


printf(" %d", A[i][j]);
}
printf("\n");

7. Find trace of elements


8. if(m==n)then Find trace of elements

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

trace = trace + A[i][i];

print Trace of matrix is: trace

9. else printf "Trace does not exist"


10. Stop

Result:
Program executed successfully without any error.
Program
#include <stdio.h>

int main()
{
int A[100][100],m, n, i, j, trace = 0;

printf("Enter size of matrix:\n");


scanf("%d%d", &m,&n);

printf("Enter elements into Matrix:\n");


for (i = 0; i < m; ++i)
for (j = 0; j < n; ++j)
scanf("%d", &A[i][j]);
printf("Matrix:\n");
for (i = 0; i < m; ++i) {
for (j = 0; j < n; ++j) {
printf(" %d", A[i][j]);
}
printf("\n");
}

//Find trace of elements


if(m==n)
{
for (i = 0; i < 3; ++i)
trace = trace + A[i][i];

printf("Trace of matrix is: %d\n", trace);


}
else
printf("Trace does not exist");

return 0;
}
Output

16. To sort n numbers


AIM:
Write a C Program to sort n numbers.

Algorithm

main()
1. declare int a[100], n, i, d, swap

2. Enter number of elements in the array: n

3. Enter n integers into array

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


scanf("%d", &a[i]);
4. call bubble_sort(a, n);
5. Print the sorted array:
for (i = 0; i < n; i++)
printf("%d\n", a[i]);

void bubble_sort(int a[], int n)

{
int i = 0, j = 0, tmp;
for (i = 0; i < n; i++)

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

{
if (a[j] > a[j + 1])
{
tmp = a[j];
a[j] = a[j + 1];
a[j + 1] = tmp;
}
}
}
}

Program

#include <stdio.h>

int main() {
int a[100], n, i, d, swap;
printf("Enter number of elements in the array:\n");
scanf("%d", &n);
printf("Enter %d integers\n", n);
for (i = 0; i < n; i++)
scanf("%d", &a[i]);
bubble_sort(a, n);
printf("Printing the sorted array:\n");
for (i = 0; i < n; i++)
printf("%d\t", a[i]);
return 0;
}

void bubble_sort(int a[], int n)

{
int i = 0, j = 0, tmp;
for (i = 0; i < n; i++)

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

{
if (a[j] > a[j + 1])
{
tmp = a[j];
a[j] = a[j + 1];
a[j + 1] = tmp;
}
}
}
}

Output
Enter number of elements in the array:4
Enter 4 integers: 5 8 2 7
Printing the sorted array:2 5 7 8

17. Find the number of words in a given sentence


AIM:
Write a C Program to find number of words in a given sentence.

Algorithm
Input : str {Array of characters /String}
N {Size of the string}
Begin:
Enter any string to character array str
words = 0;
For i = 0 to N do
If (str [i] == ' ', '\t', '\n') then
word = word + 1;
End if
End for
Print word count words
End

Program
#include <stdio.h>
#define MAX_SIZE 100

int main()
{
char str[MAX_SIZE];
int i, words;

/* Input string from user */


printf("Enter any string: ");
gets(str);

i = 0;
words = 0;

/* Runs a loop till end of string */


while(str[i] != '\0')
{
/* If the current character(str[i]) is white space */
if(str[i]==' ' || str[i]=='\n' || str[i]=='\t')
{
words++;
}

i++;
}

printf("Total number of words = %d", words);

return 0;
}
Output
Enter any string: I love C
Total number of words = 3
18.Find the number of vowels in a strings
AIM:
Write a C Program to find number of vowels in a strings.

Algorithm
1. Include header files <stdio.h> and <string.h>
2. Define constant MAX_SIZE with value 100
3. Declare character array str[MAX_SIZE]
4. Declare integer variables i, len, vowel
5. Enter any string to str using gets function
6. Set vowel = 0
7. Find length of str, len = strlen(str)
8. for(i=0; i<len; i++)
{
if( str[i] =='a' || str[i]=='e' || str[i]=='i' || str[i]=='o' ||
str[i]=='u' || str[i] =='A' || str[i]=='E' || str[i]=='I' ||
str[i]=='O' || str[i]=='U' )
vowel++;
}
9. Print Total number of vowel
10. return 0;

Program
#include <stdio.h>
#include <string.h>
#define MAX_SIZE 100 // Maximum string size

int main()
{
char str[MAX_SIZE];
int i, len, vowel;

/* Input string from user */


printf("Enter any string: ");
gets(str);

vowel = 0;
len = strlen(str);

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


{
if( str[i] =='a' || str[i]=='e' || str[i]=='i' || str[i]=='o' ||
str[i]=='u' || str[i] =='A' || str[i]=='E' || str[i]=='I' ||
str[i]=='O' || str[i]=='U' )
vowel++;
}
printf("Total number of vowel = %d\n", vowel);
return 0;
}

Output
Enter any string: I love C
Total number of vowel = 3

You might also like