Q1. Covert degrees Celsius to Fahrenheit and vice versa ?
#include<stdio.h>
#include<conio.h>
void main()
{
float celsius, fahrenheit ;
clrscr();
printf("Enter the degree celsius");
scanf("%f",&celsius);
fahrenheit=(celsius*9/5)+32;
printf("temprature in fahrenheit: %.2f",fahrenheit) ;
getch();
}
Output:
Enter the degree Celsius 34
Temperature in Fahrenheit : 93.19997
Q2 . Display three input numbers in sorted (non-decreasing) order ?
#include<stdio.h>
#include<conio.h>
void main()
{
int num1 ,num2,num3,temp;
printf("Enter the three numbers :");
scanf("%d%d%d",&num1,&num2,&num3);
if(num1>num2)
{
temp=num1;
num1=num2;
num2=temp;
}
if(num2>num3)
{
temp=num2;
num2=num3;
num3=temp;
}
if(num1>num2) //for rechecking
{
temp=num1;
num1=num2;
num2=temp;
}
printf("numbers in Non decreasing order %d %d %d ",num1,num2,num3);
getch();
}
Output :
Enter the three numbers : 6
3
9
numbers in Non decreasing order 3 6 9
Q3. Given a positive integer value n(>=0)display number ,square and cube of number from
1 to n in a tabular format ?
#include<stdio.h>
#include<conio.h>
void main()
{
int i,n,square,cube;
clrscr();
printf("Enter the number");
scanf("%d",&n);
if(n>=0)
{
printf("| number | square | cube |");
for(i=1;i<=n;i++)
{
printf("\n| %4d | %4d | %4d |",i,i*i,i*i*i);
}
}
else
printf("Please enter positive integer value") ;
getch();
}
Output :
Enter the number 5
| number | square | cube|
| 1 | 1 | 1|
| 2 | 4 | 8|
| 3 | 9 | 27|
| 4 | 16 | 64|
| 5 | 25 | 125|
Q4. Given an input positive integer number, display odd numbers from in the range 1,n]?
#include<stdio.h>
#include<conio.h>
void main()
{
int i,n ,odd;
clrscr();
printf("Enter the range of odd numbers:");
scanf("%d",&n);
for(i=1;i<=n;i++)
if(i%2!=0)
{
printf("\n %d",i);
}
getch();
}
Output :
Enter the range of odd numbers: 10
1
3
5
7
9
Q5. Display first mathematical tables, each table upto10 rows? Generalise this to display
first n (> 0) mathematical tables up to m (m > 0) rows?
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,n,table;
clrscr();
printf("Enter the range of tables:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
for(j=1;j<=10;j++)
{
printf("\n %d * %d = %d ",i,j,i*j);
}
}
getch();
}
Output :
Enter the range of tables 2
Table of 1
1*1=1
1*2 =2
1*3 =3
1*4=4
1*5=5
1*6=6
1*7=7
1*8=8
1*9=9
1 * 10 = 10
Q6 Display the following patterns of n rows(n>0), for the below examples n=5? For each
pattern write a separate algorithm/program?
(I).$
$$
$$$
$$$$
$$$$$
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,n;
clrscr();
printf("Enter the range:") ;
scanf("%d",&n);
for(i=0;i<=n;i++)
{
for(j=0;j<=i;j++)
{
printf("*");
}
Printf(“\n”);
}
getch();
}
Output :
Enter the range 5
$
$$
$$$
$$$$
$$$$$
Q6 (ii). $
$$
$$$
$$$$
$$$$$
#include<stdio.h>
#include<conio.h>
void main()
{
int i,n,j,k,l;
clrscr();
printf("Enter the range");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
for(j=1;j<=n-i;j++)
{
printf(" ");
}
for(k=1;k<=i;k++)
{
printf("*");
}
printf("\n");
}
getch();
}
Output :
Enter the range 5
$
$$
$$$
$$$$
$$$$$
Q6 (iii).12345
1234
123
12
1
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,n;
clrscr();
printf("Enter the range: ");
scanf("%d",&n);
for(i=n;i>=1;i--)
{
for(j=1;j<i;j++)
{
printf("%d",j);
}
printf("\n");
}
getch();
}
Output :
Enter the range 6
12345
1234
123
12
1
Q6(iv). 12345
1234
123
12
1
#include<stdio.h>
#include<conio.h>
void main()
{
int n, i,j,k;
clrscr();
printf("Enter the range: ");
scanf("%d",&n);
for(i=0;i<=n;i++)
{
for(j=1;j<=i;j++)
{
printf(" ");
}
for(k=1;k<=n-i;k++)
{
printf("%d",k);
}
printf("\n");
}
getch();
}
Output :
Enter the range 5
12345
1234
123
12
1
Q7. Display following patterns of n rows(n>0), For the below examples n = 5?
(i) Hollow square pattern
######
#. #
#. #
#. #
#. #
######
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j;
clrscr();
printf("enter the range: ");
scanf("%d",&n);
for(i=1;i=n;i++)
{
for(j=1;j=n;j++)
{
if(i==1||i==n||j==1||j==n)
{
printf("#");
}
else
{
printf(“ ”);
}
printf("\n");
}
getch();
}
Output : enter the range: 6
######
# #
# #
# #
# #
######
(ii) Triangle Pattern with numbers
1
121
12321
#include <stdio.h>
#include <conio.h>
void main()
{
int i, j, n, m, k;
printf("Enter number of rows: ");
scanf("%d", &n);
for(i = 1; i <= n; i++)
{
for(k = n-1; k>=i; k--)
printf(" ");
for(j = 1; j <= i; j++)
printf("%d", j);
for(m=i-1; m>0; m--)
printf("%d", m);
printf("\n");
}
getch();
}
Output :
Enter number of rows: 4
1
121
12321
1234321
(iii) Square with diagonal
******
** **
* ***
** **
** **
******
#include <stdio.h>
#include <conio.h>
void main()
{
int i, j, n;
clrscr();
printf("enter the number");
scanf("%d",&n);
for(i = 0; i <= n; i++)
{
for(j = 0; j <= n; j++)
{
if(i == 0 || i == n-1 || j == 0 || j == n-1 || i == j || j == (n - 1-i))
{
printf("*");
}
else
{
printf(" ");
}
}
printf("\n");
}
getch();
}
Output :
Enter the number: 10
***********
** **
** **
* * * *
* ** *
* ** *
* * * *
** **
** **
***********
* **
(iv) Diamond pattern
*
***
*****
***
*
#include <stdio.h>
#include <conio.h>
void main()
{
int i, j, n, space;
clrscr();
printf("Enter the number of rows (half of diamond): ");
scanf("%d", &n);
// Upper half of diamond
for(i = 1; i <= n; i++)
{
for(space = i; space < n; space++)
printf(" ");
for(j = 1; j <= (2*i - 1); j++)
printf("*");
printf("\n");
}
// Lower half of diamond
for(i = n-1; i >= 1; i--)
{
for(space = n; space > i; space--)
printf(" ");
for(j = 1; j <= (2*i - 1); j++)
printf("*");
printf("\n");
}
getch();
}
Output :
Enter the number of rows (half of diamond): 6
*
***
*****
*******
*********
***********
*********
*******
****
***
*
Q8. . Given the first term (a), difference/multiplier (d) and number of terms (n > 0) ,
display the first n terms of the arithmetic/geometric progression
#include <stdio.h>
#include <conio.h>
void main()
{
int n, i, choice;
float a, d, term;
clrscr();
printf("Enter the first term (a): ");
scanf("%f", &a);
printf("Enter the difference or multiplier (d): ");
scanf("%f", &d);
printf("Enter the number of terms (n): ");
scanf("%d", &n);
printf("\nChoose the type of progression:\n");
printf("1. Arithmetic Progression (AP)\n");
printf("2. Geometric Progression (GP)\n");
printf("Enter your choice (1 or 2): ");
scanf("%d", &choice);
printf("\nThe series is:\n");
if(choice == 1) // Arithmetic Progression
{
term = a;
for(i = 1; i <= n; i++)
{
printf("%.2f ", term);
term = term + d;
}
}
else if(choice == 2) // Geometric Progression
{
term = a;
for(i = 1; i <= n; i++)
{
printf("%.2f ", term);
term = term * d;
}
}
else
{
printf("Invalid choice!");
}
getch();
}
Output:
Enter the first term (a): 2
Enter the difference or multiplier (d): 3
Enter the number of terms (n): 5
Choose the type of progression:
1. Arithmetic Progression (AP)
2. Geometric Progression (GP)
Enter your choice (1 or 2): 1
Q9. Display the first n(n > 0) terms of the Fibonacci sequence?
#include <stdio.h>
#include <conio.h>
void main()
{
int n, i;
int a = 0, b = 1, c;
clrscr();
printf("Enter the number of terms (n > 0): ");
scanf("%d", &n);
printf("\nFibonacci Series:\n");
if(n <= 0)
printf("Invalid input! Please enter n > 0");
else if(n == 1)
printf("%d", a);
else
{
printf("%d %d ", a, b);
for(i = 3; i <= n; i++)
{
c = a + b;
printf("%d ", c);
a = b;
b = c;
}
}
getch();
}
Output :
Enter the number of terms (n > 0): 7
Fibonacci Series:
0112358
Q10. Display the n(n > 0) terms of the Tribonacci sequence?
#include <stdio.h>
#include <conio.h>
void main()
{
int n, i;
int a = 0, b = 1, c = 1, d;
clrscr();
printf("Enter the number of terms (n > 0): ");
scanf("%d", &n);
printf("\nTribonacci Series:\n");
if(n <= 0)
printf("Invalid input! Please enter n > 0");
else if(n == 1)
printf("%d", a);
else if(n == 2)
printf("%d %d", a, b);
else if(n == 3)
printf("%d %d %d", a, b, c);
else
{
printf("%d %d %d ", a, b, c);
for(i = 4; i <= n; i++)
{
d = a + b + c;
printf("%d ", d);
a = b;
b = c;
c = d;
}
}
getch();
}
Output :
Enter the number of terms (n > 0): 8
Tribonacci Series:
0 1 1 2 4 7 13 24
Q11. Given two positive integer numbers n1 and n2 check if the numbers are consecutive
numbers of the Fibonacci sequence?
#include <stdio.h>
#include <conio.h>
void main()
{
int n1, n2;
int a = 0, b = 1, c;
int found = 0;
clrscr();
printf("Enter two positive integers: ");
scanf("%d %d", &n1, &n2);
if(n1 < 0 || n2 < 0)
{
printf("Please enter positive numbers only.");
}
// Generate Fibonacci numbers and check consecutiveness
while(b <= n2)
{
if(a == n1 && b == n2)
{
found = 1;
break;
}
c = a + b;
a = b;
b = c;
}
if(found == 1)
printf("\n%d and %d are consecutive Fibonacci numbers.", n1, n2);
else
printf("\n%d and %d are NOT consecutive Fibonacci numbers.", n1, n2);
getch();
}
Output :
Enter two positive integers: 5 8
5 and 8 are consecutive Fibonacci numbers.
Q12. Compute approximate value of n considering first n(n > 0) terms of the Taylor series
for n?
#include <stdio.h>
#include <conio.h>
void main()
{
int n, i;
float x, term = 1.0, sum = 1.0; // start with 1 for 0th term
clrscr();
printf("Enter the value of x: ");
scanf("%f", &x);
printf("Enter the number of terms (n > 0): ");
scanf("%d", &n);
for(i = 1; i < n; i++)
{
term = term * x / i; // compute x^i / i!
sum = sum + term;
}
printf("\nApproximate value of e^%.2f using %d terms = %.5f", x, n, sum);
getch();
}
Output:
Enter the value of x: 2
Enter the number of terms (n > 0): 10
Approximate value of e^2.00 using 10 terms = 7.38899
Q13. Compute approximate value of ex considering first n(n > 0) terms of the Taylor series
for e^x
#include <stdio.h>
#include <conio.h>
void main()
{
int n, i;
float x, term = 1.0, sum = 1.0; // start with 1 for 0th term
clrscr();
printf("Enter the value of x: ");
scanf("%f", &x);
printf("Enter the number of terms (n > 0): ");
scanf("%d", &n);
for(i = 1; i < n; i++)
{
term = term * x / i; // compute x^i / i!
sum = sum + term;
}
printf("\nApproximate value of e^%.2f using %d terms = %.5f", x, n, sum);
getch();
}
Output :
Enter the value of x: 2
Enter the number of terms (n > 0): 10
Approximate value of e^2.00 using 10 terms = 7.38899
Q14. Compute approximate value of sin(x)/cos(x) considering firstn (n > 0) termsofthe
Taylor series for (sin(x)) / (cos(x)) ?
#include <stdio.h>
#include <conio.h>
void main()
{
int n, i;
float x, sinx = 0.0, cosx = 0.0, term;
int sign;
clrscr();
printf("Enter the value of x (in radians): ");
scanf("%f", &x);
printf("Enter the number of terms (n > 0): ");
scanf("%d", &n);
// Calculate sin(x)
term = x;
sign = 1;
for(i = 1; i <= n; i++)
{
sinx += sign * term;
term = term * x * x / ((2 * i) * (2 * i + 1));
sign = -sign;
}
// Calculate cos(x)
term = 1;
sign = 1;
for(i = 1; i <= n; i++)
{
cosx += sign * term;
term = term * x * x / ((2 * i - 1) * (2 * i));
sign = -sign;
}
if(cosx == 0)
printf("\nCannot compute tan(x): cos(x) is approximately zero.");
else
printf("\nApproximate value of sin(x)/cos(x) = tan(x) ≈ %.5f", sinx / cosx);
getch();
}
Output:
Enter the value of x (in radians): 0.7854
Enter the number of terms (n > 0): 10
Approximate value of sin(x)/cos(x) = tan(x) ≈ 1.00000
Q 15. Extract digits of an integer number (left to right and right to left)?
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int num, temp, rev=0, digit;
printf("enter the integer number :");
scanf("%d", &num);
temp = num;
printf(" \n digits from right to left :");
while (temp > 0)
{
digit=temp % 10;
printf("%d", digit);
rev= rev * 10 + digit;
temp=temp/10;
}
printf("\n digits from left to right :");
while (rev > 0)
{
digit=rev % 10;
printf ("%d", digit);
rev=rev / 10;
}
getch();
}
Output:
Enter the integer number: 1234
digits from right to left : 4321
digits from left to right : 1234
Q16. Given a sequence of digits form the number composed of the digits. Use sentinel
Controlled repetition to read the digits followed by-1.
#include <stdio.h>
#include <conio.h>
void main()
{
int digit, number = 0;
clrscr();
printf("Enter digits one by one (enter -1 to stop):\n");
while (1)
{
scanf("%d", &digit);
if (digit == -1) // sentinel condition
break;
number = number * 10 + digit; // form number
}
printf("\nThe formed number is: %d", number);
getch();
}
OUTPUT
Enter digits one by one (enter -1 to stop):
2
7
3
2
9
-1
The formed number is: 27329
Q17. Check if a given positive number is a palindrome or not?
# include <stdio.h>
# include <conio.h>
void main ()
{
int n ,r ,s =0,t ;
clrscr();
printf(“enter the number”);
scanf(“%d”,&n);
t=n;
while (n)
{
r=n%10;
n=n/10;
s=s*10 +r;
}
if (t ==s)
printf (“\n the number % d is Palindrome” , t) ;
else
printf( “\n The number % d is Not Palinddrome“ ,t );
getch( );
}
Output :
Enter the Number to check Palindrome 12321
The number 12321 is Palindrome
Q18. Compute character grade from the marks (0 ≤ marks ≤ 100) of a subject. Grading
Scheme 80-100: A, 60-79: B, 50-59: C, 40-49: D, 0-39: F? Solve this using both else-if ladder
and switch case?
//Using Else-if Ladder
#include <stdio.h>
#include <conio.h>
void main()
{
int marks;
char grade;
clrscr();
printf("Enter marks (0 - 100): ");
scanf("%d", &marks);
if (marks >= 80 && marks <= 100)
grade = 'A';
else if (marks >= 60)
grade = 'B';
else if (marks >= 50)
grade = 'C';
else if (marks >= 40)
grade = 'D';
else if (marks >= 0)
grade = 'F';
else
{
printf("Invalid marks!");
}
printf("\nGrade = %c", grade);
getch();
}
OUTPUT
Enter marks (0 - 100): 78
Grade = B
//Using Switch Case
#include <stdio.h>
#include <conio.h>
void main()
{
int marks;
char grade;
clrscr();
printf("Enter marks (0 - 100): ");
scanf("%d", &marks);
switch (marks / 10)
{
case 10:
case 9:
case 8: grade = 'A'; break;
case 7:
case 6: grade = 'B'; break;
case 5: grade = 'C'; break;
case 4: grade = 'D'; break;
default: grade = 'F';
}
printf("\nGrade = %c", grade);
getch();
}
OUTPUT
Enter marks (0 - 100): 63
Grade = B
[Link] the sum of a sequence of numbers entered using sentinel controlled
repetition?
#include <stdio.h>
#include <conio.h>
void main()
{
int num;
int sum = 0;
clrscr();
printf("Enter numbers to add (enter -1 to stop):\n");
while (1)
{
printf("Enter number: ");
scanf("%d", &num);
if (num == -1) // sentinel value
break;
sum += num;
}
printf("\nThe sum of the entered numbers is: %d", sum);
getch();
}
OUTPUT
Enter numbers to add (enter -1 to stop):
Enter number: 10
Enter number: 20
Enter number: 30
Enter number: -1
The sum of the entered numbers is: 60
Q20. Check if a given positive integer number is prime number or not?
#include <stdio.h>
#include <conio.h>
void main()
{
int n, i, count = 0;
clrscr();
printf("Enter the number to check for prime: ");
scanf("%d", &n);
if (n <= 1) {
printf("The given number %d is NOT a prime number.", n);
}
for (i = 2; i<= n / 2; i++) {
if (n % i == 0) {
count++;
break;
}
}
if (count == 0)
printf("The given number %d is a PRIME number.", n);
else
printf("The given number %d is NOT a prime number.", n);
getch();
}
Output
Enter the Number to check prime number 5
The given number 5 is prime number
Q21. Compute prime factors of a positive integer number.
#include <stdio.h>
#include <conio.h>
void main()
{
int n, i;
clrscr();
printf("Enter a positive integer: ");
scanf("%d", &n);
printf("Prime factors of %d are: ", n);
// Step 1: Divide by 2 while even
while (n % 2 == 0)
{
printf("2 ");
n = n / 2;
}
// Step 2: Divide by odd numbers starting from 3
for (i = 3; i<= n; i += 2)
{
while (n % i == 0)
{
printf("%d ", i);
n = n / i;
}
}
getch();
}
OUTPUT :
Enter a positive integer: 60
Prime factors of 60 are: 2 2 3 5
[Link] if two positive integer numbers are amicable number or not.
#include <stdio.h>
#include <conio.h>
void main()
{
int n1, n2, i, sum1 = 0, sum2 = 0;
clrscr();
printf("Enter first number: ");
scanf("%d", &n1);
printf("Enter second number: ");
scanf("%d", &n2);
// Calculate sum of divisors of n1
for (i = 1; i< n1; i++)
{
if (n1 % i == 0)
sum1 += i;
}
// Calculate sum of divisors of n2
for (i = 1; i< n2; i++)
{
if (n2 % i == 0)
sum2 += i;
}
if (sum1 == n2 && sum2 == n1)
printf("%d and %d are Amicable Numbers", n1, n2);
else
printf("%d and %d are Not Amicable Numbers", n1, n2);
getch();
}
OUTPUT
Enter first number: 1184
Enter second number: 1210
1184 and 1210 are Amicable Numbers
[Link] if a given positive number is perfect number or not ?
#include <stdio.h>
#include <conio.h>
void main()
{
int n, i, sum = 0;
clrscr();
printf("Enter a positive number: ");
scanf("%d", &n);
for (i = 1; i< n; i++)
{
if (n % i == 0)
sum = sum + i;
}
if (sum == n)
printf("%d is a Perfect Number", n);
else
printf("%d is not a Perfect Number", n);
getch();
}
OUTPUT
Enter a positive number: 6
6 is a Perfect Number
[Link] if a given positive number is Armstrong number or not?
#include <stdio.h>
#include <conio.h>
void main()
{
int n, c, r, s = 0;
clrscr();
printf("Enter the number: ");
scanf("%d", &n);
c = n;
while (n > 0)
{
r = n % 10;
s = (r * r * r) + s;
n = n / 10;
}
if (c == s)
printf("\nThe number %d is an Armstrong number.", c);
else
printf("\nThe number %d is NOT an Armstrong number.", c);
getch();
}
Output :
Enter the number: 153
The number is an Armstrong number
[Link] a positive integer number (n > 0) from one base (input Base) to another
base (output Base) (2 <= input Base, output Base <= 10). Input number should be validated
before converting to make sure the number uses only digits allowed in the input base.
#include <stdio.h>
#include <conio.h>
#include <math.h>
void main() {
int inputBase, outputBase, num, temp, valid = 1;
int decimal = 0, rem, power = 0, result[50], i = 0, j;
clrscr(); // clear the screen
printf("Enter input base (2 to 10): ");
scanf("%d", &inputBase);
printf("Enter output base (2 to 10): ");
scanf("%d", &outputBase);
printf("Enter number in base %d: ", inputBase);
scanf("%d", &num);
temp = num;
// --- Validation ---
while (temp > 0) {
rem = temp % 10;
if (rem >= inputBase) {
valid = 0;
break;
}
temp = temp / 10;
}
if (!valid) {
printf("\nInvalid number for base %d!", inputBase);
getch();
} else {
// --- Convert input number to decimal ---
temp = num;
power = 0;
decimal = 0;
while (temp > 0) {
rem = temp % 10;
decimal = decimal + (rem * (int)pow(inputBase, power));
power++;
temp = temp / 10;
}
// --- Convert decimal to output base ---
while (decimal > 0) {
result[i] = decimal % outputBase;
decimal = decimal / outputBase;
i++;
}
printf("\nThe number %d in base %d = ", num, inputBase);
for (j = i - 1; j >= 0; j--) {
printf("%d", result[j]);
}
printf(" in base %d", outputBase);
getch();
}
}
OUTPUT
Enter input base (2 to 10): 2
Enter output base (2 to 10): 8
Enter number in base 2: 1011
The number 1011 in base 2 = 13 in base 8
Q26. Write a program to display a number in text form.
#include <stdio.h>
#include <conio.h>
void main()
{
int num, rev = 0, digit, temp;
clrscr();
printf("Enter a positive number: ");
scanf("%d", &num);
temp = num;
// Reverse the number
while (temp > 0)
{
digit = temp % 10;
rev = rev * 10 + digit;
temp = temp / 10;
}
printf("In words: ");
// Print digits in words from the reversed number
while (rev > 0)
{
digit = rev % 10;
switch (digit)
{
case 0: printf("Zero "); break;
case 1: printf("One "); break;
case 2: printf("Two "); break;
case 3: printf("Three "); break;
case 4: printf("Four "); break;
case 5: printf("Five "); break;
case 6: printf("Six "); break;
case 7: printf("Seven "); break;
case 8: printf("Eight "); break;
case 9: printf("Nine "); break;
}
rev = rev / 10;
}
getch();
}
OUTPUT
Enter a positive number: 507
In words: Five Zero Seven
[Link] the grading scheme described in the question 4(UNIT III), Compute how many
Students awarded each grade and display the frequency as a bar chart (horizontal) using
Single "*" for each student. Use sentinel controlled repetition (-1 a sentinel value)in
reading the students marks. Use else-if ladder/switch case to compute the grade and the
corresponding frequency.
#include <stdio.h>
#include <conio.h>
void main()
{
int marks;
int countA = 0, countB = 0, countC = 0, countD = 0, countF = 0;
int i; // declare here for Turbo C
char grade;
clrscr(); // clear screen
printf("Enter marks of students (0 - 100), enter -1 to stop:\n");
while (1)
{
scanf("%d", &marks);
if (marks == -1) // Sentinel condition
break;
if (marks < 0 || marks > 100)
{
printf("Invalid marks! Enter between 0 - 100.\n");
continue;
}
// --- Compute Grade using else-if ladder ---
if (marks >= 80)
{
grade = 'A';
countA++;
}
else if (marks >= 60)
{
grade = 'B';
countB++;
}
else if (marks >= 50)
{
grade = 'C';
countC++;
}
else if (marks >= 40)
{
grade = 'D';
countD++;
}
else
{
grade = 'F';
countF++;
}
printf("Grade = %c\n", grade);
}
// --- Display Frequency Bar Chart ---
printf("\nGRADE FREQUENCY BAR CHART\n");
printf("--------------------------\n");
printf("A: ");
for (i = 0; i<countA; i++)
printf("*");
printf(" (%d)\n", countA);
printf("B: ");
for (i = 0; i<countB; i++)
printf("*");
printf(" (%d)\n", countB);
printf("C: ");
for (i = 0; i<countC; i++)
printf("*");
printf(" (%d)\n", countC);
printf("D: ");
for (i = 0; i<countD; i++)
printf("*");
printf(" (%d)\n", countD);
printf("F: ");
for (i = 0; i<countF; i++)
printf("*");
printf(" (%d)\n", countF);
getch();
}
OUTPUT
Enter marks of students (0 - 100), enter -1 to stop:
86
Grade = A
76
Grade = B
43
Grade = D
39
Grade = F
-1
GRADE FREQUENCY BAR CHART
--------------------------
A: * (1)
B: * (1)
C: (0)
D: * (1)
F: * (1)
Q28. Sample bar chart when the class has 7-A, 10-B, 3-C, 7-D and 1-F grades.
#include <stdio.h>
#include <conio.h>
void main()
{
int A = 7, B = 10, C = 3, D = 7, F = 1, i;
clrscr();
printf("Sample Bar Chart\n\n");
printf("A: ");
for (i = 1; i<= A; i++)
printf("*");
printf("\n");
printf("B: ");
for (i = 1; i<= B; i++)
printf("*");
printf("\n");
printf("C: ");
for (i = 1; i<= C; i++)
printf("*");
printf("\n");
printf("D: ");
for (i = 1; i<= D; i++)
printf("*");
printf("\n");
printf("F: ");
for (i = 1; i<= F; i++)
printf("*");
printf("\n");
getch();
}
OUTPUT
Sample Bar Chart
A: *******
B: **********
C: ***
D: *******
F: *
[Link] maximum, minimum, sum and average of a sequence of numbers which are
read using sentinel controlled repetition using only few variables?
#include <stdio.h>
#include <conio.h>
void main()
{
int num, sum = 0, count = 0;
int max, min;
float avg;
clrscr();
printf("Enter numbers (-1 to stop):\n");
scanf("%d", &num);
if (num == -1)
{
printf("No numbers entered.");
getch();
return;
}
// Initialize max and min with the first number
max = min = num;
while (num != -1)
{
sum += num;
count++;
if (num > max)
max = num;
if (num < min)
min = num;
scanf("%d", &num);
}
avg = (float)sum / count;
printf("\nSum = %d", sum);
printf("\nAverage = %.2f", avg);
printf("\nMaximum = %d", max);
printf("\nMinimum = %d", min);
getch();
}
OUTPUT
Enter numbers (-1 to stop):
5
8
3
10
-1
Sum = 26
Average = 6.50
Maximum = 10
Minimum = 3
[Link] body mass index, BMI=weight in KGs/(Height in Meters *Height in Meters),
Both weight and height values are positive real numbers. Your program should display
BMI value followed by whether the person is Underweight, Normal, Overweight or Obese
using the below ranges:
BMI Values
Underweight:lessthan18.5 Normal: >=18.5 and <25
Overweight:>=25and <30
Obese:>=30
#include <stdio.h>
#include <conio.h>
void main()
{
float weight, height, bmi;
clrscr();
printf("Enter weight (in kilograms): ");
scanf("%f", &weight);
printf("Enter height (in meters): ");
scanf("%f", &height);
// calculate BMI
bmi = weight / (height * height);
printf("\nYour BMI = %.2f\n", bmi);
// check category
if (bmi< 18.5)
printf("You are Underweight.");
else if (bmi>= 18.5 &&bmi< 25)
printf("You are Normal.");
else if (bmi>= 25 &&bmi< 30)
printf("You are Overweight.");
else
printf("You are Obese.");
getch();
}
OUTPUT
Enter weight (in kilograms): 68
Enter height (in meters): 1.70
Your BMI = 23.53
You are Normal.