Exercise 1 Set B: Accept X and y Coordinate of Two Points and Compute The Distance Between The Two Points
Exercise 1 Set B: Accept X and y Coordinate of Two Points and Compute The Distance Between The Two Points
Roll No : B-46
Class : FYBsc
Exercise 1
Set B
Question 1:
Accept x and y coordinate of two points and compute the distance between the
two points.
Solution:
#include <stdio.h>
#include <math.h>
main(){
float x1,y1,x2,y2,distance;
printf("Enter the coordinates of first point\n");
scanf("%f%f",&x1,&y1);
printf("Enter the coordinates of second point\n");
scanf("%f%f",&x2,&y2);
distance = sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
printf("The distance between two points : %f\n",distance);
}
Output :
Question 2 :
Name : Sayyad Saquib Anwar
Roll No : B-46
Class : FYBsc
Accept two integer from user and interchange them . Display the
interchanged numbers.
Solution :
#include <stdio.h>
main(){
int a,b;
printf("Enter the numbers a and b\n");
scanf("%d%d",&a,&b);
a = (a+b) - (b = a);
printf("Interchanged values of a and b are :\n");
printf("a = %d\nb = %d\n",a,b);
}
Output :
Question 3 :
Name : Sayyad Saquib Anwar
Roll No : B-46
Class : FYBsc
A cashier has currency notes of denomination 1, 5 and 10. Accept the
amount to be withdrawn from the user and print the total number of currency
notes of each denomination the cashier will have to give.
Solution :
#include <stdio.h>
main(){
int a;
printf("Enter amount \n");
scanf("%d",&a);
printf("Number of notes of 10 = %d\n",a/10);
a = a%10;
printf("Number of notes of 5 = %d\n",a/5);
printf("Number of notes of 1 = %d\n",a%5);
}
Output :
Enter amount
1567
Number of notes of 10 = 156
Number of notes of 5 = 1
Number of notes of 1 = 2
Exercise 2
Set B
Question 1 :
Solution :
#include <stdio.h>
main(){
char ch;
printf("Enter character \n");
scanf("%c",&ch);
if(ch>=48&&ch<=58)
printf("Digit\n");
else if(ch>=97&&ch<=122)
printf("Lower case\n");
else if(ch>=65&&ch<=90)
printf("Upper case\n");
}
Output :
Enter character
a
Lower case
Question 2 :
Accept the time as hour , minute and seconds and check whether the time
is valid .
Solution :
#include <stdio.h>
main(){
int h , m ,s;
printf("Enter time in format hh mm ss\n");
Name : Sayyad Saquib Anwar
Roll No : B-46
Class : FYBsc
scanf("%d%d%d",&h,&m,&s);
if((h>=0&&h<24)&&(m>=0&&m<60)&&(s>=0&&s<60))
printf("Valid\n");
else
printf("Invalid\n");
}
Output :
Question 3 :
Solution :
#include <stdio.h>
main(){
int year;
printf("Enter year\n");
scanf("%d",&year);
if(((year%4==0)&&(year%100!=0))||(year%400==0))
Name : Sayyad Saquib Anwar
Roll No : B-46
Class : FYBsc
printf("It is LEAP year\n");
else
printf("It is NOT LEAP year\n");
}
Output :
Enter year
1890
It is NOT LEAP year
Question 4 :
Accept three sides of triangle as input , and print whether the triangle is
valid or not .
Solution :
#include <stdio.h>
main(){
int a,b,c;
printf("Enter sides of triangle\n");
scanf("%d%d%d",&a,&b,&c);
if((a+b>c)&&(b+c>a)&&(a+c>b))
printf("Valid\n");
else
Name : Sayyad Saquib Anwar
Roll No : B-46
Class : FYBsc
printf("Invalid\n");
}
Output :
Question 5 :
Accept x and y coordinate of a point and find the quadrant in which the
point lies.
Solution :
#include <stdio.h>
main(){
int x,y;
printf("Enter the coordinates x and y\n");
scanf("%d%d",&x,&y);
if(x>0&&y>0)
printf("This point lies in the First Quadrant\n");
else if(x>0&&y<0)
printf("This point lies in the Fourth Quadrant\n");
else if(x<0&&y>0)
Name : Sayyad Saquib Anwar
Roll No : B-46
Class : FYBsc
printf("This point lies in the Second Quadrant\n");
else if(x<0&&y<0)
printf("This point lies in the Third Quadrant\n");
}
Output :
Question 6 :
Solution :
#include <stdio.h>
#include <math.h>
main(){
int a,b,c;
float d;
printf("Finding roots of equation of the form \n a*x^2+b*x+c=0\n");
printf("Enter the values of constants a,b and c\n");
scanf("%d%d%d",&a,&b,&c);
d = b*b-4*a*c;
if(d==0)
printf("Given equation has 2 same roots ie %f\n",((-1)*b-sqrt(d))/2);
Name : Sayyad Saquib Anwar
Roll No : B-46
Class : FYBsc
else if(d<0)
printf("The given equation has no Real roots\n");
else if(d>0)
printf("Roots are %f and %f\n",(b-sqrt(d))/2,((-1)*b-sqrt(d))/2);
else if(x<0&&y<0)
printf("This point lies in the Third Quadrant\n");
}
Output :
Question 7 :
Accept the cost price and selling price from the keyboard . Find out if the
seller has made a profit or loss and display how much profit or loss has been
made.
Solution :
#include <stdio.h>
main(){
float sell_p ,cost_p;
printf("Enter the selling price\n");
scanf("%f",&sell_p);
printf("Enter the costing price\n");
scanf("%f",&cost_p);
if(sell_p > cost_p)
printf("Profit . Value : %f\n",sell_p-cost_p);
else if(sell_p < cost_p)
printf("Loss . Value : %f\n",cost_p+sell_p);
else
printf("No Loss and No profit\n");
}
Output :
Name : Sayyad Saquib Anwar
Roll No : B-46
Class : FYBsc
Set C
Question 1 :
Write a program to accept marks from three subjects and find the total
marks secured , average and also display the class obtained.
Solution :
#include <stdio.h>
main(){
float sub1,sub2,sub3,total,avg;
printf("Enter the marks of subjects\n");
scanf("%f%f%f",&sub1,&sub2,&sub3);
total = sub1+sub2+sub3;
avg = total/3;
printf("Total marks = %.1f\nAverage = %.1f\n",total,avg);
if( avg >=75)
printf("First Class\n");
else if(avg>=65&&avg<75)
printf("Second class\n");
else if(avg>=35&&avg<65)
printf("Pass class\n");
else
printf("FAIL\n");
}
Output :
Question 2 :
Write a program to accept quantity and rate for three items , compute the
total sales amount. Also compute and print the discount as follows :
Solution :
#include <stdio.h>
main(){
int rate1,rate2,rate3,qty1,qty2,qty3,total;
printf("Enter Quantities and rates\n");
printf("For first item\n");
scanf("%d%d",&qty1,&rate1);
printf("For second item\n");
scanf("%d%d",&qty2,&rate2);
printf("For third item\n");
scanf("%d%d",&qty3,&rate3);
total = qty1*rate1+qty2*rate2+qty3*rate3;
printf("Total sales amount = %d\n",total);
printf("Discount : ");
if( total >=1000){
printf("20 percent\n");
printf("Total amount including discount = %.1f\n",total*0.8);
Name : Sayyad Saquib Anwar
Roll No : B-46
Class : FYBsc
}
else if(total>=500&&total<1000){
printf("15 percent\n");
printf("Total amount including discount = %.1f\n",total*0.85);
}
else if(total>=300&&total<500){
printf("8 percent\n");
printf("Total amount including discount = %.1f\n",total*0.92);
}
else
printf(" No discount\n");
}
Output :
Question 3 :
A library charges a fine for every book returned late. Accept the number
of days the member is late , compute and print the fine as follows :
Solution :
#include <stdio.h>
main(){
int days;
printf("Enter number of late days\n");
scanf("%d",&days);
if( days <=5)
printf("Fine : Rs.50\n");
else if(days>5&&days<=10)
printf("Fine : Rs.100\n");
else
printf("Fine : Rs.150\n");
}
Output :
Exercise 3
Set B
Question 1 :
Accept radius from user and write a program having menu with the
following options and corresponding actions.
Options Actions
1. Area of circle Compute the area of circle and print
2. Circumference of Compute Circumference of circle and print
circle
3. Volume of sphere Compute Volume of sphere and print
Solution :
#include <stdio.h>
#define PI 3.14
main(){
int ch;
float rad;
printf("Menu\n");
printf("1. Area of Circle\n");
printf("2. Circumference of Circle\n");
printf("3. Volume of sphere\n\n");
printf("Enter choice");
scanf("%d",&ch);
printf("Enter Radius of circle\n");
scanf("%f",&rad);
switch(ch){
case 1:
printf("Area of Circle = %f\n" ,PI*rad*rad);
break;
case 2 :
printf("Circumference of Circle = %f\n",2*PI*rad);
break;
Name : Sayyad Saquib Anwar
Roll No : B-46
Class : FYBsc
case 3 :
printf("Volume of Sphere = %f\n",PI*rad*rad*rad/3);
break;
default :
printf("Invalid choice\n");
}
Output :
Menu
1. Area of Circle
2. Circumference of Circle
3. Volume of sphere
Enter choice
1
Enter Radius of circle
1
Area of Circle = 3.140000
Name : Sayyad Saquib Anwar
Roll No : B-46
Class : FYBsc
Question 2 :
Options Actions
1. Area of square Accept length , Compute the area of square and print
2. Area of Accept length and breadth , Compute area of rectangle and print
Rectangle
3. Area of Accept base and height , Compute area of triangle and print
Triangle
Solution :
#include <stdio.h>
main(){
int ch;
float len,bre,base,height;
printf("Menu\n");
printf("1. Area of Square\n");
printf("2. Area of rectangle\n");
printf("3. Area of triangle\n\n");
printf("Enter choice\n");
scanf("%d",&ch);
switch(ch){
case 1:
printf("Enter length\n");
scanf("%f",&len);
printf("Area of Square = %f\n" ,len*len);
break;
case 2 :
printf("Enter length and breadth\n");
scanf("%f%f",&len,&bre);
printf("Area of Rectangle = %f\n",len*bre);
break;
case 3 :
printf("Enter value of base and height \n");
scanf("%f%f",&base,&height);
printf("Area of triangle = %f\n",base*height/2);
break;
default :
Name : Sayyad Saquib Anwar
Roll No : B-46
Class : FYBsc
printf("Invalid choice\n");
}
Output :
Menu
1. Area of Square
2. Area of rectangle
3. Area of triangle
Enter choice
1
Enter length
4
Area of Square = 16.000000
Set C
Question 1:
Name : Sayyad Saquib Anwar
Roll No : B-46
Class : FYBsc
Accept the three positive integers for date from the user (day , month and
year) and check whether the date is valid or invalid.
Date Output
12 – 10 - 1984 Valid
32 – 10 - 1920 Invalid
10 -13 - 1984 Invalid
29 - 2 – 1984 Invalid
29 – 2 – 2003 Invalid
29 – 2 – 1900 Invalid
03 – 06 - 1993 Valid
Solution :
#include <stdio.h>
main(){
int d,m,y;
printf("Enter date in form dd mm yy\n");
scanf("%d%d%d",&d,&m,&y);
if(m>=1&&m<=12){
switch(d){
case 12:
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
if(d>=1&&d<=31)
printf("Valid\n");
break;
case 4:
case 6:
case 9:
case 11:
if(d>=1&&d<=30)
printf("Valid\n");
break;
case 2:
if((y%4==0&&y%100!=00)||(y%400==0))
if(d>=1&&d<=29)
printf("Valid\n");
else
if(d>=1&&d<=28)
Name : Sayyad Saquib Anwar
Roll No : B-46
Class : FYBsc
printf("Valid\n");
break;
default :
printf("Invalid\n");
break;
}
}
else
printf("Invalid\n");
}
Question 2 :
Write a program having menu that has three options – add , substract or
multiply two fractions. The two fractions and options are taken as input and the
result is displayed as output. Each fraction is read as two integers , numerator
and denominator.
Solution :
#include <stdio.h>
Name : Sayyad Saquib Anwar
Roll No : B-46
Class : FYBsc
int gcd(int a , int b){
while(b!=0){
if(b>a)
a = (a+b)-(a=b);
return gcd(b,a%b);
}
return a;
}
main(){
int ch, a1,b1,a2,b2,c,d,gr;
printf("Operation Menu on two fractions\n");
printf("1.Addition\n");
printf("2.Substraction\n");
printf("3.Multiplication\n");
switch(ch){
case 1:
c = a1*b2+a2*b1;
d = b1*b2;
gr = gcd(d,c);
c /= gr;
d /= gr;
printf("Addition = %d/%d\n",c,d);
break;
case 2:
c = a1*b2-a2*b1;
d = b1*b2;
gr = gcd(d,c);
c /= gr;
d /= gr;
printf("Substraction = %d/%d\n",c,d);
break;
case 3:
Name : Sayyad Saquib Anwar
Roll No : B-46
Class : FYBsc
c = a1*a2;
d = b1*b2;
gr = gcd(d,c);
c /= gr;
d /= gr;
printf("Multiplication = %d/%d\n",c,d);
break;
default :
printf("Invalid choice\n");
break;
}
Output :
Exercise 4
Set A
Question 1:
Write the program to accept the integer n and display all even numbers
upto n.
Solution :
#include <stdio.h>
Name : Sayyad Saquib Anwar
Roll No : B-46
Class : FYBsc
main(){
int n,i;
printf("Enter number\n");
scanf("%d",&n);
printf("All even numbers upto %d\n",n);
for(i=1; i<=n; i++)
if(i%2==0)
printf("%d\n",i);
}
Output :
Enter number
5
All even numbers upto 5
2
4
Question 2:
Accept two integer x and y and calculate the sum of all integers between
x and y(both inclusive ).
Solution :
#include <stdio.h>
main(){
int x,y,sum=0,i,temp;
printf("Enter x and y\n");
scanf("%d%d",&x,&y);
printf("Sum of all integers between %d and %d\n",x,y);
if(y<x){
temp = y;
y=x;
Name : Sayyad Saquib Anwar
Roll No : B-46
Class : FYBsc
x = temp;
}
for(i=x; i<=y; i++)
sum+=i;
printf("%d\n",sum);
}
Output:
Enter x and y
71
Sum of all integers between 7 and 1
28
Question 3 :
Solution :
#include <stdio.h>
main(){
int x,i,prod=1,n;
printf("Enter x and n\n");
scanf("%d%d",&x,&n);
printf("%d^%d is\n",x,n);
for(i=1; i<=n; i++)
prod *=x;
printf("%d\n",prod);
}
Output :
Enter x and n
23
2^3 is
Name : Sayyad Saquib Anwar
Roll No : B-46
Class : FYBsc
8
Question 4 :
Solution :
#include <stdio.h>
main(){
int x,i,chk=0,n;
printf("Enter number\n");
scanf("%d",&x);
for(i=2; i<=x/2; i++)
if(x%i==0){
chk=1;
break;
}
if(chk==1)printf("It is NOT PRIME\n");
else printf("It is PRIME\n");
}
Output :
Enter number
7
It is PRIME
Name : Sayyad Saquib Anwar
Roll No : B-46
Class : FYBsc
Question 5 :
Solution :
#include <stdio.h>
main(){
int x,cnt=0;
printf("Enter number\n");
scanf("%d",&x);
if(x==0)printf("The Number has 1 digit\n");
else{
for(; x!=0; x = x/10)
cnt++;
printf("The Number has %d digits \n",cnt);
}
}
Output :
Enter number
-1222
The Number has 4 digits
Name : Sayyad Saquib Anwar
Roll No : B-46
Class : FYBsc
Question 6 :
Solution :
#include <stdio.h>
main(){
int x,cnt=0,rno=0;
printf("Enter number\n");
scanf("%d",&x);
for(; x!=0; x = x/10)
rno =rno*10+x%10;
printf("Reverse number = %d\n",rno);
}
Output:
Enter number
234
Reverse number = 432
Name : Sayyad Saquib Anwar
Roll No : B-46
Class : FYBsc
Question 7 :
Solution :
#include <stdio.h>
main(){
int x,i;
char ch;
printf("Enter character\n");
scanf("%c",&ch);
printf("Enter integer\n");
scanf("%d",&x);
Output :
Enter character
A
Enter integer
5
Next 5 characters from 'A' are :
B
C
D
Name : Sayyad Saquib Anwar
Roll No : B-46
Class : FYBsc
E
F
Set B
Question 1:
Solution :
#include <stdio.h>
main(){
int n,i,fib1=0,fib2=1, fib;
printf("First n Fibonacci numbers\n");
printf("Enter n\n");
scanf("%d",&n);
printf("\n");
printf("%d\n",fib2);
for(i=1; i<n; i++){
fib = fib1+fib2;
printf("%d\n",fib);
fib1 = fib2;
fib2 = fib;
}
}
Output :
1
1
2
3
5
8
Name : Sayyad Saquib Anwar
Roll No : B-46
Class : FYBsc
13
21
Question 2 :
Write a program to accept real number x and integer n calculate the sum of first n
terms of the series x + 3x+5x + 7x +.....
Solution :
#include <stdio.h>
main(){
int n,i;
float x,sum=0;
printf("Calculating x+3x+5x+...\n\n");
printf("Enter real number x : ");
scanf("%f",&x);
printf("Enter number of terms n : ");
scanf("%d",&n);
for(i=1; i<=n; i++)
sum += (2*i-1)*x;
printf("Sum = %f\n",sum);
}
Name : Sayyad Saquib Anwar
Roll No : B-46
Class : FYBsc
Output :
Calculating x+3x+5x+...
Sum = 9.000000
Question 3 :
Write a program to accept real number x and integer n and calculate the sum of first
n terms of the series 1/x + 2/x^2+3/x^3 + ....
Solution :
#include <stdio.h>
#include <math.h>
main(){
int n,i;
float x,sum=0;
printf("Sum = %f\n",sum);
Output :
Sum = 6.000000
Question : 4
Write a program to accept character till the user enters EOF and count number of
alphabets and digits entered.
Solution :
#include <stdio.h>
#include <math.h>
#include <ctype.h>
main(){
char ch;
int digit = 0,cnt=0;
printf("Enter characters till CTRL + D\n");
while((ch=getchar())!=EOF){
if(isdigit(ch))
digit++;
else if(isalpha(ch))
Name : Sayyad Saquib Anwar
Roll No : B-46
Class : FYBsc
cnt++;
}
printf("Number of digits = %d\nNumber of characters = %d\n",digit ,cnt);
}
Output :
Question 5 :
Write a program , which accepts a number n and displays each digit in words.
Solution :
#include <stdio.h>
#include <math.h>
main(){
int n,rno=0,b=0,c_zero=0,rem;
printf("Enter number : ");
scanf("%d",&n);
while(n!=0){
rno = rno*10 + n%10;
if(n%10==0&&b==0){
c_zero++;
}
Name : Sayyad Saquib Anwar
Roll No : B-46
Class : FYBsc
else b=1;
n/=10;
}
while(rno!=0){
rem = rno%10;
switch(rem){
case 0:
printf("Zero ");
break;
case 1:
printf("One ");
break;
case 2:
printf("Two ");
break;
case 3:
printf("Three ");
break;
case 4:
printf("Four ");
break;
case 5:
printf("Five ");
break;
case 6:
printf("Six ");
break;
case 7:
printf("Seven ");
break;
case 8:
printf("Eight ");
break;
case 9:
printf("Nine ");
break;
}
rno/=10;
}
while(c_zero>0){
printf("Zero ");
c_zero--;
}
printf("\n");
}
Output :
Exercise 5
Set A
Question 1 :
Write a program to display all prime numbers between 1 and 500.
Solution :
#include <stdio.h>
int isprime(int n){
int k,b=0;
for(k=2; k<=n/2; k++)
Name : Sayyad Saquib Anwar
Roll No : B-46
Class : FYBsc
if(n%k==0){
b=1;
break;
}
if(b==1) return 1;
else return 0;
}
main(){
int i;
printf("Prime numbers between 1 to 500\n");
for(i=2; i<=500; i++)
if(isprime(i)==0)
printf("%d\t",i);
}
Output :
2 3 5 7 11 13 17 19 23 29 31 37
41 43 47 53 59 61 67 71 73 79 83 89
97 101 103 107 109 113 127 131 137 139 149
151 157 163 167 173 179 181 191 193 197 199
211 223 227 229 233 239 241 251 257 263 269
271 277 281 283 293 307 311 313 317 331 337
347 349 353 359 367 373 379 383 389 397 401
409 419 421 431 433 439 443 449 457 461 463
467 479 487 491 499
Question : 2
Write a program to display multiplication tables from 2 to 5 having n
multiples each. The output should be displayed in a tabular format.
Solution :
#include <stdio.h>
main(){
int i,j;
for(i=1; i<=10; i++){
for(j=2; j<=5; j++)
Name : Sayyad Saquib Anwar
Roll No : B-46
Class : FYBsc
printf("%2d x %2d = %2d\t",j,i,i*j);
printf("\n");
}
}
Output :
2x 1= 2 3x 1= 3 4x 1= 4 5x 1= 5
2x 2= 4 3x 2= 6 4x 2= 8 5 x 2 = 10
2x 3= 6 3x 3= 9 4 x 3 = 12 5 x 3 = 15
2x 4= 8 3 x 4 = 12 4 x 4 = 16 5 x 4 = 20
2 x 5 = 10 3 x 5 = 15 4 x 5 = 20 5 x 5 = 25
2 x 6 = 12 3 x 6 = 18 4 x 6 = 24 5 x 6 = 30
2 x 7 = 14 3 x 7 = 21 4 x 7 = 28 5 x 7 = 35
2 x 8 = 16 3 x 8 = 24 4 x 8 = 32 5 x 8 = 40
2 x 9 = 18 3 x 9 = 27 4 x 9 = 36 5 x 9 = 45
2 x 10 = 20 3 x 10 = 30 4 x 10 = 40 5 x 10 = 50
Question 3 :
A B C D
E F G
H I
J
Solution :
#include <stdio.h>
main(){
int n,i;
Name : Sayyad Saquib Anwar
Roll No : B-46
Class : FYBsc
printf("Enter number of lines\n");
scanf("%d",&n);
char ch = 'A';
for(;n>0;n--){
for(i=1; i<=n; i++)
printf("%c\t",ch++);
printf("\n");
}
}
Output :
Set B
Question 1 :
Write a program to display all armstrong numbers between 1 and 500.
(An Armstrong number is a number such that the sum of cube of digit= number
itself )
Solution :
#include <stdio.h>
#include <math.h>
int isarm(int n){
int sum = 0,no;
no=n;
while(n!=0){
sum+=pow(n%10,3);
Name : Sayyad Saquib Anwar
Roll No : B-46
Class : FYBsc
n/=10;
}
if(no==sum)return 1;
else return 0;
}
main(){
int i;
Output :
Question 2 :
Accept character till user enters EOF and count the number of lines
entered. Also display the length of the longest line.
Solution:
#include <stdio.h>
main(){
int i,max = 0,l_cnt=0,len=0;
char ch;
printf("Enter characters till CTRL + D\n");
while((ch=getchar())!=EOF){
if(ch!='\n')
len++;
else{
l_cnt++;
if(len>max)
Name : Sayyad Saquib Anwar
Roll No : B-46
Class : FYBsc
max = (len+max)-(len=max);
len=0;
}
}
printf("Number of lines = %d\n",l_cnt);
printf("Length of longest line = %d\n",max);
}
Output :
Question 3 :
Solution:
#include <stdio.h>
int isperfect(int n){
int sum = 0,j=1;
while(j<=n/2){
if(n%j==0)
sum+=j;
j++;
}
if(n==sum)return 1;
else return 0;
}
Name : Sayyad Saquib Anwar
Roll No : B-46
Class : FYBsc
main(){
int i;
printf("All perfect number below 500\n");
for(i=1; i<=500; i++){
if(isperfect(i)==1)
printf("%d\n",i);
}
printf("\n");
Output :