C Solved Problems PDF
C Solved Problems PDF
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();
}
| 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();
}
| Structured Programming 2
inch = 12*feet;
printf("\n %d Feet = %d Inch", feet, inch);
| Structured Programming 3
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();
}
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();
}
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();
}
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
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
#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();
}
>= 80 Distinction
>= 60 First Division
| 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();
}
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();
}
| Structured Programming 42
sum = sum+term;
printf("%10ld", term);
| Structured Programming 43
}
printf("\n Sum = %ld", sum);
getch();
}
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();
}
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();
}
| 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();
}
| 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();
}
| 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();
}
| 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();
}
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();
}
| 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();
}
| 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();
}
| 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);
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
}
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();
}
| 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");
}
getch();
}
| 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]);
}
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]);
}
| 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);
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");
}
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);
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();
}
| 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();
}
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();
}
/*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();
}
| 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();
}
clrscr();
printf("\n The Matrix
is:\n"); for(i=0; i<r; i++)
{
printf("\n");
for(j=0; j<c; 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]);
}
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;
}
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()
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()
{
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.
void main()
{
int count = 0, i=0;
char text[50];
clrscr();
printf("\n Enter a sentence:- ");
gets(text);
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();
}
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();
}
{
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++)
{
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();
}
#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");
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++)
{
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
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
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
}
}
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();
}
5)
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);
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);
}
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();
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;
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();
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);
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);
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();
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)
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++)
case 6:
exit();
break;
default:
printf("\n Invalid Choice");
break;
}
getch();
}