Top Banner
INTRODUCTION TO COMPUTER PROGRAMMING ASSIGNMENT NO. 3 SUBMITTED BY; HARIS HASSAN SP13-BET-031 SUBMITTED TO; SIR MUHAMMAD USMAN YASIN
20

c programs

Jan 20, 2015

Download

Education

Haris Hassan

Write a program to compute area of a circle after reading input from user.

2. Write a program to calculate total marks.

3. Write a program to calculate total marks and percentage of three subjects.

4. Write a program to find out power of any number.

5. Write a program to Sum, Subtract, Multiply & Division of two numbers.

6. Evaluate the following expressions and show their hierarchy.

s = qui * add / 4 - 6 / 2 + 2 / 3 * 6 / g;

(qui = 4, add = 2, g = 2, assume s to be an int)

s = 1 / 3 * a / 4 - 6 / 2 + 2 / 3 * 6 / g;

(a = 4, g = 3, assume s to be an int)

7. Temperature of a city in Fahrenheit degrees is input through the keyboard. Write a program to convert this temperature into Centigrade degrees.

8. The length & breadth of a rectangle and radius of a circle are input through the keyboard. Write a program to calculate the area & perimeter of the rectangle, and the area & circumference of the circle.

9. Two numbers are input through the keyboard into two locations C and D. Write a program to interchange the contents of C and D.

10. The distance between two cities (in km.) is input through the keyboard. Write a program to convert and print this distance in meters, feet, inches and centimeters

11. Mr X’s basic salary is input through the keyboard. His dearness allowance is 40% of basic salary, and house rent allowance is 20% of basic salary. Write a program to calculate his gross salary
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: c programs

INTRODUCTION TO COMPUTER PROGRAMMING

ASSIGNMENT NO. 3

SUBMITTED BY;

HARIS HASSAN

SP13-BET-031

SUBMITTED TO;

SIR MUHAMMAD USMAN YASIN 

Page 2: c programs

1. Write a program to compute area of a circle after reading input from user.

#include <stdio.h>#include <conio.h>

int main(){ float area, radius, pi=3.141; printf("Enter the radius of circle \t"); scanf("%f", &radius); printf("Area of a circle is %f", area=pi*radius*radius); getch();}

Page 3: c programs
Page 4: c programs

2.   Write a program to calculate total marks.

#include<stdio.h>

#include<conio.h>

main()

{

int n, a, b, c=0;

printf("\n\nwrite the number of subjects\t");

scanf("%i", &n);

for(int a=0;a<n;a++)

{

printf("enter the marks of subject\t");

scanf("%i", &b);

c=c+b;

}

printf("\n\n\t\tTOTAL MARKS ARE %i", c);

getch();

}

Page 5: c programs

Another output of this program is

Page 6: c programs

3.Write a program to calculate total marks and percentage of three subjects.

#include <stdio.h>

#include <conio.h>

int main()

{

float a, b, c ;

printf("Enter the marks of first subject \t");

scanf("%f", &a);

printf("Enter the marks of second subject \t");

scanf("%f", &b);

printf("Enter the marks of third subject \t");

scanf("%f", &c);

printf("Total marks of subject are\t\t %f \n" , a+b+c);

printf("Percentage of total marks is \t %f", (a+b+c)/300*100);

getch();

}

Page 7: c programs

4.   Write a program to find out power of any number.

#include<conio.h>

#include<stdio.h>

main()

{

int num, pow, a=1, i;

printf("Enter the number\t");

scanf("%d", &num);

printf("\nEnter its power\t\t");

scanf("%d", &pow);

for(i=1;i<=pow;i++)

{

a=a*num;

}

printf("\n\n\t\t%d^%d is %d", num, pow, a);

getche();

}

Page 8: c programs

5.   Write a program to Sum, Subtract, Multiply & Division of two numbers.

#include <stdio.h>

#include <conio.h>

int main()

{

float a, b ;

printf("Enter the marks of first number \t");

scanf("%f", &a);

printf("Enter the marks of second number \t");

scanf("%f", &b);

printf("Sum of the numbers is %f\n", a+b);

printf("difference of the numbers is %f\n" , a-b);

printf("product of the numbers is %f\n", a*b);

printf("and dividing both numbers we get %f", a/b);

getch();

}

Page 9: c programs

6.   Evaluate the following expressions and show their hierarchy.

s = qui * add / 4 - 6 / 2 + 2 / 3 * 6 / g;

(qui = 4, add = 2, g = 2, assume s to be an int)

Ans;

S = 4 * 2 / 4 – 6 / 2 + 2 / 3 * 6 / 2;

S = 4 * 0.5 – 3 + 0.66 * 3; division operation

S = 2 – 3 + 2; multiplication operation

S = 4 – 3; addition operation

S = 1; subtraction operation

s = 1 / 3 * a / 4 - 6 / 2 + 2 / 3 * 6 / g;

(a = 4, g = 3, assume s to be an int)

s = 1 / 3 * 4 / 4 – 6 / 2 + 2 / 3 * 6 / 3

s = 0.33 * 1 – 3 + .66 * 2

s = 0.33 – 3 + 1.33

s = 1.66 – 3

s= –1.33

Page 10: c programs

7. Temperature of a city in Fahrenheit degrees is input through the keyboard. Write a program to convert this temperature into Centigrade degrees.

#include <stdio.h>#include <conio.h>

int main(){ float fahr, cent ; printf("\n\nEnter the Temperature in Fahrenheit degrees \t"); scanf("%f", &fahr); printf("\n\nTemperature in centigrades is \t %f", cent=5*(fahr-32)/9); getch();}

Page 11: c programs

8.   The length & breadth of a rectangle and radius of a circle are input through the keyboard. Write a program to calculate the area & perimeter of the rectangle, and the area & circumference of the circle.

#include <stdio.h>#include <conio.h>

int main(){ float leng, brea, rad, arearec, areacir, peri, circum; printf("Enter the length of rectangle\t"); scanf("%f", &leng); printf("\nEnter the breadth of rectangle\t"); scanf("%f", &brea); printf("\nEnter the radius of circle\t"); scanf("%f", &rad); arearec=leng*brea; peri=2*(brea+leng); areacir=3.141*rad*rad; circum=2*3.141* rad; printf("\n\nArea of rectangle is\t\t%f", arearec); printf("\nPerimeter of rectangle is\t%f", peri); printf("\nArea of circle is\t\t%f", areacir); printf("\nCircumference of circle is\t%f", circum); getch();}

Page 12: c programs

9.   Two numbers are input through the keyboard into two locations C and D. Write a program to interchange the contents of C and D.

#include<stdio.h>#include<conio.h> main (){ int c, d, a; printf("Enter the number as content in location C\t"); scanf("%d",&c); printf("\nenter the number as content in location D\t"); scanf("%d",&d); a = c; c = d; d = a; printf("\nAfter Interchanging\n\nNow number stored in location C is\t%d\n",c); printf("\nand the number stored in location D is\t%d",d); getche(); return 0;}

Page 13: c programs

10.   The distance between two cities (in km.) is input through the keyboard. Write a program to convert and print this distance in meters, feet, inches and centimeters

#include<stdio.h>

#include<conio.h>

main ()

{

float km, metr, feet, inch, centi;

printf("Enter the distance betweeen two cities in KMs\t");

scanf("%f",&km);

metr=km*1000;

feet=metr/0.3048;

inch=feet*12;

centi=metr*100;

printf("\n\nThe distance betweeen two cities in meters is\t\t%f",metr);

printf("\nThe distance betweeen two cities in feet is\t\t%f",feet);

printf("\nThe distance betweeen two cities in inches is\t\t%f",inch);

printf("\nThe distance betweeen two cities in centimeters is\t%f",centi);

getch();

}

Page 14: c programs
Page 15: c programs

11.   Mr X’s basic salary is input through the keyboard. His dearness allowance is 40% of basic salary, and house rent allowance is 20% of basic salary. Write a program to calculate his gross salary

#include<stdio.h>

#include<conio.h>

main ()

{

float basic, gross, dear, rent;

printf("Enter the basic salary\t");

scanf("%f",&basic);

dear=0.4*basic;

rent=0.2*basic;

gross=basic+dear+rent;

printf("\n\nThe gross salary is\t%f",gross);

getch();

}