0% found this document useful (0 votes)
7 views8 pages

Basic C Program

basic programming

Uploaded by

gyanendra gautam
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)
7 views8 pages

Basic C Program

basic programming

Uploaded by

gyanendra gautam
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

Basic C Program

1. C Hello World Program. Explanation:

#include <stdio.h>  #include <stdio.h> – This line includes the


int main() standard input-output library in the program.
{
 int main() – The main function where the
printf("Hello World");
return 0; execution of the program begins.
}  printf(“Hello, World!\n”); – This function call

Output:- prints “Hello, World!” followed by a new line.


 return 0; -This statement indicates that the
Hello World
program ended successfully.
2. C Program to Print Your Own Name.
#include <stdio.h>

int main()
{
char name[100]; // Defining string (character array) assuming 100 characters at max
printf("Enter Your Name: "); // Taking input from the user
scanf("%s", name);
printf("Your Name: %s\n", name); // Printing your name to the screen (puts(“VIKAS”))
return 0;
}
Output
Enter Your Name: Rahul
Your Name: Rahul

3. C Program to Print an Integer Entered By the User.


#include <stdio.h>
int main()
{
int num; // Declare the variables
printf("Enter the integer: "); // Input the integer
scanf("%d", &num);
printf("Entered integer is: %d", num); // Display the integer
return 0;
}

Output:
Enter the integer: 10
Entered integer is: 10
4. C Program to Add Two Numbers.
#include <stdio.h>
int main()
{
int a, b, sum = 0;
printf("Enter the value of a: ");
scanf("%d", &a);
printf("Enter the value of b: ");
scanf("%d", &b);
sum = a + b;
printf("Sum: %d\n", sum);
return 0;
}
Output
Enter the value of a: 2
Enter the value of b: 3
Sum: 5

5. C Program to Multiply two Floating-Point Numbers.


#include <stdio.h>
int main()
{
float num1, num2, product;
// Input two floating-point numbers
printf("Enter first number: ");
scanf("%f", &num1);
printf("Enter second number: ");
scanf("%f", &num2);
// Multiply the numbers
product = num1 * num2;
// Display the result
printf("Product = %.2f\n", product);
return 0;
}
Output
Enter first number: 25.53
Enter second number: 3.25
Product = 82.97
6. C Program to Print the ASCII Value of a Character.
#include <stdio.h>
int main()
{
char ch;
// Input a character
printf("Enter a character: "); Explanation:
scanf("%c", &ch); // %d displays the integer value of a character
// Print the ASCII value // %c displays the actual character
printf("ASCII value of '%c' = %d\n", ch,
ch);
return 0;
}
Output
Enter a character: h
ASCII value of 'h' = 104

7. C Program to Swap Two Numbers.

Using third variable Without using third variable


#include <stdio.h> #include <stdio.h>
int main() int main()
{ {
int a, b, temp; int a, b;
printf(“Enter first number:”); printf(“Enter first number:”);
scanf(“%d” , &a); scanf(“%d” , &a);
printf(“\nEnter second number:”); printf(“\nEnter second number:”);
scanf(“%d”, &b); scanf(“%d”, &b);
// Swap logic // Swap logic
temp = a; a = a + b;
a = b; b = a - b;
b = temp; a = a - b;
printf(“\nAfter swapping: a = %d”, a); printf(“\nAfter swapping: a = %d”, a);
printf(“\nAfter swapping: b = %d”, b ); printf(“\nAfter swapping: b = %d”, b );
return 0; return 0;
} }

Output

Enter two numbers: 10 20


After swapping: a = 20, b = 10
8. C Program to Convert Fahrenheit to Celsius.

#include <stdio.h>
int main()
{
float fahrenheit, celsius;
printf("Enter temperature in Fahrenheit: ");
scanf("%f", &fahrenheit);
celsius = (fahrenheit - 32) * 5 / 9;

printf("Temperature in Celsius = %.2f\n", celsius);

return 0;
}

Output

Enter temperature in Fahrenheit: 98.6


Temperature in Celsius = 37.00

9. C Program to Find Simple Interest.

#include <stdio.h>
int main()
{
float p, r, t, si;
printf("Enter principal: ");
scanf("%f", &p);
printf("Enter rate of interest: ");
scanf("%f", &r);
printf("Enter time (in years): ");
scanf("%f", &t);
si = (p * r * t) / 100; // Calculate simple interest
printf("Simple Interest = %.2f\n", si);
return 0;
}

Output

Enter principal: 1000


Enter rate of interest: 5
Enter time (in years): 2
Simple Interest = 100.00
10. C Program to Find Compound Interest

#include <stdio.h>
#include <math.h>

int main()
{
float p, r, t, ci;

printf("Enter principal, rate and time: ");


scanf("%f %f %f", &p, &r, &t);

ci = p * (pow((1 + r / 100), t)) - p;

printf("Compound Interest = %.2f\n", ci);

return 0;
}

Output

Enter principal, rate and time: 2000 10 2


Compound Interest = 420.00

11. C Program for Area and Perimeter of Rectangle

#include <stdio.h>

int main()
{
float length, breadth, area, perimeter;

printf("Enter length and breadth of rectangle: ");


scanf("%f %f", &length, &breadth);

area = length * breadth;


perimeter = 2 * (length + breadth);

printf("Area = %.2f\n", area);


printf("Perimeter = %.2f\n", perimeter);

return 0;
}

Output

Enter length and breadth of rectangle: 5 3


Area = 15.00
Perimeter = 16.00
12. C Program: Area of Circle.

Method -1 Method -2
#include <stdio.h> #include <stdio.h>
#define PI 3.1416 int main()
{
int main() float radius, area;
{ printf("Enter the radius of the circle: ");
float radius, area; scanf("%f", &radius);
printf("Enter the radius of the circle: "); area = 3.14* radius * radius;
scanf("%f", &radius); printf("Area of Circle = %.2f\n", area);
area = PI * radius * radius; return 0;
printf("Area of Circle = %.2f\n", area); }
return 0;
}

Method -3
#include <stdio.h>
#include <math.h> // for M_PI

int main()
{
float radius, area;

// Input radius
printf("Enter the radius of the circle: "); Output
scanf("%f", &radius); Enter the radius of the circle: 5
Area of Circle = 78.50
// Calculate area
area = M_PI * radius * radius; // use math.h
constant

// Display result
printf("Area of Circle = %.2f\n", area);

return 0;
}
13. Write a program in C to calculate the Gross Salary of an employee based on the following
allowances and the basic salary:

 Basic Salary: The starting salary of the employee, Enter by the user.
 HRA (House Rent Allowance): 20% of the basic salary.
 DA (Dearness Allowance): 80% of the basic salary.
 Other Allowances (OA): 25% of the basic salary.

#include <stdio.h>
int main() {
float basic, hra, da, oa, gross;
printf("Enter Basic Salary: ");
scanf("%f", &basic); // Input basic salary
// Calculating allowances
hra = 0.20 * basic; // 20% of basic
da = 0.80 * basic; // 80% of basic
oa = 0.25 * basic; // 25% of basic
// Gross salary calculation
gross = basic + hra + da + oa;
// Display result
printf("\nBasic Salary : %.2f", basic);
printf("\nHRA (20%%) : %.2f", hra);
printf("\nDA (80%%) : %.2f", da);
printf("\nOther Allowance(25%%): %.2f", oa);
printf("\n---------------------------------");
printf("\nGross Salary = %.2f\n", gross);

return 0; Output
} Enter Basic Salary: 10000

Basic Salary : 10000.00


HRA (20%) : 2000.00
DA (80%) : 8000.00
Other Allowance(25%): 2500.00
---------------------------------
Gross Salary = 22500.00
14. Write a C program to calculate the Gross Salary of an employee.
The program should take the Basic Salary and the percentages of HRA (House Rent
Allowance), DA (Dearness Allowance), and Other Allowance as input at runtime.

#include <stdio.h>
int main()
{
float basic; // Basic Salary
float hra_per, da_per, oa_per; // Percentages for allowances
float hra, da, oa, gross; // Allowances and Gross Salary
// Step 1: Input Basic Salary
printf("Enter Basic Salary of the Employee: ");
scanf("%f", &basic);
// Step 2: Input Percentages
printf("Enter HRA percentage: ");
scanf("%f", &hra_per);
Program Output
printf("Enter DA percentage: ");
scanf("%f", &da_per); Enter Basic Salary of the Employee: 10000
printf("Enter Other Allowance percentage: "); Enter HRA percentage: 20
Enter DA percentage: 80
scanf("%f", &oa_per); Enter Other Allowance percentage: 25
// Step 3: Calculate Allowances
hra = (hra_per / 100) * basic; ------ Salary Breakdown ------
da = (da_per / 100) * basic; Basic Salary : 10000.00
HRA (20.00%) : 2000.00
oa = (oa_per / 100) * basic; DA (80.00%) : 8000.00
// Step 4: Calculate Gross Salary Other Allowance (25.00%): 2500.00
gross = basic + hra + da + oa; -------------------------------
// Step 5: Display Results Gross Salary = 22500.00
printf("\n------ Salary Breakdown ------\n");
printf("Basic Salary : %.2f\n", basic);
printf("HRA (%.2f%%) : %.2f\n", hra_per, hra);
printf("DA (%.2f%%) : %.2f\n", da_per, da);
printf("Other Allowance (%.2f%%): %.2f\n", oa_per, oa);
printf("-------------------------------\n");
printf("Gross Salary = %.2f\n", gross);

return 0;
}

You might also like