C Program Assistant

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 10

1)Write C program to read 10 numbers from the keyboard and find their sum & average

---------------------------------------------------------

Test Data

Input 10 numbers from the keyboard and calculate their sum & average.

---------------------------------------------------------

Code:

Copy code

#include <stdio.h>

intmain()

sum =0,;

float average;

achievement

printf("Enter 10 numbers:");

"For(int i=1;i<=10;i++)

printf("Number-%d: ",i);
scanf("%d", &num);

sum+=num;

//

average=(float)sum/10;

-------

printf("The sum of 10 numbers is: %d

",sum);

printf("The average is: %.6f

typeid(" ");

"

"

return 0;

Explanation:
Header:

#include

#include

Definitions:

#define num int n; Using for this purpose will avoid repetition and lengthy codes.

#define sum int s; It stores the sum of the entered number

#define average int av; It is the average, this is because it's computation is based on the sum

Ask user: This program will prompt the user to enter 10 numbers

Read and Sum numbers:

A for loop which iterates 10 times; reads in a number entered, then adds it to sum.

Calculates average by dividing sum by 10.

Print calculated sum and average to the console

Example Output

Paste code

Input 10 numbers:

Number-1: 2
Number-2: 3

Number-10: 2

Sum of 10 numbers is: 55

Average is: 5.500000

2)C

#include <stdio.h>

int main() {

float basic_salary, dearness_allowance, house_rent_allowance, gross_salary;

printf("Enter the basic salary: ");

scanf("%f", &basic_salary);
dearness_allowance

= 0.40 * basic_salary;

house_rent_allowance = 0.20 * basic_salary;

gross_salary = basic_salary + dearness_allowance + house_rent_allowance;

printf("Gross

salary: %.2f

", gross_salary);

return 0;

Explanation:
Include header: It includes the stdio.h header file for standard input-output operations.

Declare variables:

basic_salary: It stores the basic salary taken as input from the user.

dearness_allowance : It calculates and stores the dearness allowance

house_rent_allowance : Calculates and stores the house rent allowance

gross_salary: Stores the calculated gross salary

Prompt user : The program prompts the user to input his basic salary .

Compute allowances :

The dearer allowance is computed as 40% of the basic salary .

The house rent allowance is computed as 20 percent of the basic salary.

Calculating gross salary: Gross salary = Basic salary + dearness allowance + house rent allowance.

Print output: The calculated gross salary is printed to the console.

Output:

Input basic salary: 20000


Gross salary: 28000.00

Exercise:

Assignment: Make above program with following slabs for calculation of income tax:

Income Range: Income Tax

Up to 2.5 lakhs : Nil

2.5 lakhs to 5 lakhs: 5%

5 lakhs to 7.5 lakhs: 10%

7.5 lakhs to 10 lakhs: 15%

Above 10 lakhs: 20%

As Different standard deduction of 50,000 rupees.

Hint: There may use of conditional statements like if-else or switch-case for tax slab deduction on the
basis of gross salary.
3)C

#include <stdio.h>

int main () {

float cost_price, selling_price, profit_loss;

printf("Enter the cost price: ");

scanf("%f",&cost_price);

printf("Enter the selling price: ");

scanf("%f",&selling_price);

printf("

END

if(selling_price >= " < "endemic_value<<<<

Header : stdio.h is the library that is present in the program to handle standard input and output
functions.
Variable Declaration:

cost_price: It is used for containing the cost price entered by the user.

Selling_price: It is used for containing the selling price entered by the user

profit_loss: It is used to store profit or loss calculated.

User Input :Program looks specifically for the user input of Cost price and Selling price .

Profit loss Calculation

If Else statements check whether selling price is more, less or equal to cost price.

If Selling Price is more, then difference is calculated as Profit

If selling Price is low, then difference is calculated as Loss

If selling Price is same, then neither profit nor loss

Console Output: The profit or loss is computed in the console.

Input:

Enter cost price: 100

Enter Selling Price: 120


Profit: 20.00

Task:

Task : Change the above code to calculate percentage of profit or percentage of loss from the cost price

Hint: You can use the following the percentage formula

percentage = (profit_loss / cost_price) * 100

You might also like