C Practical
Q1. Develop an Algorithm and draw corresponding flowchart and also write an interactive C Programing for the Following
a. To Check if a given Integer is odd or even .
b. To Calculate the sum of odd and even numbers from a given list of Numbers.
c. To check whether the given Integer is Divisible by 7.
d. To Find Whether the given integer is divisible by 7.
e. To Compute and Display the sum of Digits of a given 5 Digits Number.
Ans 1.
(a) To check whether a given integer is Odd or Even
Algorithm
- Start.
- Input an integer n.
- Check whether n % 2 == 0.
- If true, print Even Number.
- Otherwise, print Odd Number.
- Stop.
Flowchart
#include <stdio.h>
int main() {
int n;
printf("Enter an integer: ");
scanf("%d", &n);
if (n % 2 == 0)
printf("Even Number");
else
printf("Odd Number");
return 0;
}
Algorithm
- Start.
- Input the number of elements n.
- Initialize evenSum = 0 and oddSum = 0.
- Read each number from the list.
- If the number is divisible by 2, add it to evenSum; otherwise, add it to oddSum.
- Repeat until all numbers are processed.
- Display both sums.
- Stop.
Flowchart
#include <stdio.h>
int main() {
int n, i, num;
int evenSum = 0, oddSum = 0;
printf("Enter number of elements: ");
scanf("%d", &n);
for (i = 1; i <= n; i++) {
printf("Enter number %d: ", i);
scanf("%d", &num);
if (num % 2 == 0)
evenSum += num;
else
oddSum += num;
}
printf("Sum of even numbers = %d\n", evenSum);
printf("Sum of odd numbers = %d\n", oddSum);
return 0;
}
(c) To check whether a given integer is divisible by 7
Algorithm
- Start.
- Input an integer n.
- Check whether n % 7 == 0.
- If true, display Divisible by 7.
- Otherwise, display Not divisible by 7.
- Stop.
Flowchart
#include <stdio.h>
int main() {
int n;
printf("Enter an integer: ");
scanf("%d", &n);
if (n % 7 == 0)
printf("Divisible by 7");
else
printf("Not divisible by 7");
return 0;
}
(e) To compute and display the sum of digits of a given 5-digit number
Algorithm
- Start.
- Input a five-digit positive integer n.
- Initialize sum = 0.
- Extract the last digit using digit = n % 10.
- Add the digit to sum.
- Remove the last digit using n = n / 10.
- Repeat steps 4–6 until n becomes 0.
- Display the sum of digits.
- Stop.
Flowchart
#include <stdio.h>
int main() {
int n, digit, sum = 0;
printf("Enter a 5-digit number: ");
scanf("%d", &n);
if (n < 10000 || n > 99999) {
printf("Please enter a valid 5-digit number.");
return 0;
}
while (n > 0) {
digit = n % 10;
sum = sum + digit;
n = n / 10;
}
printf("Sum of digits = %d", sum);
return 0;
}
Session 2
Q 2. Design a Flowchart and write an interactice program for the Problem given below:
a. To Convert the given decimal Number to Binary.
b. To convert the Octal number to Decimal.
Ans 2.
(a) Convert the Given Decimal Number to Binary
Algorithm
- Start.
- Input a decimal number n.
- If n = 0, print binary number 0.
- Otherwise, divide n by 2 and store the remainder.
- Repeat the division until n becomes 0.
- Store the remainders in reverse order.
- Display the binary number.
- Stop.
Flowchart
3. Interactive C Program
This program accepts a non-negative decimal integer and displays its binary equivalent.
#include <stdio.h>
int main() {
int n, remainder;
long long binary = 0, place = 1;
printf("Enter a decimal number: ");
scanf("%d", &n);
if (n < 0) {
printf("Please enter a non-negative number.");
return 0;
}
if (n == 0) {
printf("Binary = 0");
return 0;
}
while (n > 0) {
remainder = n % 2;
binary = binary + remainder * place;
place = place * 10;
n = n / 2;
}
printf("Binary = %lld", binary);
return 0;
}
(b) Convert the Octal Number to Decimal
Algorithm
- Start.
- Input an octal number n.
- Initialize decimal = 0 and base = 1.
- Extract the last digit using digit = n % 10.
- If the digit is greater than 7, display an invalid octal number message and stop.
- Add digit × base to decimal.
- Multiply base by 8.
- Remove the last digit using n = n / 10.
- Repeat steps 4–8 until n becomes 0.
- Display the decimal number.
- Stop.
Flowchart
3. Interactive C Program
#include <stdio.h>
int main() {
int n, digit, decimal = 0, base = 1;
printf("Enter an octal number: ");
scanf("%d", &n);
if (n < 0) {
printf("Please enter a non-negative octal number.");
return 0;
}
if (n == 0) {
printf("Decimal = 0");
return 0;
}
while (n > 0) {
digit = n % 10;
if (digit > 7) {
printf("Invalid octal number.");
return 0;
}
decimal = decimal + digit * base;
base = base * 8;
n = n / 10;
}
printf("Decimal = %d", decimal);
return 0;
}
Q 3. Desgin a Flowchart and write an Interactive Program that reads in Integer until a 0 is Entered.If it Encounteres 0 as Input, then it should display:
- Number of Even and Odd Integers
- Sum and Average value of Even Integers
- Sum and Average value of Odd Integers.
Note: Use Switch statement for Selection.
Ans 3.
Program: Count Even and Odd Integers Using switch Statement
Algorithm
- Start.
- Initialize evenCount = 0, oddCount = 0, evenSum = 0, and oddSum = 0.
- Read an integer n.
- If n == 0, go to Step 9.
- Use switch (n % 2) to check whether the number is even or odd.
- If the remainder is 0, increment evenCount and add n to evenSum.
- Otherwise, increment oddCount and add n to oddSum.
- Read the next integer and repeat from Step 4.
- Display the number of even and odd integers, their sums, and their averages. If a count is zero, display its average as unavailable.
- Stop.
Flowchart
3. Interactive C Program
#include <stdio.h>
int main() {
int n;
int evenCount = 0, oddCount = 0;
int evenSum = 0, oddSum = 0;
double evenAvg, oddAvg;
printf("Enter integers (0 to stop):\n");
while (1) {
printf("Enter a number: ");
scanf("%d", &n);
if (n == 0)
break;
switch (n % 2) {
case 0:
evenCount++;
evenSum += n;
break;
case 1:
case -1:
oddCount++;
oddSum += n;
break;
}
}
printf("\nNumber of even integers = %d\n", evenCount);
printf("Number of odd integers = %d\n", oddCount);
printf("Sum of even integers = %d\n", evenSum);
printf("Sum of odd integers = %d\n", oddSum);
if (evenCount > 0) {
evenAvg = (double)evenSum / evenCount;
printf("Average of even integers = %.2f\n",
evenAvg);
} else {
printf("Average of even integers = N/A\n");
}
if (oddCount > 0) {
oddAvg = (double)oddSum / oddCount;
printf("Average of odd integers = %.2f\n",
oddAvg);
} else {
printf("Average of odd integers = N/A\n");
}
return 0;
}
Q4. Write an Interactive Program to Generate the Divisiors of a given Integer.
Ans 4.
C Program: Generate the Divisors of a Given Integer
Algorithm
- Start.
- Input an integer n.
- Check whether n is positive. If not, display an error message.
- Repeat from i = 1 to n.
- If n % i == 0, print i because it is a divisor of n.
- Stop.
Flowchart
3. Interactive C Program
#include <stdio.h>
int main() {
int n, i;
printf("Enter a positive integer: ");
scanf("%d", &n);
if (n <= 0) {
printf("Please enter a positive integer.");
return 0;
}
printf("Divisors of %d are: ", n);
for (i = 1; i <= n; i++) {
if (n % i == 0) {
printf("%d ", i);
}
}
return 0;
}
Q 5. Write a program to find all Armstrong Number in the range of 0 and 999
Hint: An Armstrong number of three digits is an Integer such that the sum of the Cubes of its digits is Equal to the Number itself. For Example, 371 is an Armstrong Number since 3**3+7**3+1**3=371.
Ans 5.
C Program: Armstrong Numbers Between 0 and 999
Algorithm
- Start.
- Initialize num = 0.
- Repeat for every number from 0 to 999.
- Store the number in temp.
- Initialize sum = 0.
- Extract each digit using digit = temp % 10.
- Add the cube of the digit to sum.
- Remove the last digit using temp = temp / 10.
- Repeat until temp = 0.
- If sum == num, print the number.
- Continue until num = 999.
- Stop.
Flowchart
3. Interactive C Program
#include <stdio.h>
int main() {
int num, temp, digit, sum;
printf("Armstrong numbers between 0 and 999 are:\n");
for (num = 0; num <= 999; num++) {
temp = num;
sum = 0;
do {
digit = temp % 10;
sum = sum + digit * digit * digit;
temp = temp / 10;
} while (temp > 0);
if (sum == num) {
printf("%d ", num);
}
}
return 0;
}
Q 6. Write a program to check whether a given Number is a Perfect Number or Not. Hint: A Positive Integer N is called a Perfect Number if it is equal to the sum of all of its positive Divisors, Excluding N itself. For Example, 6 is a perfect Number,because 1,2 and 3 are its proper positive Divisors and 1 + 2 3 =6. The next perfect Number is 28 = 1+2+4+7+14. The Next Numbers are 496 and 8128.
Ans 6.
C Program: Armstrong Numbers Between 0 and 999
Algorithm
- Start.
- Initialize num = 0.
- Repeat for every number from 0 to 999.
- Store the number in temp.
- Initialize sum = 0.
- Extract each digit using digit = temp % 10.
- Add the cube of the digit to sum.
- Remove the last digit using temp = temp / 10.
- Repeat until temp = 0.
- If sum == num, print the number.
- Continue until num = 999.
- Stop.
Flowchart
3. Interactive C Program
#include <stdio.h>
int main() {
int num, temp, digit, sum;
printf("Armstrong numbers between 0 and 999 are:\n");
for (num = 0; num <= 999; num++) {
temp = num;
sum = 0;
do {
digit = temp % 10;
sum = sum + digit * digit * digit;
temp = temp / 10;
} while (temp > 0);
if (sum == num) {
printf("%d ", num);
}
}
return 0;
}
Q 7. Write a program to check whether given two number are amicable number or not.
Ans 7.
#include <stdio.h>
int main() {
int a, b, i;
int sumA = 0, sumB = 0;
printf("Enter two positive integers: ");
scanf("%d %d", &a, &b);
if (a <= 0 || b <= 0) {
printf("Please enter positive integers.");
return 0;
}
for (i = 1; i <= a / 2; i++) {
if (a % i == 0)
sumA += i;
}
for (i = 1; i <= b / 2; i++) {
if (b % i == 0)
sumB += i;
}
if (sumA == b && sumB == a)
printf("Amicable Numbers");
else
printf("Not Amicable Numbers");
return 0;
}
Q 8. Write a program to find the roots of a Quadratic Equation
Ans 8.
Algorithm
- Start.
- Input coefficients a, b, and c.
- Calculate D = bb - 4a*c.
- If D > 0, calculate and display two distinct real roots.
- If D == 0, calculate and display equal roots.
- If D < 0, calculate and display complex roots.
- Stop.
#include <stdio.h>
#include <math.h>
int main() {
double a, b, c, D, r1, r2, real, imag;
printf("Enter coefficients a, b and c: ");
scanf("%lf %lf %lf", &a, &b, &c);
if (a == 0) {
printf("Not a quadratic equation.");
return 0;
}
D = b * b - 4 * a * c;
if (D > 0) {
r1 = (-b + sqrt(D)) / (2 * a);
r2 = (-b - sqrt(D)) / (2 * a);
printf("Two distinct real roots: %.2f and %.2f",
r1, r2);
}
else if (D == 0) {
r1 = -b / (2 * a);
printf("Equal roots: %.2f", r1);
}
else {
real = -b / (2 * a);
imag = sqrt(-D) / (2 * fabs(a));
printf("Complex roots: %.2f + %.2fi and %.2f - %.2fi",
real, imag, real, imag);
}
return 0;
}
Q 9. Write an itterative C program to find the sum and average of a given array of elements.
Ans 9.
Algorithm
- Start.
- Input the number of elements n.
- Read n array elements.
- Initialize sum = 0.
- Use a loop to calculate the sum of all elements.
- Calculate average = sum / n.
- Display the sum and average.
- Stop.
#include <stdio.h>
int main() {
int a[100], n, i, sum = 0;
float avg;
printf("Enter number of elements: ");
scanf("%d", &n);
if (n <= 0 || n > 100) {
printf("Invalid number of elements.");
return 0;
}
printf("Enter array elements:\n");
for (i = 0; i < n; i++) {
scanf("%d", &a[i]);
sum += a[i];
}
avg = (float)sum / n;
printf("Sum = %d\n", sum);
printf("Average = %.2f\n", avg);
return 0;
}
Q 10. Write an interactive C Programming to find the second largest and smallest elements in a given single-dimensional array.
Ans 10.
Algorithm
- Start.
- Input the number of elements n.
- Read the array elements.
- Find the largest and smallest distinct elements.
- Find the second largest and second smallest distinct elements.
- Display the results.
- If fewer than three distinct elements exist, display that the second distinct values cannot both be found.
- Stop.
#include <stdio.h>
int main() {
int a[100], n, i, j, temp;
int secondSmall, secondLarge, foundSmall = 0, foundLarge = 0;
printf("Enter number of elements: ");
scanf("%d", &n);
if (n < 2 || n > 100) {
printf("Invalid array size.");
return 0;
}
printf("Enter array elements:\n");
for (i = 0; i < n; i++)
scanf("%d", &a[i]);
// Sort array in ascending order
for (i = 0; i < n - 1; i++) {
for (j = i + 1; j < n; j++) {
if (a[i] > a[j]) {
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
// Find second smallest distinct element
for (i = 1; i < n; i++) {
if (a[i] != a[0]) {
secondSmall = a[i];
foundSmall = 1;
break;
}
}
// Find second largest distinct element
for (i = n - 2; i >= 0; i--) {
if (a[i] != a[n - 1]) {
secondLarge = a[i];
foundLarge = 1;
break;
}
}
if (foundSmall)
printf("Second smallest = %d\n", secondSmall);
else
printf("Second smallest does not exist.\n");
if (foundLarge)
printf("Second largest = %d\n", secondLarge);
else
printf("Second largest does not exist.\n");
return 0;
}
Q11. Write a program to find the largest number in an array.
Ans 11.
#include <stdio.h>
int main()
{
int a[100], n, i, largest;
printf("Enter the number of elements: ");
scanf("%d", &n);
printf("Enter array elements:\n");
for (i = 0; i < n; i++)
{
scanf("%d", &a[i]);
}
largest = a[0];
for (i = 1; i < n; i++)
{
if (a[i] > largest)
{
largest = a[i];
}
}
printf("Largest number = %d\n", largest);
return 0;
}
Q 12. Write a program in C to put te given list of odd and even numbers in two different arrays( Even_Array and Odd_Array).
Ans 12.
#include <stdio.h>
int main()
{
int a[100], Even_Array[100], Odd_Array[100];
int n, i, e = 0, o = 0;
printf("Enter number of elements: ");
scanf("%d", &n);
printf("Enter the elements:\n");
for (i = 0; i < n; i++)
scanf("%d", &a[i]);
for (i = 0; i < n; i++)
{
if (a[i] % 2 == 0)
Even_Array[e++] = a[i];
else
Odd_Array[o++] = a[i];
}
printf("Even numbers: ");
for (i = 0; i < e; i++)
printf("%d ", Even_Array[i]);
printf("\nOdd numbers: ");
for (i = 0; i < o; i++)
printf("%d ", Odd_Array[i]);
return 0;
}
Q 13. Write an Interactive program to do the following Computations by providing the option using the switch statement :
- Add two Matrix
- Subtract Two Matrix
- Multiply Two MATRIX
Ans13.
#include <stdio.h>
int main()
{
int a[10][10], b[10][10], c[10][10];
int r, col, i, j, k, ch;
printf("Enter rows and columns: ");
scanf("%d%d", &r, &col);
printf("Enter first matrix:\n");
for(i = 0; i < r; i++)
for(j = 0; j < col; j++)
scanf("%d", &a[i][j]);
printf("Enter second matrix:\n");
for(i = 0; i < r; i++)
for(j = 0; j < col; j++)
scanf("%d", &b[i][j]);
printf("\n1. Add Matrices\n");
printf("2. Subtract Matrices\n");
printf("3. Multiply Matrices\n");
printf("Enter your choice: ");
scanf("%d", &ch);
switch(ch)
{
case 1:
for(i = 0; i < r; i++)
for(j = 0; j < col; j++)
c[i][j] = a[i][j] + b[i][j];
break;
case 2:
for(i = 0; i < r; i++)
for(j = 0; j < col; j++)
c[i][j] = a[i][j] - b[i][j];
break;
case 3:
for(i = 0; i < r; i++)
for(j = 0; j < col; j++)
c[i][j] = 0;
for(i = 0; i < r; i++)
for(j = 0; j < col; j++)
for(k = 0; k < col; k++)
c[i][j] += a[i][k] * b[k][j];
break;
default:
printf("Invalid choice!");
return 0;
}
printf("Resultant Matrix:\n");
for(i = 0; i < r; i++)
{
for(j = 0; j < col; j++)
printf("%d ", c[i][j]);
printf("\n");
}
return 0;
}
Q 14. Write a program to compute the transpose of a matrix
Ans 14.
#include <stdio.h>
int main()
{
int a[10][10], i, j, r, c;
printf("Enter rows and columns: ");
scanf("%d%d", &r, &c);
printf("Enter matrix elements:\n");
for(i = 0; i < r; i++)
for(j = 0; j < c; j++)
scanf("%d", &a[i][j]);
printf("Transpose of matrix:\n");
for(i = 0; i < c; i++)
{
for(j = 0; j < r; j++)
printf("%d ", a[j][i]);
printf("\n");
}
return 0;
}
Q 15. Write a program to find the inverse of a matrix.
Ans 15.
#include <stdio.h>
int main()
{
float a, b, c, d, det;
printf("Enter 2x2 matrix elements:\n");
scanf("%f%f%f%f", &a, &b, &c, &d);
det = a * d - b * c;
if (det == 0)
printf("Inverse does not exist.");
else
{
printf("Inverse of matrix:\n");
printf("%.2f %.2f\n", d / det, -b / det);
printf("%.2f %.2f\n", -c / det, a / det);
}
return 0;
}
Q 16. Using a function, swap the values of 2 variables.
Ans 16.
#include <stdio.h>
void swap(int *a, int *b)
{
int temp = *a;
*a = *b;
*b = temp;
}
int main()
{
int x, y;
printf("Enter two numbers: ");
scanf("%d%d", &x, &y);
swap(&x, &y);
printf("After swapping: x = %d, y = %d", x, y);
return 0;
}
Q 17. Using function, write a C program to display the prime number between Intervals.
Ans 17.
#include <stdio.h>
int prime(int n)
{
int i;
if (n < 2)
return 0;
for (i = 2; i * i <= n; i++)
if (n % i == 0)
return 0;
return 1;
}
int main()
{
int start, end, i;
printf("Enter the interval: ");
scanf("%d%d", &start, &end);
printf("Prime numbers are: ");
for (i = start; i <= end; i++)
if (prime(i))
printf("%d ", i);
return 0;
}
Q18. Write a program to check whether the number is a prime or Armstong number using user defined function.
Ans 18.
#include <stdio.h>
int prime(int n)
{
int i;
if (n < 2)
return 0;
for (i = 2; i * i <= n; i++)
if (n % i == 0)
return 0;
return 1;
}
int armstrong(int n)
{
int temp = n, sum = 0, digit;
while (temp > 0)
{
digit = temp % 10;
sum += digit * digit * digit;
temp /= 10;
}
return sum == n;
}
int main()
{
int n;
printf("Enter a number: ");
scanf("%d", &n);
if (prime(n))
printf("Prime number\n");
else
printf("Not a prime number\n");
if (n >= 0 && armstrong(n))
printf("Armstrong number\n");
else
printf("Not an Armstrong number\n");
return 0;
}
Q19. USING USER-DEFINED FUNCTION, CHECK WHETHER THE GIVEN YEAR IS A LEAP YEAR OR NOT.
Ans19.
#include <stdio.h>
int leap(int year)
{
if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0))
return 1;
else
return 0;
}
int main()
{
int year;
printf("Enter a year: ");
scanf("%d", &year);
if (leap(year))
printf("Leap year");
else
printf("Not a leap year");
return 0;
}
Q 20. Using Recursion write a C program:
- To find sum of digits of a given Number
- To reverse a given number
- To find the Factorial of a number
- To find Greatest Common Divisor (GCD) of two numbers
- to generate Fiboancci Sequence
Ans 20.
1. C Program to Find the Sum of Digits Using Recursion
#include <stdio.h>
int sum(int n)
{
if (n == 0)
return 0;
return n % 10 + sum(n / 10);
}
int main()
{
int n;
printf("Enter a number: ");
scanf("%d", &n);
printf("Sum of digits = %d", sum(n));
return 0;
}
2. C Program to Reverse a Number Using Recursion
#include <stdio.h>
int reverse(int n, int rev)
{
if (n == 0)
return rev;
return reverse(n / 10, rev * 10 + n % 10);
}
int main()
{
int n;
printf("Enter a number: ");
scanf("%d", &n);
printf("Reverse = %d", reverse(n, 0));
return 0;
}
3. C Program to Find Factorial Using Recursion
#include <stdio.h>
int factorial(int n)
{
if (n <= 1)
return 1;
return n * factorial(n - 1);
}
int main()
{
int n;
printf("Enter a number: ");
scanf("%d", &n);
printf("Factorial = %d", factorial(n));
return 0;
}
4. C Program to Find GCD Using Recursion
#include <stdio.h>
int gcd(int a, int b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
int main()
{
int a, b;
printf("Enter two numbers: ");
scanf("%d%d", &a, &b);
printf("GCD = %d", gcd(a, b));
return 0;
}
5. C Program to Generate Fibonacci Sequence Using Recursion
#include <stdio.h>
int fib(int n)
{
if (n <= 1)
return n;
return fib(n - 1) + fib(n - 2);
}
int main()
{
int n, i;
printf("Enter number of terms: ");
scanf("%d", &n);
for (i = 0; i < n; i++)
printf("%d ", fib(i));
return 0;
}
Q 21 Write a program to convert a given lowercase string to an uppercase string without using the inbuilt string function.
Ans 21.
#include <stdio.h>
int main()
{
char str[100];
int i;
printf("Enter a lowercase string: ");
scanf("%99s", str);
for (i = 0; str[i] != '\0'; i++)
{
if (str[i] >= 'a' && str[i] <= 'z')
str[i] = str[i] - 32;
}
printf("Uppercase string: %s", str);
return 0;
}
Q 22. Write a program the number of vowels, consonants, and spaces in a given string.
Ans 22.
#include <stdio.h>
int main()
{
char str[100];
int i, vowels = 0, consonants = 0, spaces = 0;
printf("Enter a string: ");
fgets(str, sizeof(str), stdin);
for (i = 0; str[i] != '\0'; i++)
{
char ch = str[i];
if (ch == ' ')
spaces++;
else if (ch >= 'A' && ch <= 'Z')
ch = ch + 32;
if (ch >= 'a' && ch <= 'z')
{
if (ch == 'a' || ch == 'e' || ch == 'i' ||
ch == 'o' || ch == 'u')
vowels++;
else
consonants++;
}
}
printf("Vowels = %d\n", vowels);
printf("Consonants = %d\n", consonants);
printf("Spaces = %d\n", spaces);
return 0;
}
Q 23. Write a program to input a string and output the reversed string, i.e., if
"USF" is input; the program has to output "FSU." You are not to use array notation to access the characters; instead, use pointer notation.
Ans 23.
#include <stdio.h>
int main()
{
char str[100], *p;
int len = 0;
printf("Enter a string: ");
scanf("%99s", str);
p = str;
while (*(p + len) != '\0')
len++;
printf("Reversed string: ");
for (p = p + len - 1; len > 0; len--, p--)
printf("%c", *p);
return 0;
}
Q 24. Write a program to find the length of the string without using the inbuilt string function,instead use pointer notation.
Ans 24.
#include <stdio.h>
int main()
{
char str[100], *p;
int len = 0;
printf("Enter a string: ");
fgets(str, sizeof(str), stdin);
p = str;
while (*p != '\0' && *p != '\n')
{
len++;
p++;
}
printf("Length of string = %d", len);
return 0;
}
Q 25. Write a program to process the Students - evaluation records using structures.
Ans 25.
#include <stdio.h>
struct Student
{
int roll;
char name[50];
float marks;
};
int main()
{
struct Student s[100];
int n, i;
printf("Enter number of students: ");
scanf("%d", &n);
for (i = 0; i < n; i++)
{
printf("\nEnter Roll No, Name and Marks:\n");
scanf("%d %49s %f", &s[i].roll, s[i].name, &s[i].marks);
}
printf("\nStudent Evaluation Records\n");
printf("Roll No\tName\tMarks\tResult\n");
for (i = 0; i < n; i++)
{
printf("%d\t%s\t%.2f\t", s[i].roll, s[i].name, s[i].marks);
if (s[i].marks >= 40)
printf("Pass\n");
else
printf("Fail\n");
}
return 0;
}
Q 26. Using Structures, write a program to process the electricity Bill.
Ans 26.
#include <stdio.h>
struct Electricity
{
int consumer_no;
char name[50];
int units;
float bill;
};
int main()
{
struct Electricity e;
printf("Enter consumer number: ");
scanf("%d", &e.consumer_no);
printf("Enter consumer name: ");
scanf("%49s", e.name);
printf("Enter units consumed: ");
scanf("%d", &e.units);
if (e.units <= 100)
e.bill = e.units * 1.50;
else if (e.units <= 200)
e.bill = 100 * 1.50 + (e.units - 100) * 2.50;
else
e.bill = 100 * 1.50 + 100 * 2.50 + (e.units - 200) * 4.00;
printf("\n--- Electricity Bill ---\n");
printf("Consumer Number: %d\n", e.consumer_no);
printf("Consumer Name: %s\n", e.name);
printf("Units Consumed: %d\n", e.units);
printf("Total Bill: Rs. %.2f\n", e.bill);
return 0;
}
Q 27 Using Structure, write an interactive C programming to process the retail medical strore bill.
Ans 27.
#include <stdio.h>
struct Medicine
{
char name[50];
int quantity;
float price, amount;
};
int main()
{
struct Medicine m[100];
int n, i;
float total = 0;
printf("Enter number of medicines: ");
scanf("%d", &n);
for (i = 0; i < n; i++)
{
printf("\nEnter medicine name: ");
scanf("%49s", m[i].name);
printf("Enter quantity: ");
scanf("%d", &m[i].quantity);
printf("Enter price per unit: ");
scanf("%f", &m[i].price);
m[i].amount = m[i].quantity * m[i].price;
total += m[i].amount;
}
printf("\n----- MEDICAL STORE BILL -----\n");
printf("Medicine\tQty\tPrice\tAmount\n");
for (i = 0; i < n; i++)
{
printf("%s\t\t%d\t%.2f\t%.2f\n",
m[i].name, m[i].quantity,
m[i].price, m[i].amount);
}
printf("------------------------------\n");
printf("Total Bill = Rs. %.2f\n", total);
return 0;
}
Q 28 . Write a program that prompts the user the name of a file and then counts and display
Comments
Join the discussionNo comments yet. Be the first to comment.


