C Program Manual- Third Semster
C Program Manual- Third Semster
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;
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;
return 0;
}
Output
Enter any number to find reverse: 123
Sum = 6
Reverse = 321
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;
return 0;
}
Output
Enter any number to calculate factorial: 3
Factorial of 3 = 6
*
* *
* * *
* * * *
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);
printf("\n");
}
return 0;
}
Output
Enter N: 4
*
* *
* * *
* * * *
int main()
{
int year;
return 0;
}
Output
Enter year : 2004
LEAP YEAR
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;
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
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
6. Set j=2
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
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,
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;
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
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;
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;
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
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;
tempOctal = octal;
decimal = 0;
place = 0;
while(tempOctal > 0)
{
rem = tempOctal % 10;
decimal += pow(8, place) * rem;
tempOctal /= 10;
place++;
}
return 0;
}
Output
Enter any octal number: 172
Octal number = 172
Decimal number = 122
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;
n3 = n1 + n2;
for(i = 0, j=0; i < n3; i++)
{
if (i<n1)
c[i] = a[i];
else
{
c[i] = b[j];
j++;
}
}
Result:
Program executed successfully without any error.
Program
#include <stdio.h>
int main()
{
int a, b, c, i, terms;
a = 0;
b = 1;
c = 0;
return 0;
}
Output
Enter number of terms: 10
Fibonacci terms:
0 1 1 2 3 5 8 13 21 34
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)
scanf("%d", &A[i][j]);
6. Printf Matrix:
Result:
Program executed successfully without any error.
Program
#include <stdio.h>
int main()
{
int A[100][100],m, n, i, j, trace = 0;
return 0;
}
Output
Algorithm
main()
1. declare int a[100], n, i, d, swap
{
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;
}
{
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
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;
i = 0;
words = 0;
i++;
}
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;
vowel = 0;
len = strlen(str);
Output
Enter any string: I love C
Total number of vowel = 3