[go: up one dir, main page]

0% found this document useful (0 votes)
144 views149 pages

C Solved Problems PDF

The document contains 14 programs demonstrating the use of decision control structures and basic mathematical operations in C programming. The programs include: 1) Comparing two numbers and displaying the largest 2) Calculating total price by multiplying quantity and price 3) Finding discount amount and selling price of a product 4) Calculating net salary after tax and allowance deductions 5) Performing basic conversions between units like rupees to dollars, hours to seconds, etc. 6) Computing square, cube, and square root of a number 7) Finding area, circumference of a circle using predefined constants 8) Calculating simple interest and final amount given principal, rate, and time

Uploaded by

dinesh ghemosu
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)
144 views149 pages

C Solved Problems PDF

The document contains 14 programs demonstrating the use of decision control structures and basic mathematical operations in C programming. The programs include: 1) Comparing two numbers and displaying the largest 2) Calculating total price by multiplying quantity and price 3) Finding discount amount and selling price of a product 4) Calculating net salary after tax and allowance deductions 5) Performing basic conversions between units like rupees to dollars, hours to seconds, etc. 6) Computing square, cube, and square root of a number 7) Finding area, circumference of a circle using predefined constants 8) Calculating simple interest and final amount given principal, rate, and time

Uploaded by

dinesh ghemosu
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/ 149

General Programs:

1. Write a program to ask number of quantities and price for per quantity then find total
price.
#include<stdio.h>
#include<conio.h>
void main()
{
int qty, price, amount;
clrscr();
printf("\n Input Number of quantity:- ");
scanf("%d", &qty);
printf("\n Input Price per quantity:- ");
scanf("%d", &price);
amount=qty*price;
printf("\n Total Price = Rs %d", amount);
getch();
}

2. Write a program to read mark price of radio and find discount which is 40% of mark price
then find actual selling price of radio.
#include<stdio.h>
#include<conio.h>
void main()
{
int price, discount,
sp; clrscr();
printf("\n Input Price of a Radio:-");
scanf("%d", &price);
discount = price*0.4;
sp=price-discount;
printf("\n Selling Price = Rs %d",sp);
getch();
}

3. Write a program to read basic salary then find tax and allowance, tax is 15% of basic salary
and allowance 25% of basic salary. Also find out net salary.
#include<stdio.h>
#include<conio.h>
void main()
{
int bs;
float tax, allowance, net_salary;
clrscr();
printf("\n Input Basic Salary:-");
scanf("%d", &bs);
tax = bs*0.15;
allowance = bs*0.25;
net_salary = bs + allowance - tax;
printf("\n Tax = Rs %0.2f", tax);
printf("\n Allowance = Rs %0.2f", allowance);
printf("\n Net Salary = Rs %0.2f",net_salary);
getch();
}

4. Write a program to find area of circle

| Structured Programming 1
#include<stdio.h>
#include<conio.h>
void main()
{
float radius,
area; clrscr();
printf("\n Input Radius of a Circle (in cm):-
"); scanf("%f", &radius);
area = 2*3.14*radius;
printf("\n Area = %0.2f cm2", area);
getch();
}

5. Write a program to convert followings:


a) Rupees into dollar [1 dollar = 89.58 Rs]
#include<stdio.h>
#include<conio.h>
void main()
{
int rs; float
dollar;
clrscr();
printf("\n Input Rupees:-
"); scanf("%d", &rs);
dollar = rs / 89.58;
printf("\n %d Rupees = %.2f dollar", rs, dollar);
getch();
}

b) Hour into seconds


#include<stdio.h>
#include<conio.h>
void main()
{
int hours;
long int sec;
clrscr();
printf("\n Input Time (in Hours):- ");
scanf("%d", &hours);
sec = hours*60*60;
printf("\n %d Hours = %ld Seconds", hours, sec);
getch();
}

c) Feet into inch [1 feet = 12 inch]


#include<stdio.h>
#include<conio.h>
void main()
{
int feet, inch;
clrscr();
printf("\n Input Measure (in feet):-
"); scanf("%d", &feet);

| Structured Programming 2
inch = 12*feet;
printf("\n %d Feet = %d Inch", feet, inch);

| Structured Programming 3
getch();
}

d) Fahrenheit temperature into centigrade temperature. [c = 5/9 *(f-32)]


#include<stdio.h>
#include<conio.h>
void main()
{
float faren,
centi; clrscr();
printf("\n Input Temperature (in Fahrenheit):-
"); scanf("%f", &faren);
centi = (5*(faren-32))/9;
printf("\n %.2f Fahrenheit = %.2f centigrade", faren, centi);
getch();
}

e) Meter into centimeter. [1 meter = 100 centimeter]


#include<stdio.h>
#include<conio.h>
void main()
{
int meter
long int cm;
clrscr();
printf("\n Input Measure (in meter):- ");
scanf("%d", &meter);
cm = 100*meter;
printf("\n %d meter = %ld centimeter", meter, cm);
getch();
}

6. Write a program to find square, cube and square root of given number.
#include<stdio.h>
#include<conio.h>
#include<math.h>

void main()
{
int num, org, square, root, cube;
clrscr();
printf("\n Input any number:- ");
scanf("%d", &num);
org = num;
square = pow(num,2);
root = sqrt(num);
cube = pow(num,3);
printf("\n Square of %d is %d", num, square);
printf("\n Square Root of %d is %d", num, root);
printf("\n Cube of %d is %d", num, cube);
getch();
}

| Structured Programming 4
7. Write a program to read radius of circle then find circumference, area and
perimeter of the circle.

| Structured Programming 5
#include<stdio.h>
#include<conio.h>
#define PI 3.14
void main()
{
float radius, area,
circum; clrscr();
printf("\n Input Radius of circle:-
"); scanf("%f", &radius);
area = PI*radius*radius; circum
= 2*PI*radius; printf("\n Area
= %.2f", area);
printf("\n Circumference = %.2f",circum);
getch();
}

8. Write a program to input principle amount, rate of interest and time from the user and find
simple interest and mixed amount.
#include<stdio.h>
#include<conio.h>

void main()
{
int p;
float t, r, si,
ma; clrscr();
printf("\n Input Principal Amount (Rs):-
"); scanf("%d", &p);
printf("\n Input Rate of interest (%):- ");
scanf("%f", &r);
printf("\n Input Time (Years):-
"); scanf("%f", &t);
si= (p*t*r)/100;
ma=si+p;
printf("\n Simple Interest = Rs %.2f", si);
printf("\n Mixed Amount = Rs %.2f",ma);
getch();
}

9. Write a program to read two numbers and swap them and display them.
#include<stdio.h>
#include<conio.h>

void main()
{
int a, b, temp;
clrscr();
printf("\n Input First Number:-
"); scanf("%d", &a);
printf("\n Input Second Number:- ");
scanf("%d", &b);
printf("\n Numbers before swapping\n");
printf("\n First Number = %d and Second Number = %d",

| Structured Programming 6
a, b);
temp = a;

| Structured Programming 7
a = b;
b = temp;
printf("\n Numbers after swapping\n");
printf("\n First Number = %d and Second Number = %d", a,
b); getch();
}

10. Write a program to read a four digit number and find the sum of first and last digit of the
number. [Example: 1234 = 1 + 4 = 5]
#include<stdio.h>
#include<conio.h>
void main()
{
int num, first, last, sum;
clrscr();
printf("\n Input any four digit number:-
"); scanf("%d",&num);
first=num/1000;
last=num%10;
sum = first + last;
printf("\n Sum of %d and %d is %d", first, last,
sum); getch();
}

11. Write a program to read a four digit number and display the sum of those individual digits.
[Example: 1234 = 1 + 2 + 3+ 4 = 10]
#include<stdio.h>
#include<conio.h>
void main()
{
int num, first, second, third, fourth, sum;
clrscr();
printf("\n Input any four digit number:-
"); scanf("%d", &num);
fourth = num%10;
num = num/10;
third = num%10;
num = num/10;
second = num%10;
num = num/10;
first = num%10;
sum = first + second + third + fourth;
printf("\n Sum of %d + %d + %d + %d is %d", first, second, third,
fourth, sum);
getch();
}

12. Write a program to read a four digit number and display its reverse. [E.g.: 1234 = 4321]
#include<stdio.h>
#include<conio.h>
void main()
{
int num, first, second, third, fourth, org,

| Structured Programming 8
reverse; clrscr();
printf("\n Input any four digit number:- ");

| Structured Programming 9
scanf("%d", &num);
org = num;
fourth = num%10;
num = num/10;
third = num%10;
num = num/10;
second = num%10;
num = num/10;
first = num%10;
reverse = fourth*1000 + third*100 + second*10 + first*1;
printf("\n Reverse of %d is %d", org, reverse); getch();
}

13. Write a program to read a five digit number and display the sum of square of individual
2 2 2 2 2
digits. [Example: 1234 5= 1 + 2 + 3 + 4 +5 = 55]
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
long int num, org;
int first, second, third, fourth, fifth,
sum; clrscr();
printf("\n Input any five digit number:-
"); scanf("%ld", &num);
org = num; fifth
= num%10; num =
num/10; fourth =
num%10; num =
num/10; third =
num%10; num =
num/10; second =
num%10; num =
num/10;
first = num%10;
sum = pow(fifth,2) + pow(fourth,2) + pow(third,2) + pow(second,2)
+ pow(first,2);
printf("\n Reverse of %ld is %d", org,
sum); getch();
}

14. Write a program to read a five digit number then print a new number by adding one to each
of its digits. [Example: 1234 = 2345]
#include<stdio.h>
#include<conio.h>

void main()
{
int num, org, first, second, third, fourth,
new_num; clrscr();
printf("\n Input any four digit number:-
"); scanf("%d", &num);

| Structured Programming 10
org = num;
fourth = num%10+1;

| Structured Programming 11
num = num/10;
third = num%10+1;
num = num/10+1;
second = num%10;
num = num/10+1;
first = num%10;
new_num = first*1000 + second*100 + third*10 + fourth*1;
printf("\n Required number is %d", new_num);
getch();
}

Decision Control Structure Programs:


1. Write a program to input any two numbers and display the largest number between them.
#include<stdio.h>
#include<conio.h>
void main()
{
int first, second;
clrscr();
printf("\n Input First Number:-
"); scanf("%d", &first);
printf("\n Input Second Number:- ");
scanf("%d", &second);
if(first>second)
printf("\n %d is large number",
first); else
printf("\n %d is large number",
second); getch();
}

2. Write a program to input any three numbers and display the largest number among them.
#include<stdio.h>
#include<conio.h>
void main()
{
int first, second,
third; clrscr();
printf("\n Input First Number:-
"); scanf("%d", &first);
printf("\n Input Second Number:- ");
scanf("%d", &second);
printf("\n Input Third Number:-
"); scanf("%d", &third);
if(first>second)
{
if(first>third)
printf("\n %d is largest number", first);
else
printf("\n %d is largest number", third);
}
else
{

| Structured Programming 12
if(second>third)
printf("\n %d is largest number", second);

| Structured Programming 13
else
printf("\n %d is largest number", third);
}
getch();
}

Alternate method
#include<stdio.h>
#include<conio.h>
void main()
{
int first, second,
third; clrscr();
printf("\n Input First Number:-
"); scanf("%d", &first);
printf("\n Input Second Number:- ");
scanf("%d", &second);
printf("\n Input Third Number:-
"); scanf("%d", &third);
if(first>second && first>third)
printf("\n %d is largest number", first);
else if(second>first && second>third)
printf("\n %d is largest number",
second); else
printf("\n %d is largest number", third);
getch();
}

3. Write a program to read a number and find whether it is odd or even number.
#include<stdio.h>
#include<conio.h>
void main()
{
int num;
clrscr();
printf("\n Input any Number:- ");
scanf("%d", &num);
if(num%2==0)
printf("\n %d is even number", num);
else
printf("\n %d is odd number",
num); getch();
}

4. Write a program to input a number and check the number is positive or negative.
#include<stdio.h>
#include<conio.h>
void main()
{
int num;
clrscr();
printf("\n Input any Number:- ");
scanf("%d", &num);

| Structured Programming 14
if(num<0)
printf("\n %d is negative number", num);

| Structured Programming 15
else
printf("\n %d is positive number",
num); getch();
}

5. Write a program to find roots of a quadratic equation.


#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
float a,b,c, root1, root2;
clrscr();
printf("\n Input value of a, b, and c :-
"); scanf("%f%f%f", &a, &b, &c);
if((b*b)>(4*a*c))
{
root1 = -b + sqrt((b*b)-(4*a*c)/(2*a));
root2 = -b – sqrt((b*b)-(4*a*c)/(2*a));
printf("\n Roots are:- \n");
printf("First Root = %.2f and Second Root = %8.2f", root1,
root2);
}
else
printf("\n The roots are imaginary");

getch();
}

6. Write a program to read a binary number and display its equivalent decimal number.
/*binary number to decimal
number*/ #include<stdio.h>
#include<conio.h>

void main()
{
long int num, bin_num;
int dec = 0, base = 1,
rem; clrscr();
printf("\n Input a binary number :-
"); scanf("%ld", &num);
bin_num = num;
while(num>0)
{
rem = num%10;
dec = dec + rem * base;
num = num /10;
base = base *2;
}
printf("\n Binary number = %ld and Decimal number = %d", bin_num,
dec);
getch();
}

| Structured Programming 16
7. Write a program to read a decimal number and display its equivalent binary number.
/*decimal number to binary
number*/ #include<stdio.h>
#include<conio.h>

void main()
{
long int num,rem, dec_num, base = 1, binary = 0;
clrscr();
printf("\n Input a decimal number :-
"); scanf("%ld", &num);
dec_num = num;
while(num>0)
{
rem = num%2;
binary = binary + rem *
base; num = num/2;
base = base * 10;
}
printf("\n Decimal number = %ld and Binary number = %ld",
dec_num, binary);
getch();
}

8. Write a program to read a octal number and display its equivalent decimal number.
/*Octal number to decimal
number*/ #include<stdio.h>
#include<conio.h>

void main()
{
long int num, oct_num;
int dec = 0, base = 1,
rem; clrscr();
printf("\n Input a octal number :- ");
scanf("%ld", &num);
oct_num = num;
while(num>0)
{
rem = num%10;
dec = dec + rem * base;
num = num /10;
base = base * 8;
}
printf("\n Octal number = %ld and Decimal number = %d", oct_num,
dec);
getch();
}

| Structured Programming 17
9. Write a program to calculate the salary as per following table
Gender Year of Service Qualification Salary
>=10 Post Graduate 15000
>=10 Graduate 10000
Male
<10 Post Graduate 10000
<10 Graduate 7000
>=10 Post Graduate 12000
>=10 Graduate 9000
Female
<10 Post Graduate 10000
<10 Graduate 6000

#include<stdio.h>
#include<conio.h> void
main()
{
int year_service, quali; char
sex;
clrscr();
printf("\n Input Sex (\"M\" for Male or \"F\" for Female):- ");
scanf("%c", &sex);
printf("\n Input year of service:- ");
scanf("%d", &year_service);
printf("\n Input qualification (0 for Post Graduate or 1 for
Graduate):- ");
scanf("%d", &quali);
if(sex=='m' || sex == 'M')
{
if(year_service>=10)
{
if(quali== 0)
printf("\n Salary = Rs. 15000");
else
printf("\n Salary = Rs. 10000");
}
else
{
if(quali== 0)
printf("\n Salary = Rs. 10000");
else
printf("\n Salary = Rs. 7000");
}
}
else
{
{
if(year_service>=10)
{
if(quali== 0)
printf("\n Salary = Rs. 12000");
else
printf("\n Salary = Rs. 9000");
| Structured Programming 18
}
else
{
if(quali== 0)
printf("\n Salary = Rs. 10000");
else
printf("\n Salary = Rs. 6000");
}
}
}

getch();
}

Alternate method
#include<stdio.h>
#include<conio.h>
void main()
{
int year_service, quali,
salary; char sex;
clrscr();
printf("\n Input Sex (\"M\" for Male or \"F\" for Female):- ");
scanf("%c", &sex);
printf("\n Input year of service:- ");
scanf("%d", &year_service);
printf("\n Input qulaification (0 for Post Graduate or 1 for
Graduate):- ");
scanf("%d", &quali);
if(sex=='m' && year_service>=10 &&
quali==0) salary = 15000;
else if (sex=='m' && year_service>=10 &&
quali==1) salary = 10000;
else if ((sex=='m' && year_service<10 && quali==0)||(sex=='f' &&
year_service<10 && quali==0))
salary = 10000;
else if (sex=='m' && year_service<10 &&
quali==1) salary = 7000;
else if (sex=='f' && year_service>=10 &&
quali==0) salary = 12000;
else if (sex=='f' && year_service>=10 &&
quali==1) salary = 9000;
else
salary = 6000;
printf("\n Salary = Rs. %d",
salary); getch();
}

10. Write a program to read cost price and selling price of product and determine whether
there is loss or profit and also determine how much profit made or loss incurred.
#include<stdio.h>
#include<conio.h>
void main()

| Structured Programming 19
{
int cp, sp, profit,
loss; clrscr();
printf("\n Input Cost Price:- ");
scanf("%d", &cp);
printf("\n Input Selling Price:- ");
scanf("%d", &sp);
if(sp==cp)

| Structured Programming 20
printf("\n No profit No loss");
else
{
if(sp>cp)
{
profit = sp - cp;
printf("\n Profit = Rs %d", profit);
}
else
{
loss = cp - sp;
printf("\n Loss = Rs %d", loss);
}
}
getch();
}

11. Write a program to input any year through keyboard and determine whether it is leap year
or not.
#include<stdio.h>
#include<conio.h>
void main()
{
int year;
clrscr();
printf("\n Input any year:- ");
scanf("%d", &year);
if(year%4000 == 0 )
printf("\n %d is a leap year",
year); else if (year %100 == 0)
printf("\n %d is a leap year",
year); else if(year%4 ==0)
printf("\n %d is a leap year",
year); else
printf("\n %d is not a leap year", year);
getch();
}

12. Write a program to check whether a triangle is valid or not, when the three angles of the
triangle are entered through the keyboard. A triangle is valid if the sum of all the three
angles is equal to 180 degree.
#include<stdio.h>
#include<conio.h>
void main()
{
int a, b, c,
sum; clrscr();
printf("\n Input three sides of a triangle:- ");
scanf("%d%d%d", &a, &b, &c);
sum = a+b+c;
if(sum == 180)
printf("\n It is valid
triangle"); else

| Structured Programming 21
printf("\n It is non-valid triangle");
getch();

| Structured Programming 22
}

13. Given the length and breadth of a rectangle, write a program to find whether the area of the
rectangle is greater than its perimeter. For example, the area of the rectangle with length =
5 and breadth = 4 is greater than its perimeter.
#include<stdio.h>
#include<conio.h>
void main()
{
int length, breadth, area,
perimeter; clrscr();
printf("\n Input length of a rectangle:-
"); scanf("%d", &length);
printf("\n Input breadth of a rectangle:-
"); scanf("%d", &breadth);
area = length*breadth;
perimeter = 2*(length + breadth);
printf("\n Area = %d", area);
printf("\n Perimeter = %d",
perimeter); if(area>perimeter)
printf("\n Area of the rectangle is greater than its
perimeter");
else
printf("\n Area of the rectangle is less than its
perimeter"); getch();
}

14. Any character is entered through the keyboard; Write a program to determine whether the
character entered is a capital letter, a small letter, a digit or a special symbol.
#include<stdio.h>
#include<conio.h>
void main()
{
char ch;
clrscr();
printf("\n Input any character:-
"); scanf("%c", &ch);
if(ch>='A' && ch<='Z')
printf("\n %c whose value is %d is a capital letter", ch,
ch); else if(ch>='a' && ch <='z')
printf("\n %c whose value is %d is a small letter", ch,
ch); else if(ch>='0' && ch<='9')
printf("\n %c whose value is %d is a number", ch,
ch); else
printf("\n %c whose value is %d is a special letter", ch,
ch); getch();
}

| Structured Programming 23
Alternate: with ASCII value

15. A library charges a fine for every book returned late. For first 5 days the fine is 50 paisa,
for 6 to 10 days fine is one rupee and above 10 days fine is 5 rupees. If you return
the book after 30 days your membership will be cancelled. Write a program to
accept number of days the member is late to return the book and display the
fine or the appropriate message.
#include<stdio.h>
#include<conio.h>
void main()
{
int days;
float fine;
clrscr();
printf("\n Input days of return:-
"); scanf("%d", &days);
if(days<=5)
{
fine = days*0.05;
printf("\n Your fine is = Rs. %.2f", fine);
}
else if(days>=6 && days <=10)
{
fine = days *1;
printf("\n Your fine is = Rs. %.2f", fine);
}
else if(days>=11 && days<=30)
{
fine = days *5;
printf("\n Your fine is = Rs. %.2f", fine);
}
else
printf("\n Your membership has been cancelled");
getch();
}

16. If the three sides of a triangle are entered through the keyboard, write a program to check
whether the triangle is isosceles, equilateral, scalene or right angled triangle.
17. The policy followed by a company to process customer orders is given by the following
rules:
a) If a customer order quantity is less than or equal to that in stock and his credit is OK,
supply his requirement.
b) If the credit is not OK do not supply. Send him intimation.
c) If the credit is OK but the item in stock is less than his order, supply what is in stock.
Intimate to him the date on which the balance will be shipped.
Write a program to implement the company policy.
#include<stdio.h>
#include<conio.h>
void main()
{

| Structured Programming 24
char health, sex, area;
int age;
printf("Enter health condn(e/p),sex(m/f),area(c/v)&age\n");
scanf("%c %c %c %d", &health, &sex, &area, &age);
if(health=='e'&& sex=='m'&& area=='c'&& age>=25&&age<=35)
{
printf("\n Insured\n");
printf("\n Premium rate = Rs. 4 per 1,000\n");
printf("\n maximum policy amount = Rs. 2,00,000");
}
else if(health=='e'&& sex=='f'&&
area=='c'&& age>=25&&age<=35)
{

| Structured Programming 25
printf("\n Insured");
printf("\n Premium Rate = Rs. 3 per 1000");
printf("\n Maximum policy amount = Rs. 1,00,000");
}
else if(health=='p'&& sex=='m'&& area=='v'&& age>=25&&age<=35)
{
printf("\n Insured");
printf("\n Premium Rate = Rs. 6 per 1,000");
printf("\n Maximum policy amount = Rs. 10,000");
}
else printf("\n You cannot be insured\n"); }

18. Write a program that check whether the number entered by user is exactly divisible by 5
but not by 11.
#include<stdio.h>
#include<conio.h>
void main()
{
int num;
clrscr();
printf("\n Input any number:- ");
scanf("%d", &num);
if(num%5 == 0 && num %11!=0)
printf("\n %d is a required number",
num); else
printf("\n %d is not required number",
num); getch();
}

19. Write a program to display the name of day on the basis of entered number from 1 to 7. For
example, Sunday for 1.
#include<stdio.h>
#include<conio.h>
void main()
{
int day;
clrscr();
printf("\n Input any number:- ");
scanf("%d", &day);
switch(day)
{
case 1:
printf("\n
Sunday"); break;
case 2:
printf("\n
Monday"); break;
case 3:
printf("\n Tuesday");
break;
case 4:
printf("\n Wednesday");

| Structured Programming 26
break;
case 5:

| Structured Programming 27
printf("\n
Thursday"); break;
case 6:
printf("\n
Friday"); break;
case 7:
printf("\n
Saturday"); break;
default:
printf("\n Invalid Choice");
break;
}
getch();
}

20. A company pays its employees on hourly basis. If an employee works for 8 hours he gets Rs
120 per hour, and Rs 150 per hour for additional hours. Write a program to read working
hours of an employee and calculate the total salary.
#include<stdio.h>
#include<conio.h>
void main()
{
int hours, salary;
clrscr();
printf("\n Input Number of Hours to work:- ");
scanf("%d", &hours);
if(hours<= 8)
{
salary = hours*120;
printf("\n Salary = Rs. %d only", salary);
}
else
{
salary = (8*120) + (hours-8)*150;
printf("\n Salary = Rs. %d only", salary);
}
getch();
}

21. Write a menu driven program using switch statement having following options.
1. Addition
2. Subtraction
3. Multiplication
4. Division
5. Exit
/*Illustration of switch case*/
#include<stdio.h>
#include<conio.h>
void main()
{
int a, b, choice, sum, sub, rem, quot,
mult; clrscr();
printf("\n Menu for Arithmetic

| Structured Programming 28
operations:\n"); printf("\n1. Addition");
printf("\n2. Subtraction");

| Structured Programming 29
printf("\n3. Multiplication");
printf("\n4. Division");
printf("\n5. Exit");
printf("\n Input your choice:-
"); scanf("%d", &choice);

switch(choice)
{
case 1:
printf("\n Input First Number:-
"); scanf("%d", &a);
printf("\n Input Second Number:- ");
scanf("%d", &b);
sum=a+b;
printf("\n Sum = %d",
sum); break;

case 2:
printf("\n Input First Number:-
"); scanf("%d", &a);
printf("\n Input Second Number:- ");
scanf("%d", &b);
sub=a-b;
printf("\n Subtraction = %d",
sub); break;

case 3:
printf("\n Input First Number:-
"); scanf("%d", &a);
printf("\n Input Second Number:- ");
scanf("%d", &b);
mult=a*b;
printf("\n Multiplication = %d",
mult); break;

case 4:
printf("\n Input First Number:-
"); scanf("%d",&a);
printf("\n Input Second Number:- ");
scanf("%d", &b);
quot=a/b;
rem=a%b;
printf("\n Quotient = %d", quot);
printf("\n Remainder = %d", rem);
break;

case 5:
exit();
break;
default:
printf("\n Invalid choice");
}
getch();

| Structured Programming 30
}

| Structured Programming 31
22. Write a program to read age of a person and find out in which category the person belongs
to:
Category Age
Child 0 to 12

Teenager 13 to 19

Adult 20 to 30
Mature 31 to 50
Old age above 50
#include<stdio.h>
#include<conio.h>
void main()
{
int age;
clrscr();
printf("\n Input your age:- ");
scanf("%d", &age);
if(age<=12)
printf("\n You are Child");
else if(age>=13 && age<=19)
printf("\n You are Teen Aged");
else if(age>=20 && age<=30)
printf("\n You are Young");
else if(age>=31 && age<=50)
printf("\n You are
Adult"); else
printf("\n You are Old");

getch();
}

23. Write a program to input sales amount for a sales person then find commission according to
following conditions.
Sales amount Commission
Below 10000 No commission
Greater than or equals to 10000 but less than
25%
25000
Above 25000 33%
#include<stdio.h>
#include<conio.h>
void main()
{
int sales_amt;
float commission;
clrscr();
printf("\n Input Sales Amount:-
"); scanf("%d", &sales_amt);

| Structured Programming 32
if(sales_amt<=10000)
printf("\n No Commission");

| Structured Programming 33
else if(sales_amt>10000 && sales_amt<=25000)
{
commission = sales_amt*0.25;
printf("\n Commission = Rs. %.2f", commission);
}
else
{
commission = sales_amt*0.33;
printf("\n Commission = Rs. %.2f", commission);
}
getch();
}

24. Write a program to calculate telephone bill system on following basic.


Consumed call Charge unit
Less than 200 calls Minimum Rs 180

More than 200 calls Rs 2 extra charge for per extra call
TSC 5% on payment
VAT 13%

#include<stdio.h>
#include<conio.h>
void main()
{
int curr_call, prev_call, tot_call, excess_call;
float vat, tsc, sub_tot, tot, grand_tot;
clrscr();
printf("\n Input Previous Call:- ");
scanf("%d", &prev_call);
printf("\n Input Current Call:-
"); scanf("%d", &curr_call);
tot_call = curr_call-prev_call;
excess_call = tot_call-200;
if(excess_call<=0)
{
tot = 180;
tsc = tot*0.05;
sub_tot =tot + tsc;
vat = sub_tot*0.13;
grand_tot = sub_tot + vat;
printf("\n Total Call = %d", tot_call);
printf("\n TSC = Rs. %.2f", tsc);
printf("\n VAT = Rs. %.2f", vat);
printf("\n Grand Total = Rs. %.2f", grand_tot);
}
else
{
tot = 180 + excess_call*2;
tsc = tot*0.05;

| Structured Programming 34
sub_tot = tot + tsc;
vat = sub_tot*0.13;
grand_tot = sub_tot + vat;

| Structured Programming 35
printf("\n Total Call = %d", tot_call);
printf("\n TSC = Rs. %.2f", tsc);
printf("\n VAT = Rs. %.2f", vat);
printf("\n Grand Total = Rs. %.2f", grand_tot);
}

getch();
}

25. Write a program to calculate the electricity authority billing system of following basic.
Unit of electricity Charge unit
Unit <= 20 Minimum Rs 80

20 < unit <=40 Rs 6 per unit for more than 20 unit

40 < unit <=80 Rs 6.5 per unit for more than 40 unit
80 < unit <=100 Rs 7 per unit for more than 80 unit

Otherwise Rs 8 per unit for more than 100 unit

#include<stdio.h>
#include<conio.h>
void main()
{
int curr_unit, prev_unit, tot_unit, excess_unit;
float vat, sub_tot, tot, grand_tot;
clrscr();
printf("\n Input Previous Unit:- ");
scanf("%d", &prev_unit);
printf("\n Input Current Unit:-
"); scanf("%d", &curr_unit);
tot_unit = curr_unit-prev_unit;

if(tot_unit<=20)
{
sub_tot=80;
vat=sub_tot*0.13;
grand_tot=sub_tot+vat;
printf("\n Total Unit = %d", tot_unit);
printf("\n VAT = Rs. %.2f", vat);
printf("\n Grand Total = Rs. %.2f", grand_tot);
}
else if (tot_unit>20 && tot_unit<=40)
{
sub_tot=80+((tot_unit-
20)*6); vat=sub_tot*0.13;
grand_tot=sub_tot+vat;
printf("\n Total Call = %d", tot_unit);
printf("\n VAT = Rs. %.2f", vat);
printf("\n Grand Total = Rs. %.2f", grand_tot);
}
else if (tot_unit>40 && tot_unit<=80)
| Structured Programming 36
{
sub_tot=80+(20*6)+((tot_unit-40)*6.5);

| Structured Programming 37
vat=sub_tot*0.13;
grand_tot=sub_tot+vat;
printf("\n Total Call = %d", tot_unit);
printf("\n VAT = Rs. %.2f", vat);
printf("\n Grand Total = Rs. %.2f", grand_tot);
}
else if (tot_unit>80 && tot_unit<=100)
{
sub_tot=80+(20*6)+(40*6.5)+((tot_unit-
80)*7); vat=sub_tot*0.13;
grand_tot=sub_tot+vat;
printf("\n Total Call = %d", tot_unit);
printf("\n VAT = Rs. %.2f", vat);
printf("\n Grand Total = Rs. %.2f", grand_tot);
}
else
{
sub_tot=80+(20*6)+(40*6.5)+ (20*7)+((tot_unit-
100)*8); vat=sub_tot*0.13;
grand_tot=sub_tot+vat;
printf("\n Total Call = %d", tot_unit);
printf("\n VAT = Rs. %.2f", vat);
printf("\n Grand Total = Rs. %.2f", grand_tot);
}

getch();
}

26. Write a program to calculate SLC Mark sheet on following basic:


Percentage Division

>= 80 Distinction
>= 60 First Division

>= 60 First Division


>= 60 First Division
Otherwise Fail
/*SLC Marksheet*/
#include<stdio.h>
#include<conio.h>
void main()
{
int m1,m2,m3,m4,m5,m6,m7,m8,total;
float per;
clrscr();
printf("\n Input mars of 8 subjects:- ");
scanf("%d%d%d%d%d%d%d%d",&m1,&m2,&m3,&m4,&m5,&m6,&m7,&m8);
total=m1+m2+m3+m4+m5+m6+m7+m8;
printf("Total Marks = %d", total);
if(m1>=32 && m2>=32 && m3>=32 && m4>=32 && m5>=32 &&
m6>=32 && m7>=32 && m8>=32)
| Structured Programming 38
{
per=(float)total/8;

| Structured Programming 39
printf("\percentage =
%8.2f",per); if(per>=80)
printf("\n Result :
Distinction"); else if(per>=60)
printf("\n Result : First
Division"); else if(per>=45)
printf("\n Result : Second Division");
else
printf("\n Result : Third Division");
}
else
printf("\n
Failed"); getch();
}

The Loop Control Structure Programs:


1. Write a program to display series and sum of the series of followings:
a) 1, 2, 3, ………, 100
/*Natural Number*/
#include<stdio.h>
#include<conio.h>
void main()
{
int i, sum = 0;
clrscr();
for(i=1;i<=100;i++)
{
printf("%5d",i);
sum = sum + i;
}
printf("\n Sum = %d",
sum); getch();
}

b) 2, 4, 6, …………..100
/*Sum of first 100 even numbers*/
#include<stdio.h>
#include<conio.h>
void main()
{
int i, sum = 0;
clrscr();
for(i=1;i<=100;i++)
{
if(i%2 == 0)
{
printf("%5d",i);
sum = sum + i;
}
}
printf("\n Sum = %d",
sum); getch();
}

| Structured Programming 40
th
c) 1, 8, 27, 64, up to n term.

| Structured Programming 41
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int i, n;
long int num, sum =
0; clrscr();
printf("\n How many terms:- ");
scanf("%d", &n);
for(i=1;i<=n;i++)
{
num = pow(i,3);
printf("%5d",num);
sum = sum + num;
}
printf("\n Sum = %ld", sum);
getch();
}

d) 1, 11, 111, 1111, …… up to 10 terms


#include<stdio.h>
#include<conio.h>
void main()
{
int i,n;
long int sum = 0,term = 0;
clrscr();
printf("\n How many terms:- ");
scanf("%d", &n);
for(i=0;i<n;i++)
{
term=term*10 + 1;
sum = sum + term;
printf("%10ld", term);
}
printf("\n Sum = %ld", sum);
getch();
}
th
e) 3, 9, 27, n………. terms Up to
#include<stdio.h>
#include<conio.h>
void main()
{
int i,n;
long int sum = 0,term = 1;
clrscr();
printf("\n How many terms:- ");
scanf("%d", &n);
for(i=0;i<n;i++)
{
term=term*3;

| Structured Programming 42
sum = sum+term;
printf("%10ld", term);

| Structured Programming 43
}
printf("\n Sum = %ld", sum);
getch();
}

f) (2x3)/5+ (4x5)/7+ (6x7)/9+ ….. up to n term


#include<stdio.h>
#include<conio.h>
void main()
{
int i,n;
float sum = 0, term, nume, deno = 5,first = 2, second = 3;

clrscr();
printf("\n How many terms:- ");
scanf("%d", &n);
for(i=0;i<n;i++)
{
nume = first*second;
term = nume/deno;
sum = sum + term;
printf("%10.2f", term);
first = first + 2;
second = second +2;
deno = deno + 2;
}
printf("\n Sum = %.2f",
sum); getch();
}

g) 1x3+ 3x5 + 5x7 + ……… up to n terms


#include<stdio.h>
#include<conio.h>
void main()
{
int i,n;
long int sum = 0, term, first = 1, second = 3;

clrscr();
printf("\n How many terms:- ");
scanf("%d", &n);
for(i=0;i<n;i++)
{
term = first * second;
sum = sum + term;
printf("%6ld", term);
first = second; second
= second +2;
}
printf("\n Sum = %ld", sum);
getch();
}

| Structured Programming 44
h) 0.9 + 0.99 + 0n.terms999 + …….. up to
#include<stdio.h>

| Structured Programming 45
#include<conio.h>
void main()
{
int i,n;
float term = 0.0, sum =
0.00; clrscr();
printf("\n How many terms:- ");
scanf("%d", &n);
for(i=0;i<n;i++)
{
term = (term * 0.10) +
0.9; printf("%10f", term);
sum = sum + term;
}
printf("\n Sum = %.2f",
sum); getch();
}
1 3 5 7
i) 1 x 1 + 2 x 3 + 1 x 5 + 1 x 7 up to n terms
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int i,n,value = 1, second = 1,exp = 1;
long int term = 0, sum = 0, first;
clrscr();
printf("\n How many terms:- ");
scanf("%d", &n);
for(i=0;i<n;i++)
{
first = pow(value,exp);
term = first*second;
printf("%ldX%d\t", first,second);
value = value + 1;
second = second + 2;
exp = exp + 2;
sum = sum + term;
}
printf("\n Sum = %ld", sum);
getch();
}
2 3 5
j) x + x /2 + x /3 + x /5…………… up to n terms
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int i,n,x, deno, exp = 1, a = 1, b =
1; long int nume;
long float term = 0, sum =
0; clrscr();

| Structured Programming 46
printf("\n Input value of x:- ");
scanf("%d", &x);

| Structured Programming 47
printf("\n How many terms:- ");
scanf("%d", &n);
for(i=0;i<n;i++)
{
nume = pow(x,exp);
deno = exp;
term = nume/deno;
printf("%ld/%d\t", nume, deno);
exp = a + b;
a = b; b
= exp;
sum = sum + term;
}
printf("\n Sum = %.2lf",
sum); getch();
}

2. Write a program to calculate the simple interest for 3 different sets of p, t, and r.
#include<stdio.h>
#include<conio.h>

void main()
{
int i, p;
float t, r, interest,
a; clrscr();
for(i=0; i<3;i++)
{
printf("\n Input Principle Amount:-
"); scanf("%d", &p);
printf("\n Input Time taken:- ");
scanf("%f", &t);
printf("\n Input Rate of Interest:-
"); scanf("%f", &r);
interest = (p*t*r)/100;
a = interest + p;
printf("\n Simple interest = %.2f", interest);
printf("\n Mixed Amount = %.2f", a); clrscr();
}
getch();
}

3. Write a program to find factorial of a given number.


/*Factorial Number*/
#include<stdio.h>
#include<conio.h>
void main()
{
int i,n;
long int fact=1;
clrscr();
printf("\n Input any number:- ");

| Structured Programming 48
scanf("%d", &n);
if(n<0)

| Structured Programming 49
printf("\n Invalid Number");
else if(n==0 || n==1)
printf("\n Factorial of %d is 1",n);
else
{
for(i=1; i<=n; i++)
fact = fact*i;
printf("\n Factorial of %2d = %ld", n, fact);
}
getch();
}

4. Write a program to display multiplication table of a number which is entered through


keyboard.
/*Multiplication table*/
#include<stdio.h>
#include<conio.h>
void main()
{
int i,n;
clrscr();
printf("\n Input any number:- ");
scanf("%d", &n);
for(i=1;i<=10;i++)
printf("\n %2d x %2d = %3d", n, i,
n*i); getch();
}

5. Write a program to display all odd numbers from 1 to 500.


#include<stdio.h>
#include<conio.h>
void main()
{
int i;
clrscr();
for(i=1; i<500;i++)
{
if(i%2!=0)
printf("%5d", i);
}
getch();
}

6. Write a program to display all prime numbers from 1 to 1000.


/*Prime Number from 1 to 1000*/
#include<stdio.h>
#include<conio.h>
void main()
{
int i, j; clrscr();
for(i=1;i<1000;i++)

| Structured Programming 50
for(j=2;j<i; j++)
{

| Structured Programming 51
if(i%j==0)
break;
}
if(i == j)
printf("%5d",i);
}
getch();
}

7. Write a program to read any number and determine whether it is Armstrong number of
not.
/*Armstrong Number*/
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int rem;
long int num, org, arms=0;
clrscr();
printf("\n Input any number:- ");
scanf("%ld", &num);
org=num;
do
{
rem = num%10;
arms = arms + pow(rem,3);
num = num/10;
} while(num!=0);
if(org==arms)
printf("\n %ld is Armstrong number",
org); else
printf("\n %ld is non-Armstrong number", org);
getch();
}

8. Write a program to display Armstrong numbers up to nth term


#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int i, temp, n, rem, sum =
0; clrscr();
printf("\n Armstrong number up to:-
"); scanf("%d", &n);
for(i=1;i<=n;i++)
{
sum = 0;
temp = i;
while(temp!=0)
{
rem = temp%10;

| Structured Programming 52
sum = sum + pow(rem,3);
temp = temp/10;

| Structured Programming 53
}
if(i == sum)
printf("%5d", sum);
}
getch();
}

9. Write a program to read a number and determine whether it is a prime number or not.
/*to find prime numbers*/
#include<stdio.h>
#include<conio.h>
void main()
{
int i, num;
clrscr();
printf("\n Input any number:- ");
scanf("%d", &num);
for(i=2; i<num; i++)
{
if(num%i==0)
{
printf("%d is non-prime number", num);
break;
}
}
if(num==i)
printf("\n %d is prime number",
num); getch();
}

10. Write a program to count number of prime numbers from 1 to up to „n‟ numbers.
#include<stdio.h>
#include<conio.h>
void main()
{
int i, j, n, count=0, prime;
clrscr();
printf("\n How many terms:- ");
scanf("%d", &n);
for(i=1;i<=n;i++)
{
prime = 1;
for(j=2;j<i;j++)
{
if(i%j==0)
{
prime =
0; break;
}
}
if(prime==1)
{
printf("%5d",

| Structured Programming 54
i); count++;
}

| Structured Programming 55
}
printf("\n There are %d prime numbers between 1 to %d",count,n);
getch();
}

11. Write a program to find the sum of the square of first n natural numbers, where n is
inputted through keyboard.
#include<stdio.h>
#include<conio.h>

void main()
{
int i, n;
long int sum=0;
clrscr();
printf("\n How many terms:- ");
scanf("%d", &n);
for(i=1;i<=n; i++)
sum = sum + (i*i);
printf("\n Sum = %ld", sum);
getch();
}

12. Write a program to find the sum of numbers from 1 to 1000, which are exactly divisible by 4
but not by 6.
#include<stdio.h>
#include<conio.h>

void main()
{
int i;
long int sum=0;
clrscr();
for(i=1;i<=100;i++)
{
if(i%4==0 && i%6!=0)
sum = sum + (i*i);
}
printf("\n Sum = %ld", sum);
getch();
}

13. An employee gets salary Rs 10 per day on the first day. On second day he Rs 20, in this way
his salary is doubled everyday. Write a program to find the total salary after one month.
/*Salary Calculation of */
#include<stdio.h>
#include<conio.h>
void main()
{
int i;
long int salary = 10;
clrscr();
for(i=1;i<30;i++)

| Structured Programming 56
{
salary = salary *2;

| Structured Programming 57
}
printf("\n Total Monthly Salary = %ld", salary);
getch();
}

14. Write a program to find sum of odd and even numbers from 1 to 100 numbers.
#include<stdio.h>
#include<conio.h>
void main()
{
int i, even_sum = 0, odd_sum = 0;
clrscr();
for(i=1;i<=100;i++)
{
if(i%2==0)
even_sum +=i;
else
odd_sum +=i;
}
printf("\n Sum of odd numbers is:- %d", odd_sum);
printf("\n Sum of even numbers is:- %d", even_sum);
getch();
}

15. Write a program to read a positive number less than 20 and display the multiplication table.
/*Multiplication table*/
#include<stdio.h>
#include<conio.h>
void main()
{
int i,n;
clrscr();
printf("\n Input any number:- ");
scanf("%d", &n);
if(n<20)
{
for(i=1;i<=10;i++)
printf("\n %2d x %2d = %3d", n, i, n*i);
}
else
printf("\n Invalid Number");
getch();
}

16. Write a program to read a number and display its reverse number.
/*Reverse Number*/
#include<stdio.h>
#include<conio.h>
void main()
{
int rem;
long int num, org,
reverse=0; clrscr();

| Structured Programming 58
printf("\n Input any number:- ");
scanf("%ld", &num);

| Structured Programming 59
org =
num; do
{
rem = num%10;
reverse = reverse*10+rem;
num = num/10;
} while(num!=0);
printf("\n Reverse of %ld is %ld", org,
reverse); getch();
}

17. Write a program to input any number and determine whether it is palindrome number of
not.
/*Palindrome Number*/
#include<stdio.h>
#include<conio.h>
void main()
{
int rem;
long int num, org,
reverse=0; clrscr();
printf("\n Input any number:- ");
scanf("%ld", &num);
org=num;
do
{
rem=num%10;
reverse=reverse*10+rem;
num=num/10;
}while(num!=0);
if(org==reverse)
printf("\n %ld is palindrome number", org);
else
printf("\n %ld is non-palindrome number",
org); getch();
}

18. Write a program to display Fibonacci series.


/*Fibonacci Series*/
#include<stdio.h>
#include<conio.h>
void main()
{
int i, n, first=0,second=1,third;
clrscr();
printf("\n How many terms:- ");
scanf("%d", &n);
printf("%5d%5d",first,second);
for(i=0;i<n-2;i++)
{
third = first + second;
printf("%5d",third);
first = second;

| Structured Programming 60
second = third;
}

| Structured Programming 61
getch();
}

19. The city of Dhangadhi has a population of 100000, but this is falling by 10% each year. The
town of Mahendranagar has population of 30,000, but this is increasing by 5% each year.
Write a program to predict the number of years that will pass before Mahendranagar will
have a greater population than Dhangadhi, if this trend continues.
/*Population calculation
*/ #include<stdio.h>
#include<conio.h>
void main()
{
int year = 0;
long int dhn = 100000, mnr = 30000;

clrscr();
do
{
dhn = dhn - (dhn *
0.10); mnr = mnr + (mnr
* 0.05); year++;
}while(dhn>mnr);
printf("\n Total time = %d years", year);
getch();
}

20. Write a program to display following patterns:


a) 1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
#include<stdio.h>
#include<conio.h>
void main()
{
int i, j;
clrscr();
for(i=1;i<=5;i++)
{
for(j=1;j<=i;j++)
printf("%2d",j);
printf("\n");
}
getch();
}

b) 5 4 3 2 1
5 4 3 2
5 4 3
5 4
5
#include<stdio.h>

| Structured Programming 62
#include<conio.h>
void main()

| Structured Programming 63
{
int i, j;
clrscr();
for(i=1;i<=5;i++)
{
for(j=5;j>=i;j--)
printf("%2d",j);
printf("\n");
}
getch();
}

c) 1 2 3 4 5
1 2 3 4
1 2 3
1 2
1
#include<stdio.h>
#include<conio.h>
void main()
{
int i, j;
clrscr();
for(i=5;i>=1;i--)
{
for(j=1;j<=i;j++)
printf("%2d",j);
printf("\n");
}
getch();
}

d) C O M P U T E R
C O M P U T E
C O M P U T
C O M P U
C O M P
C O M
C O
C
#include<stdio.h>
#include<conio.h>
void main()
{
int i, j,k;
char text[] =
"COMPUTER"; clrscr();
for(i=0;i<8;i++)
{
k=0;
for(j=8;j>i;j--)
{
printf("%2c",text[k]);

| Structured Programming 64
k++;
}

| Structured Programming 65
printf("\n");
}
getch();
}

e) C
C O
C O M
C O M P U
C O M P U T
C O M P U T E
C O M P U T E R
#include<stdio.h>
#include<conio.h>
void main()
{
int i, j;
char text[]=
"COMPUTER"; clrscr();
for(i=0;i<8;i++)
{
for(j=0;j<=i;j++)
printf("%2c", text[j]);
printf("\n");
}
getch();
}

f) 1 2 3 4 5 2 3 4 5

3 4 5
4 5
5
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,k,n;
clrscr();
for(i=1;i<=5;i++)
{
for(k=1;k<=i-1;k++)
printf(" ");
for(j=i;j<=5;j++)
printf("%d ",j);
printf("\n");
}
getch();
}

g) 5
4 5
3 4 5

| Structured Programming 66
2 3 4 5
1 2 3 4 5

| Structured Programming 67
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,k,n;
clrscr();
for(i=5;i>=1;i--)
{
for(k=1;k<=i-1;k++)
printf(" ");
for(j=i;j<=5;j++)
printf("%2d",j);
printf("\n");
}
getch();
}

h) 5
4 5
3 4 5
2 3 4 5
1 2 3 4 5
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,k,n;
clrscr();
for(i=5;i>=1;i--)
{
for(k=1;k<=i-1;k++)
printf(" ");
for(j=i;j<=5;j++)
printf("%d",j);
printf("\n");
}
getch();
}
i) H HH
EEE
LLLL

OOOOO
WWWWWW

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
int i, j, length;
char text[50];

| Structured Programming 68
clrscr();
printf("\n Input any text:- ");

| Structured Programming 69
gets(text);
length = strlen(text);
for(i=0;i<length;i++)
{
for(j=0;j<=i;j++)
printf("%c", text[i]);
printf("\n");
}
printf("\n");
for(i=0;i<length;i++)
{
for(j=0;j<=i;j++)
printf("%c", text[j]);
printf("\n");
}
printf("\n");
for(i=length-1;i>=0;i--)
{
for(j=0;j<=i;j++)
printf("%c", text[j]);
printf("\n");
}
getch();
}

ARRAY PROGRAMS
1) Write a program to read 20 numbers and display them.
#include<stdio.h>
#include<conio.h>
void main()
{
int num[20],
i; clrscr();
printf("\n Input 20
data"); for(i=0;i<20;i++)
{
printf("\n num[%d]:- ",
i); scanf("%d", &num[i]);
}
for(i=0;i<20;i++)
{
printf("%5d", num[i]);
}

getch();
}

2) Write a program to read „n‟ numbers and displa


#include<stdio.h>
#include<conio.h>
void main()
{
int num[100], i, j, n, temp;

| Structured Programming 70
clrscr();
printf("\n How many data:- ");

| Structured Programming 71
scanf("%d", &n);
printf("\n Input %d data’s:-", n);
for(i=0;i<n;i++)
{
printf("\n num[%d]:- ",
i); scanf("%d", &num[i]);
}
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(num[i]>num[j])
{
temp = num[i];
num[i] = num[j];
num[j] = temp;
}
}
}
printf("\n The data in ascending orders
are:\n"); for(i=0;i<n;i++)
{
printf("%5d", num[i]);
}

getch();
}

3) Write a program to read „n‟ andnumberslargest amongand displ


the numbers.
#include<stdio.h>
#include<conio.h>
void main()
{
int i, num[100],n, greatest, smallest;
clrscr();
printf("\n How many terms:- ");
scanf("%d", &n);
printf("\n Input %d numbers", n);
for(i=0;i<n;i++)
{
printf("\n Number [%d]:- ",i+1);
scanf("%d", &num[i]);
}
greatest = num[0];
smallest = num[0];
for(i=0;i<n;i++)
{
if(num[i]>greatest)
greatest = num[i];
if(num[i]<smallest)
smallest = num[i];
}

| Structured Programming 72
printf("\nThe Numbers are: \n");
for(i=0;i<n;i++)

| Structured Programming 73
printf("%5d",num[i]);
printf("\n The greatest number is %d", greatest);
printf("\n The smallest number is %d", smallest);
getch();
}

4) Write a program to read percentage of „n‟ students


boys who scored above 80%.
#include<stdio.h>
#include<conio.h>
void main()
{
int num[100], i, n, count = 0;
clrscr();
printf("\n How many marks:- ");
scanf("%d", &n);
printf("\n Input %d percentage", n);
for(i=0;i<n;i++)
{
printf("\n Percentage[%d]:- ", i);
scanf("%d", &num[i]);
}
for(i=0;i<n;i++)
{
if(num[i]>=80)
count++;
}
printf("There are %d boys who scored above 80%", count);
getch();
}

5) Write a program to store Kathmandu Valley‟s 7 days minimum a


and calculate average, maximum and minimum temperature.
/*WAP to store kathmandu valley's 7 days maximum and minimum
temperature and calculate
average, maximum among minimum temperature and minimum among
maximum temperature*/
#include<stdio.h>
#include<conio.h>
void main()
{
int i,max_temp[7],min_temp[7],min,max,tot_max=0,tot_min=0;
clrscr();
printf("\n Input Maximum temperature of 7 days:-
"); for(i=0;i<7;i++)
{
printf("\n Maximum Temperature [%d]:-
",i+1); scanf("%d", &max_temp[i]);
tot_max = tot_max + max_temp[i];
}

printf("\n Input Minimum temperature of 7 days:-


"); for(i=0;i<7;i++)

| Structured Programming 74
{
printf("\n Minimum temperature [%d]:- ",i+1);

| Structured Programming 75
scanf("%d", &min_temp[i]);
tot_min = tot_min + min_temp[i];
}
clrscr();
printf("\n Minimum Temperature of 7 days
are:\n"); for(i=0;i<7;i++)
printf("%5d",min_temp[i]);
max=min_temp[0];
for(i=0;i<7;i++)
{
if(min_temp[i]>max)
max = min_temp[i];
}
printf("\n Maximum among minimum temperature is %d", max);

printf("\n Maximum Temperature of 7 days


are:\n"); for(i=0;i<7;i++)
printf("%5d",max_temp[i]);

min = max_temp[0];
for(i=0;i<7;i++)
{
if(max_temp[i]<min)
min = max_temp[i];
}
printf("\n Minimum among maximum temperature is %d", min);
getch();
}

6) Write a program to read the age of 100 persons and count the number of persons in the age
group between 50 to 60 years. Use “for” and “c
#include<stdio.h>
#include<conio.h>
void main()
{
int num[100], i, n, count =
0; clrscr();
printf("\n Input age of 100 persons:- ");
for(i=0;i<100;i++)
{
printf("\n Percentage[%d]:- ",
i); scanf("%d", &num[i]);
}
for(i=0;i<100;i++)
{
if(num[i]>=50 && num[i]<=60)
count++;
else
continue;
}
printf("There are %d persons whose age is in between 50
and 60 years", count);
getch();

| Structured Programming 76
}

| Structured Programming 77
7) Write a program to read salary of 200 employees and count the numbers of employees
getting salary between 5,000 and 10,000.
#include<stdio.h>
#include<conio.h>
void main()
{
int salary[200], i, n, count = 0;
clrscr();
printf("\n Input salary of 200 employees:- ");
for(i=0;i<200;i++)
{
printf("\n Salary[%d]:- ",
i); scanf("%d", &salary[i]);
}
for(i=0;i<200;i++)
{
if(salary[i]>=5000 &&
salary[i]<=10000) count++;
}
printf("There are %d employees whose salary is in between Rs.
5000 and 10000", count);
getch();
}

8) Fifty numbers are entered from the keyboard into an array. The number to be searched is
entered through the keyboard by the user. Write a program to find if the number to be
searched is present in the array and if it is present, display the number of times it appears in
the array.
#include<stdio.h>
#include<conio.h>
void main()
{
int num[100], i, n, item, count = 0;
clrscr();
printf("\n How many terms:- ");
scanf("%d", &n);
printf("\n Input %d numbers: ",
n); for(i=0; i<n; i++)
{
printf("\n Number[%d]:-
",i); scanf("%d", &num[i]);
}
printf("\n Input item to be searched:-
"); scanf("%d", &item);
for(i=0; i<n; i++)
{
if(num[i] == item)
count++;
}

if(count==0)
printf("\n Data doesn't
exist"); else

| Structured Programming 78
printf("\n There are %d %d's", count, item);

| Structured Programming 79
getch();
}

9) Twenty five numbers are entered from the keyboard into an array. Write a program to find
out how many of them are positive, how many are negative, how many are even and how
many are odds.
#include<stdio.h>
#include<conio.h>
void main()
{
int num[25], i, n, odd = 0, even = 0, positive = 0, negative =
0; clrscr();
printf("\n Input any 25 numbers: ");
for(i=0; i<25; i++)
{
printf("\n Number[%d]:-
",i); scanf("%d", &num[i]);
if(num[i]%2==0)
even++;
else
odd++;
if(num[i]<0)
negative++;
else
positive++;
}
printf("\n There are %d even and %d odd numbers", even, odd);
printf("\n There are %d positive and %d negative numbers",
positive, negative);
getch();
}

10) Write a program that interchanges the odd and even elements of an array.
11) There are 50 students in a class and the marks obtained by each student in an exam, then
print out the highest and second highest marks obtained in the exam
#include<stdio.h>
#include<conio.h>

void main()
{
int marks[50], i, first = 0, second =
0; clrscr();
printf("\n Input 50 marks:- ");
for(i=1;i<=10;i++)
{
printf("\n Marks[%d]:- ",i);
scanf("%d", &marks[i]);
if(marks[i]>first)
first = marks[i];
}
for(i=0;i<10;i++)
{
if(marks[i]!=first)

| Structured Programming 80
if(marks[i]>second)
second = marks[i];

| Structured Programming 81
}

printf("\n First Marks :- %5d", first);


printf("\n Second Marks:- %5d",
second); getch();
}

12) Write a program to display 20 Fibonacci series using array.


/*Fibonacci series using
array*/ #include<stdio.h>
#include<conio.h>

void main()
{
int series[50], i;
series[0]=0,
series[1]=1; clrscr();

for(i=2;i<=20;i++)
series[i] = series[i-1] + series[i-
2]; for(i=0;i<20;i++)
printf("%5d",
series[i]); getch();
}

13) Write a program to copy the contents of one array into another in the reverse order.
#include<stdio.h>
#include<conio.h>
void main()
{
int num[100], i, n, reverse[100];
clrscr();
printf("\n How many terms:- ");
scanf("%d", &n);
printf("\n Input %d numbers: ",
n); for(i=0; i<n; i++)
{
printf("\n Number[%d]:-
",i); scanf("%d", &num[i]);
reverse[i] = num[i];
}
printf("\n Given Matrix is:-
\n"); for(i=0;i<n;i++)
printf("%5d", num[i]);
printf("\n\n Copied and Reverse of the given Matrix is:-
\n"); for(i=n-1;i>=0;i--)
printf("%5d",
num[i]); getch();
}

14) Write a program to perform following matrix operations.


a) To read a matrix and display the matrix in appropriate form.
#include<stdio.h>

| Structured Programming 82
#include<conio.h>
void main()

| Structured Programming 83
{
int matrix[5][5], i, j, r,
c; clrscr();
printf("\n Input order of a matrix:-
"); scanf("%d %d", &r, &c);
printf("\n Input elements of %d x %d matrix:- ", r,
c); for(i=0; i<r; i++)
{
for(j=0; j<c; j++)
{
printf("\n Element [%d][%d]:- ",i, j);
scanf("%d", &matrix[i][j]);
}
}
printf("\n The Matrix
is:\n"); for(i=0; i<r; i++)
{
printf("\n");
for(j=0; j<c; j++)
printf("%5d", matrix[i][j]);
}
getch();
}

b) To read any two matrix and display the sum of the matrixes.
/*Addition of a Matrix*/
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,A[10][10],B[10][10],sum[10][10],r1,c1,r2,c2;
clrscr();
printf("\n Input order of first matrix:-
"); scanf("%d%d",&r1,&c1);
printf("\n Input order of second matrix:-
"); scanf("%d%d",&r2,&c2);
if(r1==r2 && c1==c2)
{
printf("\n Input Elements of First %d X %d Matrix:-
",r1,c1);
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
printf("\n Elements [%d][%d]:- ",i, j);
scanf("%d", &A[i][j]);
}
}
printf("\n Input Elements of Second %d X %d Matrix:-
",r2,c2);
for(i=0;i<r2;i++)
{
for(j=0;j<c2;j++)

| Structured Programming 84
{

| Structured Programming 85
printf("\n Elements [%d][%d]:- ",i, j);
scanf("%d", &B[i][j]);
}
}
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
sum[i][j] = A[i][j] + B[i][j];
}

clrscr();
printf("\n First Matrix:
\n\n"); for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
printf("%5d",A[i][j]);
printf("\n");
}

printf("\n Second Matrix: \n\n");


for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
printf("%5d",B[i][j]);
printf("\n");
}
printf("\n Addition of the Matrix:
\n\n"); for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
printf("%5d",sum[i][j]);
printf("\n");
}
}
else
printf("\n Invalid Matrices");

getch();
}

c) To display transpose of the given matrix.


#include<stdio.h>
#include<conio.h>
void main()
{
int matrix[5][5], i, j, r,
c; clrscr();
printf("\n Input order of a matrix:-
"); scanf("%d%d", &r, &c);
printf("\n Input elements of %d x %d matrix:- ", r,
c); for(i=0; i<r; i++)
{
for(j=0; j<c; j++)

| Structured Programming 86
{
printf("\n Element [%d][%d]:- ",i, j);

| Structured Programming 87
scanf("%d", &matrix[i][j]);
}
}
printf("\n The Given Matrix is:\n");
for(i=0; i<r; i++)
{
printf("\n");
for(j=0; j<c; j++)
printf("%5d", matrix[i][j]);
}

printf("\n The Transpose Matrix


is:\n"); for(i=0; i<r; i++)
{
printf("\n");
for(j=0; j<c; j++)
printf("%5d", matrix[j][i]);
}

getch();
}

d) Count number of odd and even numbers present in the given matrix.
#include<stdio.h>
#include<conio.h>
void main()
{
int matrix[5][5], i, j, r, c, odd = 0, even = 0;
clrscr();
printf("\n Input order of a matrix:-
"); scanf("%d%d", &r, &c);
printf("\n Input elements of %d x %d matrix:- ", r,
c); for(i=0; i<r; i++)
{
for(j=0; j<c; j++)
{
printf("\n Element [%d][%d]:- ",i, j);
scanf("%d", &matrix[i][j]);
}
}
printf("\n The Matrix
is:\n"); for(i=0; i<r; i++)
{
printf("\n");
for(j=0; j<c; j++)
printf("%5d", matrix[i][j]);
}

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


{
for(j=0; j<c; j++)
{
if(matrix[i][j]%2==0)

| Structured Programming 88
even++;
else

| Structured Programming 89
odd++;
}
}
printf("\n There are %d odds and %d even numbers", odd, even);

getch();
}

e) To read any two matrix and display the sum of first row of the first matrix and first
column of second matrix.
#include<stdio.h>
#include<conio.h>
void main()
{
int A[10][10],B[10][10];
int i,j, r1,c1,r2,c2, sumr = 0, sumc = 0;
clrscr();
printf("\n Input order of first matrix:-
"); scanf("%d%d", &r1,&c1);
printf("\n Input order of second matrix:-
"); scanf("%d%d", &r2,&c2);

printf("\n Input Elements of First %d X %d Matrix:- ",r1,c1);


for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
printf("\n Elements [%d][%d]:- ",i,
j); scanf("%d", &A[i][j]);
if(i==0)
sumr = sumr + A[i][j];
}
}

printf("\n Input Elements of Second %d X %d Matrix:-


",r2,c2); for(i=0;i<r2;i++)
{
for(j=0;j<c2;j++)
{
printf("\n Elements [%d][%d]:- ",i,
j); scanf("%d", &B[i][j]);
if(j==0)
sumc = sumc + B[i][j];
}
}

clrscr();
printf("\n First Matrix:
\n\n"); for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
printf("%5d", A[i][j]);
printf("\n");

| Structured Programming 90
}

| Structured Programming 91
printf("\n Second Matrix: \n\n");
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
printf("%5d", B[i][j]);
printf("\n");
}

printf("\n Sum of 1st row of first matrix is %d", sumr);


printf("\n\n Sum of 1st column of second matrix is %d",
sumc); getch();
}

f) Read any two matrixes and check whether two matrixes are equal or not.
#include<stdio.h>
#include<conio.h>
void main()
{
int i, j, A[10][10], B[10][10], r1, c1, r2, c2, flag =
1; clrscr();
printf("\n Input order of first matrix:-
"); scanf("%d%d",&r1,&c1);
printf("\n Input order of second matrix:-
"); scanf("%d%d",&r2,&c2);

printf("\n Input Elements of First %d X %d Matrix:- ",r1,c1);


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

printf("\n Input Elements of Second %d X %d Matrix:-


",r2,c2); for(i=0;i<r2;i++)
{
for(j=0;j<c2;j++)
{
printf("\n Elements [%d][%d]:- ",i,
j); scanf("%d", &B[i][j]);
}
}

clrscr();
printf("\n First Matrix:
\n\n"); for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
printf("%5d",A[i][j]);
printf("\n");
}

| Structured Programming 92
printf("\n Second Matrix: \n\n");

| Structured Programming 93
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
printf("%5d",B[i][j]);
printf("\n");
}

for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
{
if(A[i][j]!=B[i][j])
flag = 0;
}
}
if(flag == 1)
printf("\n Both Matrix are equal");
else
printf("\n Both Matrix are not
equal"); getch();
}

g) To read any two matrixes and display the multiplication of them.


/*Multiplication of two
matrixes*/ #include<stdio.h>
#include<conio.h>
void main()
{
int A[10][10], B[10][10], C[10][10];
int i, j, k, r1, r2, c1, c2, sum;
clrscr();
printf("\n Input order of first matrix:-
"); scanf("%d%d", &r1, &c1);
printf("\n Input elements of %d x %d Matrix:- ", r1,
c1); for(i=0;i<r1;i++)
for(j=0;j<c1;j++)
{
printf("\n A[%d][%d]:- ", i,
j); scanf("%d", &A[i][j]);
}
printf("\n Input order of second matrix:-
"); scanf("%d%d", &r2, &c2);
printf("\n Input elements of %d x %d Matrix:- ", r2,
c2); for(i=0;i<r2;i++)
for(j=0;j<c2;j++)
{
printf("\n B[%d][%d]:- ", i,
j); scanf("%d", &B[i][j]);
}
k = 0;
while(k<=r1)
{
sum = 0;

| Structured Programming 94
for(i=0;i<r1;i++)
{

| Structured Programming 95
for(j=0;j<c1;j++)
sum = sum + A[i][j] *
B[j][k]; C[i][k] = sum;
}
k++;
}
/*Result of the multiplication*/
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
printf("%5d", C[i][j]);
printf("\n");
}
getch();
}

h) To read a matrix and display the sum of:


i. Diagonal elements
#include<stdio.h>
#include<conio.h>
void main()
{
int matrix[5][5], i, j, r, c, sum = 0;
clrscr();
printf("\n Input order of a matrix:-
"); scanf("%d%d", &r, &c);
printf("\n Input elements of %d x %d matrix:- ", r,
c); for(i=0; i<r; i++)
{
for(j=0; j<c; j++)
{
printf("\n Element [%d][%d]:- ",i, j);
scanf("%d", &matrix[i][j]);
}
}

printf("\n The Matrix


is:\n"); for(i=0; i<r; i++)
{
printf("\n");
for(j=0; j<c; j++)
printf("%5d", matrix[i][j]);
}

if(r==c)
{
for(i=0; i<r; i++)
{
for(j=0; j<c; j++)
{
if(i==j)
sum = sum + matrix[i][j];
}

| Structured Programming 96
}
}

| Structured Programming 97
else
printf("\n Invalid Matrix");
printf("\n The sum of diagonal elements is %d", sum);

getch();
}

ii. Upper triangular matrix


/*Sum of upper matrix*/
#include<stdio.h>
#include<conio.h>
void main()
{
int A[10][10], i, j, r, c, sum =
0; clrscr();
printf("\n Input order of a matrix:-
"); scanf("%d%d", &r, &c);
printf("\n Input elements of %d x %d Matrix:- ", r,
c); for(i=0;i<r;i++)
for(j=0;j<c;j++)
{
printf("\n A[%d][%d]:- ", i,
j); scanf("%d", &A[i][j]);
}

/*Given Matrix*/
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
printf("%5d", A[i][j]);
printf("\n");
}
/*Sum of Upper matrix*/
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
if(i<=j)
sum = sum + A[i][j];
}
}
printf("\n Sum of upper matrix is %d",
sum); getch();
}

iii. Lower triangular matrix


/*Sum of lower matrix*/
#include<stdio.h>
#include<conio.h>
void main()
{
int A[10][10], i, j, r, c, sum =
0; clrscr();

| Structured Programming 98
printf("\n Input order of a matrix:-
"); scanf("%d%d", &r, &c);

| Structured Programming 99
printf("\n Input elements of %d x %d Matrix:- ", r,
c); for(i=0;i<r;i++)
for(j=0;j<c;j++)
{
printf("\n A[%d][%d]:- ", i,
j); scanf("%d", &A[i][j]);
}

/*Given Matrix*/
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
printf("%5d", A[i][j]);
printf("\n");
}
/*Sum of Lower matrix*/
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
if(i>=j)
sum = sum + A[i][j];
}
}
printf("\n Sum of lower matrix is %d",
sum); getch();
}

iv. Sum of each row


#include<stdio.h>
#include<conio.h>
void main()
{
int matrix[5][5], row, i, j, r, c, row_sum =
0; clrscr();
printf("\n Input order of a matrix:-
"); scanf("%d%d", &r, &c);
printf("\n Input elements of %d x %d matrix:- ", r,
c); for(i=0; i<r; i++)
{
for(j=0; j<c; j++)
{
printf("\n Element [%d][%d]:- ",i, j);
scanf("%d", &matrix[i][j]);
}
}

clrscr();
printf("\n The Matrix
is:\n"); for(i=0; i<r; i++)
{
printf("\n");
for(j=0; j<c; j++)

| Structured Programming 100


printf("%5d", matrix[i][j]);
}

| Structured Programming 101


printf("\n Which row you want to add:-
"); scanf("%d", &row);
if(row>=0 && row<r)
{
for(i=0; i<r; i++)
{
for(j=0; j<c; j++)
{
if(i==row)
row_sum = row_sum + matrix[i][j];
}
}
printf("\n The sum of %d row is %d", row, row_sum);
}
else
printf("\n Invalid
Row"); getch();
}

v. Sum of each column


#include<stdio.h>
#include<conio.h>
void main()
{
int matrix[5][5], col, i, j, r, c, col_sum =
0; clrscr();
printf("\n Input order of a matrix:-
"); scanf("%d%d", &r, &c);
printf("\n Input elements of %d x %d matrix:- ", r,
c); for(i=0; i<r; i++)
{
for(j=0; j<c; j++)
{
printf("\n Element [%d][%d]:- ",i, j);
scanf("%d", &matrix[i][j]);
}
}

clrscr();
printf("\n The Matrix
is:\n"); for(i=0; i<r; i++)
{
printf("\n");
for(j=0; j<c; j++)
printf("%5d", matrix[i][j]);
}

printf("\n Which column you want to add:-


"); scanf("%d", &col);
if(col>=0 && col<c)
{
for(i=0; i<r; i++)

| Structured Programming 102


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

| Structured Programming 103


{
if(j==col)
col_sum = col_sum + matrix[i][j];
}
}
printf("\n The sum of %d column is %d", col, col_sum);
}
else
printf("\n Invalid Column");
getch();
}

vi. Sum of all the elements


/*Sum of all of elements of a matrix*/
#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,A[10][10],r, c,
sum=0; clrscr();
printf("\n Input order of a matrix:-
"); scanf("%d%d",&r,&c);
printf("\n Input Elements of First %d X %d Matrix:- ",r,
c); for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
printf("\n Elements [%d][%d]:- ",i,j);
scanf("%d", &A[i][j]);
sum=sum+A[i][j];
}
}
clrscr();
printf("\n Given Matrix:
\n\n"); for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
printf("%5d",A[i][j]);
printf("\n");
}
printf("\n Sum of All elements is %d",
sum); getch();
}

15) The following table shows sales made by salespersons.

Salesperson Sunday Monday Tuesday Thursday Friday


Prakash 40 43 42 44 78
Aakash 55 44 35 75 67
Deepak 67 65 75 34 88
Santosh 75 64 67 77 86
| Structured Programming 104
Write a program that will store the above data in a matrix and then calculate and
print.
a) Total daily sales
b) Total weekly sales for each salesperson
c) Total weekly sales
#include<stdio.h>
#include<conio.h>
void main()
{
int sales[][5]={40, 43, 42, 44, 78, 55, 44, 35, 75, 67, 67, 65,
75, 34, 88, 75, 64, 67, 77, 86};
int day_sale[5],per_sale[5],week_sale = 0, choice;
char name[4][20]={"Prakash", "Aakash","Deepak", "Santosh"};
int i, j, col_sum[4],sum, count=0;
clrscr();
printf("\t Sunday Monday Tuesday Wed Friday\n");
printf("\n-------------------------------------------------\n");
for(i=0;i<4;i++)
{
printf("%s",name[i]);
for(j=0;j<5;j++)
printf("\t %5d", sales[i][j]);
printf("\n");
}
printf("\n-------------------------------------------------\n");
printf("\n 1. Total Daily Sales :");
printf("\n 2. Total weekly sales of each person :");
printf("\n 3. Total weekly sales :");
printf("\n Input your choice:- ");
scanf("%d", &choice);
switch(choice)
{
case 1:
/*Calculation of weekly sales*/
for(i=0;i<5;i++)
{
day_sale[i]=0;
for(j=0;j<4;j++)
day_sale[i] += sales[j][i];
}
printf("\n\n\n");
printf("\t Sunday Monday Tuesday Wed Friday\n");
printf("\n------------------------------------------\n");
for(i=0;i<4;i++)
{
printf("%s",name[i]);
for(j=0;j<5;j++)
printf("\t %5d", sales[i][j]);
printf("\n");
}
printf("\n-------------------------------------
-----\n");
printf("Daily Sales");
| Structured Programming 105
for(i=0;i<5;i++)
printf("%5d ", day_sale[i]);

| Structured Programming 106


printf("\n------------------------------------------\n");
break;

case 2:
/*Calculation of daily sales of each persons*/
for(i=0;i<4;i++)
{
per_sale[i]=0;
for(j=0;j<5;j++)
per_sale[i] += sales[i][j];
}
//clrscr();
printf("\n\n\n");
printf("\t Sunday Monday Tuesday Wed Friday
Total\n");
printf("\n------------------------------------------\n");
for(i=0;i<4;i++)
{
printf("%s",name[i]);
for(j=0;j<5;j++)
printf("\t %5d", sales[i][j]);
printf("%9d",per_sale[i]);
printf("\n");
}
printf("\n------------------------------------------\n");
break;

case 3:
/*Calculation of weekly sales*/
for(i=0;i<4;i++)
{
for(j=0;j<5;j++)
week_sale += sales[i][j];
}
printf("\n\n\n");
printf("\t Sunday Monday Tuesday Wed Friday\n");
printf("\n------------------------------------------\n");
for(i=0;i<4;i++)
{
printf("%s",name[i]);
for(j=0;j<5;j++)
printf("\t %5d", sales[i][j]);
printf("\n");
}
printf("\n------------------------------------------\n");
printf("Total Weekly Sales = Rs. %d", week_sale);
printf("\n------------------------------------------\n");
break;

default:
printf("\n Invalid Choice");
break;
}

| Structured Programming 107


getch();
}

| Structured Programming 108


16) An organization has 5 stores
#include<stdio.h>
#include<conio.h>
void main()
{
int
stock[5][4]={30,35,0,45,24,35,12,0,0,34,8,19,12,0,34,18,10,10,17,
45};
int i,j,col_sum[4],sum,
count=0; clrscr();
printf("\t Item1 Item2 Item3 Item4\n"); printf("\n---
------------------------------------\n");
for(i=0;i<5;i++)
{
printf("Store %d",i+1);
for(j=0;j<4;j++)
printf("\t %2d",stock[i][j]);
printf("\n");
}
printf("\n Input Store number:-
"); scanf("%d", &i);
i=i-1;
for(j=0;j<4;j++)
{
if(stock[i][j]==0)
{
count++;
printf("\n Item %d of store %d is out of
stock\n",j+1,i+1);
}
}
if(count==0)
printf("\n None of item is out of
stock"); //Finding total item in column wise
for(j=0;j<4;j++)
{
col_sum[j]=0;
for(i=0;i<5;i++)
col_sum[i] += stock[i][j];
}
for(j=0;j<4;j++)
printf("\n Total stock of item %d is %d\n",j+1,col_sum[j]);

getch();
}

17) Write a program to read 'n' numbers and display mean, median and range of the
given numbers.
/*Mean, Median and Range*/
#include<stdio.h>
#include<conio.h>
void main()

| Structured Programming 109


{
int i, j, num[100],n, temp, range, total=0,median_term;

| Structured Programming 110


float mean,
median; clrscr();
printf("\n How many terms:- ");
scanf("%d", &n);
printf("\n Input %d numbers", n);
for(i=0;i<n;i++)
{
printf("\n Number [%d]:-
",i+1); scanf("%d", &num[i]);
total=total + num[i];
}
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(num[i]>num[j])
{
temp=num[i];
num[i]=num[j];
num[j]=temp;
}
}
}
printf("\n The Numbers in ascending orders are: \n");
for(i=0;i<n;i++)
printf("%5d",num[i]);
mean=total/n;
range=num[n-1]-
num[0]; if(n%2==0)
{
median_term=(n+1)/2; median=(float)((num[median_term-
1]+num[median_term]))/2;
}
else
{
median_term=(n+1)/2;
median=num[median_term-1];
}
printf("\nMean = %5.2f",mean);
printf("\nRange = %5d",range);
printf("\nMedian =
%5.2f",median); getch();
}

18) dfdf

STRING PROGRAMS

1) Write a program to read any 10 strings and replace the first letter of strings by "k"
#include<stdio.h>
#include<conio.h>
#include<string.h>
| Structured Programming 111
void main()
{

| Structured Programming 112


int i;
char name[10][20];
clrscr();
printf("\n Input name of 10 students:-
"); for(i=0;i<10;i++)
gets(name[i]);
for(i=0;i<10;i++)
{
if(name[i][0]!='k' || name[i][0]!='K')
name[i][0]='k';
}

printf("\n Required strings


are:\n"); for(i=0;i<10;i++)
printf("\n %s",
name[i]); getch();
}

2) Write a program to read a string and change the character increase by 1 value
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
int i=0;
char name[20];
clrscr();
printf("\n Input any string:- ");
gets(name);
strlwr(name);
while(name[i]!='\0')
{
if(name[i]>=97 && name[i]<=122)
name[i]=name[i]+1;
i++;
}
printf("\n %s",
name); getch();
}

3) Write a program to enter a string and count total number of alphabet „A‟ oc in the string.

/*Counting 'A' in the


string*/ #include<stdio.h>
#include<conio.h>

void main()
{
int count = 0, i=0;
char text[50];
clrscr();
printf("\n Enter a sentence:- ");
gets(text);

| Structured Programming 113


while(text[i]!='\0')
{

| Structured Programming 114


if(text[i]=='A' ||
text[i]=='a') count++;
i++;
}
printf("There are %d \"A\" in the sentence",
count); getch();
}

4) Write a program to print all the rotation of a word typed into it. [E.g. nast, astn,
stna, tnas]
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
int i, j, length;
char text[20],temp;
clrscr();
printf("\n Input any text:- ");
gets(text);
length = strlen(text);
for(i=0;i<length;i++)
{
temp=text[0];
for(j=0;j<length-1;j++)
text[j]=text[j+1];
text[length-1]=temp;
puts(text);
printf("\n");
}
getch();
}

5) Write a program that converts a string lik


/*String to number*/
#include<stdio.h>
#include<conio.h>
#include<string.h>

void main()
{
char string[50];
int number;
clrscr();
printf("\n Input a string :-
"); scanf("%s", string);
number = atoi(string);
if(number == 0 && string[0]!= '0')
printf("\n Invalid number");
else
printf("\n Equivalent number is %d",
number); getch();
}

| Structured Programming 115


6) Write a program to delete all vowels from a sentence.

| Structured Programming 116


7) Write a program that will read a line and delete from it all occurrences of the word
„the‟.
8) Write a program to input names of n number of students and sort them in
alphabetical order
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
int i, j, n;
char name[100][100],temp[100];
clrscr();
printf("\n How many students:-
"); scanf("%d", &n);
printf("\n Input name of %d students",
n); for(i=0;i<n;i++)
{
fflush(stdin);
printf("\n Name [%d]:-
",i+1); gets(name[i]);
}
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
if(strcmp(name[i],name[j])>0)

{
strcpy(temp,name[i]);
strcpy(name[i],name[j]);
strcpy(name[j],temp);
}
}
}
printf("\n Name in Alphabetical order are: \n");
for(i=0;i<n;i++)
printf("\n %s",
name[i]); getch();
}

9) WAP to read 10 different names and age into an array and sort them in ascending
order by age and print sorted listed.
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
int i, j, age[10],temp_age;
char name[10][30],temp[30];
clrscr();
//printf("\n How many students:- ");
//scanf("%d", &n);
printf("\n Input name and age of 5 students");
| Structured Programming 117
for(i=0;i<5;i++)
{

| Structured Programming 118


fflush(stdin);
printf("\n Name [%d]:-
",i+1); gets(name[i]);
printf("\n Age [%d]:- ",i+1);
scanf("%d", &age[i]);
}
//Sorting data on the basic of
age for(i=0;i<5;i++)
{
for(j=i+1;j<5;j++)
{
if(age[i]>age[j])
{
strcpy(temp, name[i]);
strcpy(name[i],name[j]);
strcpy(name[j],temp);

temp_age = age[i];
age[i] = age[j];
age[j] = temp_age;
}
}
}
printf("\n Data in ascending order on basic of age are: \n");
printf("\n Name\t\t Age\n");
for(i=0;i<5;i++)
printf("\n %s\t %d",
name[i],age[i]); getch();
}

10) Construct following menu driven program.


1. UPPER CASE
2. Sentence case
3. lower case
4. Title Case
5. tOGGLE cASE
6. Exit

#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<ctype.h>
#include<process.h>

void main()
{
int i, j, n,
choice; char
text[50]; clrscr();
printf("\n \t\tMAIN
MENU\n"); for(i=0;i<35;i++)
printf("-");
printf("\n");

| Structured Programming 119


printf("\n 1. UPPER CASE");
printf("\n 2. Sentence case");

| Structured Programming 120


printf("\n 3. lower case");
printf("\n 4. Title Case");
printf("\n 5. tOGGLE cASE");
printf("\n 6. Exit\n");
for(i=0;i<35;i++)
printf("-");
printf("\n");
printf("\n Enter a sentence:- ");
gets(text);
n = strlen(text);
printf("\n\n Input your choice(1 to 6):-
"); scanf("%d", &choice);
for(i=0;i<35;i++)
printf("-");
printf("\n");
switch(choice)
{
case 1:
for(i=0;i<n;i++)
text[i] = toupper(text[i]);
printf("%s", text);
break;

case 2:
for(i=0;i<n;i++)
text[i] = tolower(text[i]);
text[0] = toupper(text[0]);
printf("%s", text);
break;

case 3:
for(i=0;i<n;i++)
text[i] = tolower(text[i]);
printf("%s", text);
break;

case 4:
for(i=0;i<n;i++)
text[i] = tolower(text[i]);
text[0] = toupper(text[0]);
for(j=0;j<n;j++)
{
if(text[j] == ' ')
text[j+1] = toupper(text[j+1]);
}
puts(text);
break;
case 5:
for(i=0;i<n;i++)
text[i] = toupper(text[i]);
text[0] = tolower(text[0]);
for(j=0;j<n;j++)
{

| Structured Programming 121


if(text[j] == ' ')
text[j+1] = tolower(text[j+1]);

| Structured Programming 122


}
puts(text);
break;
case 6:
exit(1);
break;
default:
printf("\n Invalid Choice");
break;
}
getch();
}

11) Develop a program that receives the month and year from the keyboard as integers
and prints the calendar in the following format.
March 2006
Mon Tue Wed Thu Fri Sat Sun
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 23 25 26
27 28 29 30 31

12) Write a program to read and perform following string operations.


b) A string and find length of the given string.
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
int length;
char string[100];
clrscr();
printf("\n Input any text:- ");
gets(string);
length = strlen(string);
printf("\n There are %d characters in the string
\"%s\"",length, string);
getch();
}

c) A string and reverse the string.


/*Reverse character*/
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
int length;
char string[100],string1[100];
clrscr();
printf("\n Input any text:- ");

| Structured Programming 123


gets(string);
strcpy(string1,string);

| Structured Programming 124


strrev(string);
printf("\n The Reverse of \"%s\" text is \"%s\"",string1,
string);
getch();
}

d) Convert the string into upper case letters.


/*converting lower case string into upper case*/
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
int length;
char string[100],string1[100];
clrscr();
printf("\n Input any lower case text:-
"); gets(string);
strcpy(string1,string);
printf("\n The upper case of \"%s\" is \"%s\"",string1,
strupr(string));
getch();
}

e) A string and count the number of digits, vowels, consonants, white space and
other characters.
/*counting the number of vowels, consonants, symbols and words
in a given text*/
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
int i,v_count=0,c_count=0,b_count=0,length;
char string[100];
clrscr();
printf("\n Input any text:- ");
gets(string);
strlwr(string);
length = strlen(string);
for(i=0;i<length;i++)
{
if(string[i]=='a'||string[i]=='e'||string[i]=='i'||string
[i]=='o'||string[i]=='u')
v_count++;
else if(string[i]==' ')
b_count++;
else
c_count++;
}
printf("\n Given Text is \"%s\"",string);
printf("\n There are %d vowels, %d consonants, %d
blank spaces and %d

| Structured Programming 125


words",v_count,c_count,b_count,b_count+1);
getch();

| Structured Programming 126


}

f) A string and check whether it is a palindrome or not.


/*Palindrome character*/
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
int length;
char string[100],string1[100];
clrscr();
printf("\n Input any text:- ");
gets(string);
strlwr(string);
strcpy(string1,string);
strrev(string);
if(strcmp(string,string1)==0)
printf("\n The text \"%s\" is a palindrome text",
string1);
else
printf("\n The text \"%s\" is a non-palindrome text",
string1);
getch();
}

g) Copy the message from one location of the screen to another location.

FUNCTION PROGRAMS
1) Write a program to find factorial of a given number, using function.

#include<stdio.h>
#include<conio.h>
long int factorial(int);
void main()
{
int num[10],i;
long int result;
clrscr();
printf("\n Input any 10 numbers:-
"); for(i=0;i<10;i++)
{
printf("\n Number [%d]:-
",i); scanf("%d", &num);
if(num<0)
printf("\n Invalid Number");
else
{
result=factorial(num[i]);
printf("\n Factorial of %d is %ld",
num[i],result);
| Structured Programming 127
}
}

| Structured Programming 128


getch();
}

long int factorial(int n)


{
int i;
long int fact=1;
if(n==0 || n==1)
return(1);
else
{
for(i=1;i<=n;i++)
fact=fact*i;
return(fact);
}
}

2) Write a program to check whether the given number is prime number or composite,
using function.
#include<stdio.h>
#include<conio.h>
void prime(int,int);

void main()
{
int num[10],i;
clrscr();
printf("\n Input 10 numbers:- ");
for(i=0;i<10;i++)
{
printf("\n Number [%d]:-
",i+1); scanf("%d", &num[i]);
}
printf("\n Prime Numbers
are:\n"); for(i=0;i<10;i++)
prime(num[i],10);
getch();
}

void prime(int num, int n)


{
int i;
for(i=2;i<num;i++)
{
if(num%i==0)
break;
}
if(i==num)
printf("%5d",num);
}

3) Write a program to find the reverse of a given number, using function.


4) Write a program display using„n‟recursionterms. of Fibon
| Structured Programming 129
#include<stdio.h>
#include<conio.h>

| Structured Programming 130


fib(int, int,
int); void main()
{
int n;
clrscr();
printf("\n How many terms:- ");
scanf("%d", &n);
fib(0, 1, n);
getch();
}

fib(int a, int b, int c)


{
if(c!=0)
{
printf("%5d", a);
fib(b, b+a, c-1);
}
}

5)

STRUCTURE AND UNION PROGRAMS

1) Write a program to input title name, author name, and number of pages and price
of a book and display them.
2) Write a program to input name and address of 'n' number of students and sort them
by name using structure variable.
#include<stdio.h>
#include<conio.h>
#include<string.h>
struct student
{
char name[50];
char
address[50]; };
struct student s[100];

void main()
{
int i, j, n;
char temp[50];
clrscr();
printf("\n How many students:-
"); scanf("%d", &n);
printf("\n Input name and address of %d students:- ",
n); for(i=0;i<n;i++)
{
fflush(stdin);
printf("\n Name[%d]:-
",i+1); gets(s[i].name);
printf("\n Address[%d]:- ", i+1);

| Structured Programming 131


gets(s[i].address);
}

| Structured Programming 132


for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(strcmpi(s[i].name,s[j].name)>0)
{
strcpy(temp, s[i].name);
strcpy(s[i].name, s[j].name);
strcpy(s[j].name, temp);
strcpy(temp, s[i].address);
strcpy(s[i].address, s[j].address);
strcpy(s[j].a ddress, temp);
}
}
}
printf("\n Sorted List:-\n");
printf("\n Name\t
Address\n"); for(i=0;i<n;i++)
printf("\n%s \t %s", s[i].name, s[i].address);

getch();
}

3) In a bank there are 'n' customers with attributes name, account number and
balance. Write a program to find the information of the customer who has highest
balance using structure.
#include<stdio.h>
#include<conio.h>
#include<string.h>
struct customer
{
char name[50];
int acc_no,
balance; };
struct customer c[100];

void main()
{
int i, n, max;
clrscr();
printf("\n How many customers:-
"); scanf("%d", &n);
printf("\n Input following information of %d customers:- ", n);
for(i=0;i<n;i++)
{
fflush(stdin);
printf("\n Name[%d]:-
",i+1); gets(c[i].name);
printf("\n Account number[%d]:- ",
i+1); scanf("%d", &c[i].acc_no);
printf("\n Balance[%d]:- ", i+1);
scanf("%d", &c[i].balance);
}

| Structured Programming 133


max=c[0].balance;

| Structured Programming 134


for(i=0;i<n;i++)
{
if(c[i].balance>max)
max = c[i].balance;
}
/*Result*/
for(i=0;i<n;i++)
{
if(c[i].balance==max)
printf("\n%s\t%d\t%d",c[i].name,c[i].acc_no,
c[i].balance);
}
getch();
}

4) Write a program to input name, roll number, and marks of a student and display
the name, roll number and total marks.
#include<stdio.h>
#include<conio.h>
struct student
{
char name[50];
int roll;
};

struct marks
{
int mark[8];
struct student
s; };
struct marks m;

void main()
{
int i, total=0;
clrscr();
printf("\n Input name of a student:-
"); gets(m.s.name);
printf("\n Input Roll number:-
"); scanf("%d", &m.s.roll);
printf("\n Input marks of 8 subjects\n");
for(i=0;i<8;i++)
{
printf("\n Marks[%d]:- ",i+1);
scanf("%d", &m.mark[i]); total
= total + m.mark[i];
}

/*Result*/
printf("\n Name:- %s", m.s.name);
printf("\n Roll Number:- %d",
m.s.roll); printf("\n Total:- %d",
total); getch();

| Structured Programming 135


}

| Structured Programming 136


5) A record contains name of cricketer, his age, number of test matches that he has
played and the average runs that he has scored in each test match. create an array of
structures to hold records of 20 such cricketers and then write a program to read
these records and arrange them in ascending order by average runs.
#include<stdio.h>
#include<conio.h>
#include<string.h>
struct cricketer
{
char name[50];
int age,test_match, avg_run;
};
struct cricketer c[200];

void main()
{
int i, j, n, temp1;
char temp[50];
clrscr();
printf("\n How many cricketers:- ");
scanf("%d", &n);
printf("\n Input information of %d cricketers", n);
for(i=0;i<n;i++)
{
fflush(stdin);
printf("\n Name[%d]:- ",
i+1); gets(c[i].name);
printf("\n Age[%d]:- ",i+1);
scanf("%d", &c[i].age);
printf("\n Number of test match[%d]:- ", i+1);
scanf("%d", &c[i].test_match);
printf("\n Average Runs[%d]:- ", i+1);
scanf("%d", &c[i].avg_run);
}
/*Sorting*/
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(c[i].avg_run>c[j].avg_run)
{
strcmp(temp,c[i].name);
strcmp(c[i].name,c[j].name);
strcmp(c[j].name,temp);
temp1 = c[i].age;
c[i].age = c[j].age;
c[j].age = temp1;
temp1 = c[i].test_match;
c[i].test_match = c[j].test_match;
c[j].test_match = temp1;
temp1 = c[i].avg_run;
c[i].avg_run = c[j].avg_run;
c[j].avg_run = temp1;

| Structured Programming 137


}
}

| Structured Programming 138


}
clrscr();
/*Sorted List*/
printf("\n Information in Ascending order\n");
printf("\n Name\t Age\t Test Match\t Average run");
for(i=0;i<n;i++)
printf("\n%s\t%d\t%d\t%d",c[i].name,c[i].age,c[i].test_match,c
[i].avg_run);
getch();
}

6) There is a structure called employee that holds information like employee code,
name, date of joining. Write a program to create an array of structures and enter
some data into it. then ask the user to enter current date. Display the names of those
employees whose tenure is 3 or more than 3 years according to the given current
date.
#include<stdio.h>
#include<conio.h>
#include<string.h>

struct date
{
int year, month, day;
};

struct employee
{
char name[50];
int emp_code;
struct date d;
};
struct employee e[200];

void main()
{
int i, n, curr_year, curr_date, curr_day,
service_year[50]; clrscr();
printf("\n Input current date:- "); scanf("%d-%d-
%d",&curr_year, &curr_date, &curr_day); printf("\n
How many employees:- ");
scanf("%d", &n);
printf("\n Input information's of %d employees",
n); for(i=0;i<n;i++)
{
fflush(stdin);
printf("\n Name[%d]:- ",
i+1); gets(e[i].name);
printf("\n Employee Code[%d]:- ",i+1);
scanf("%d", &e[i].emp_code);
printf("\n Date of join[%d]:- ", i+1);
scanf("%d-%d-%d", &e[i].d.year, &e[i].d.month,
&e[i].d.day);
service_year[i] = curr_year - e[i].d.year;
| Structured Programming 139
}
clrscr();

| Structured Programming 140


printf("\n Complete Information of Employee");
printf("\n Code\t Name\t\t Joining date");
for(i=0;i<n;i++)

printf("\n%5d\t%s\t%d/%d/%d",e[i].emp_code,e[i].name,e[i].d.year,
e[i].d.month, e[i].d.day);

printf("\n\n Required Information");


printf("\n Code\t Name\t\t Joining
date"); for(i=0;i<n;i++)
{
if(service_year[i]>=3)
printf("\n%3d\t%s\t%d/%d/%d",e[i].emp_code,e[i].name,e[i]
.d.year, e[i].d.month, e[i].d.day);
}
getch();
}

7) Create a structure to specify data of customers in a bank. The data to be stored is:
account number, name, balance in account. Assume maximum of 200 customers in
the bank.
a) write a function to print the account number and name of each customer with
balance below Rs. 100.
#include<stdio.h>
#include<conio.h>
struct bank
{
char name[50];
int acc_no,
balance; };
struct bank b[200];

void main()
{
int I;
clrscr();
printf("\n Input following information's of 200 customer:-
"); for(i=0;i<200;i++)
{
fflush(stdin);
printf("\n Name[%d]:- ",
i+1); gets(b[i].name);
printf("\n Account Number[%d]:-
",i+1); scanf("%d", &b[i].acc_no);
printf("\n Balance[%d]:- ", i+1);
scanf("%d", &b[i].balance);
}
clrscr();
printf("\n Complete information:");
printf("\n************************************");
printf("\n Name\t Account\t Balance");
for(i=0;i<200;i++)
if(b[i].balance<=100)
| Structured Programming 141
printf("\n%s\t%d\t%d",
b[i].name,b[i].acc_no,b[i].balance);

| Structured Programming 142


printf("\n************************************");
printf("\n Required Record");
printf("\n Name\t Account\t Balance");
for(i=0;i<200;i++)
{
if(b[i].balance<=100)
printf("\n%s\t%d\t%d",
b[i].name,b[i].acc_no,b[i].balance);
}
getch();
}

b) if a customer requests for withdrawal or deposit, it is given in the


form: acct_no, amount code (1 for deposit, 0 for withdrawal)
Write a program to given a message, "The balance is insufficient for the specified
withdrawal".
8) Write a menu driven program that depicts the working of a library. The menu
options should be:
1. Add book information
2. Display book information
3. List all books of given author
4. List the title of specified book
5. List the count of books in the library
6. List the books in the order of accession number.
7. Exit
Create a structure called library to hold accession number, title of the book, author
name, price of the book, and flag indicating whether book is issued or not.
9) Write a program to input roll number, name and percentage of „n‟ find the followings.

a) Display record of all students.


b) Display the record of those students whose percentage is greater than 80.
c) Find the name of greatest percentage student.
d) Count the number of students whose percentage is between 50 and 60.
e) Display the percentage in ascending order.
#include<stdio.h>
#include<conio.h>
#include<string.h>

struct student
{
char
name[50]; int
roll, per; };
struct student s[200];

void main()
{
int choice, n, i, j, count = 0, highest,
temp1; char temp[50];
clrscr();

| Structured Programming 143


printf("\n How many students:-
"); scanf("%d", &n);

| Structured Programming 144


printf("\n Input information of %d students",
n); for(i=0;i<n;i++)
{
fflush(stdin);
printf("\n Name[%d]:- ",
i+1); gets(s[i].name);
printf("\n Roll Number[%d]:- ",
i+1); scanf("%d", &s[i].roll);
printf("\n Percentge[%d]:- ",
i+1); scanf("%d", &s[i].per);
if(s[i].per>=80)
count++;
if(s[i].per>=50 &&
s[i].per<=60) count++;
}
clrscr();
printf("\n Menu");
printf("\n 1. Display All Records");
printf("\n 2. Display records of students securing percentage
above 80%");
printf("\n 3. Display the name of student who has
highest percentage");
printf("\n 4. Count the number of students having
percentage between 50 and 60");
printf("\n 5. Display the percentage in ascending order");
printf("\n 6. Exit");
printf("\n Input your choice ( 1 to 6):- ");
scanf("%d", &choice);
switch(choice)
{
case 1:
printf("\n----------------------------------------\n");
printf("\n \t Records\n");
printf("\n----------------------------------------\n");
printf("\n Name \t\tRoll \t Percentage");
for(i=0;i<n;i++)
printf("\n %s\t%d\t%d", s[i].name, s[i].roll, s[i].per);
break;

case 2:
clrscr();
printf("\n----------------------------------------\n");
printf("\n \t Records\n");
printf("\n----------------------------------------\n");
for(i=0;i<n;i++)
printf("\n %s\t%d\t%d", s[i].name, s[i].roll, s[i].per);

printf("\n----------------------------------------\n");
printf("\n There are %d students\n ", count);
printf("\n Name \t\tRoll \t Percentage");
for(i=0;i<n;i++)
{
if(s[i].per>=80)

| Structured Programming 145


printf("\n %s\t%d\t%d", s[i].name,
s[i].roll, s[i].per);

| Structured Programming 146


}
break;

case 3:
highest = s[0].per;
for(i=0;i<n;i++)
{
if(s[i].per>highest)
highest = s[i].per;
}
clrscr();
printf("\n----------------------------------------\n");
printf("\n \t Records\n");
printf("\n----------------------------------------\n");
for(i=0;i<n;i++)
printf("\n %s\t%d\t%d", s[i].name, s[i].roll, s[i].per);
printf("\n----------------------------------------\n");
printf("\n Record of highest percentage \n"); printf("\n
Name \t\tRoll \t Percentage"); for(i=0;i<n;i++)
{
if(s[i].per == highest)
printf("\n %s\t%d\t%d", s[i].name, s[i].roll, s[i].per);
}
break;

case 4:
clrscr();
printf("\n----------------------------------------\n");
printf("\n \t Records\n");
printf("\n----------------------------------------\n");
for(i=0;i<n;i++)
printf("\n %s\t%d\t%d", s[i].name, s[i].roll, s[i].per);
printf("\n----------------------------------------\n");
printf("\n There are %d students\n ",
count); printf("\n Name \t\t Roll \t
Percentage"); for(i=0;i<n;i++)
{
if(s[i].per>=50 && s[i].per<=60)
printf("\n %s\t%d\t%d", s[i].name, s[i].roll, s[i].per);
}
break;

case 5:
clrscr();
printf("\n----------------------------------------\n");
printf("\n \t Records\n");
printf("\n----------------------------------------\n");
for(i=0;i<n;i++)
printf("\n %s\t%d\t%d", s[i].name, s[i].roll, s[i].per);
printf("\n-------------------------------------
---\n");
for(i=0;i<n;i++)

| Structured Programming 147


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

| Structured Programming 148


{
if(s[i].per>s[j].per)
{
strcpy(temp,s[i].name);
strcpy(s[i].name,s[j].name);
strcpy(s[j].name,temp);
temp1 = s[i].roll;
s[i].roll = s[j].roll;
s[j].roll = temp1;
temp1 = s[i].per;
s[i].per = s[j].per;
s[j].per = temp1;
}
}
}
printf("\n Records in ascending
order\n"); printf("\n Name \t\t Roll \t
Percentage"); for(i=0;i<n;i++)
printf("\n %s\t%d\t%d", s[i].name, s[i].roll, s[i].per);
break;

case 6:
exit();
break;

default:
printf("\n Invalid Choice");
break;
}
getch();
}

| Structured Programming 149

You might also like