0% found this document useful (0 votes)
12 views11 pages

To Display "Hello World"

The document provides a series of C programming examples demonstrating various basic functionalities such as displaying text, performing arithmetic operations, calculating areas and circumferences, handling user input, and applying bonuses based on salary conditions. Each example includes code snippets that illustrate the implementation of these functionalities. Additionally, notes are provided for certain examples to highlight best practices and potential issues.
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)
12 views11 pages

To Display "Hello World"

The document provides a series of C programming examples demonstrating various basic functionalities such as displaying text, performing arithmetic operations, calculating areas and circumferences, handling user input, and applying bonuses based on salary conditions. Each example includes code snippets that illustrate the implementation of these functionalities. Additionally, notes are provided for certain examples to highlight best practices and potential issues.
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
You are on page 1/ 11

1. To display “Hello World”.

#include <stdio.h>
#include <conio.h>
void main()
{
printf("\nHello World");
getch();
}

2. To find out the sum of two numbers.


#include <stdio.h>
#include <conio.h>
void main()
{
int a, b, c;
a = 50;
b = 100;
c = a + b;
printf("\nThe sum is: %d", c);
getch();
}

3. To find the area of a circle.


#include <stdio.h>
#include <conio.h>
void main()
{
float r, p, a;
r = 25.5;
p = 22/7;
a = p * r * r;
printf("\nThe area is: %f", a);
getch();
}
4. To find the circumference of a circle.
#include <stdio.h>
#include <conio.h>
void main()
{
float r, p, pe;
r = 25.5;
p = 22/7;
pe = 2 * p * r;
printf("\nThe circumference is: %f", pe);
getch();
}

5. To find the total and average marks of a student in three subjects.


#include <stdio.h>
#include <conio.h>
void main()
{
int s1, s2, s3, t;
float avg;
s1 = 98;
s2 = 100;
s3 = 99;
t = s1 + s2 + s3;
avg = t/3
printf("\nThe total mark is: %d", t);
printf("\nThe average mark is: %f", avg);
getch();
}
6. To swap a number.
#include <stdio.h>
#include <conio.h>
void main()
{
int a, b;
a = 8;
b = 4;
printf("Before Swap: ");
printf("a = %d, b = %d", a, b);
a = a + b;
b = a - b;
a = a - b;
printf("\nAfter Swap: ");
printf("a = %d, b = %d", a, b);
getch();
}

7. To show whether a student is pass or fail in an exam.


#include <stdio.h>
#include <conio.h>
void main()
{
float s1, s2, s3, s4, s5, total, avg, percentage;
s1 = 95;
s2 = 96;
s3 = 98;
s4 = 83;
s5 = 100;
total = s1 + s2 + s3 + s4 + s5;
percentage = (total/500)*100;
if (percentage < 33)
{
printf("Fail");
}
else
{
printf("Pass");
}
getch();
}
8. To find out the given number is positive or negative.
#include <stdio.h>
#include <conio.h>
void main()
{
int n;
n = 12;
if (n >= 0)
{
printf("Positive");
}
else
{
printf("Negative");
}
getch();
}
OR
#include <stdio.h>
#include <conio.h>
void main()
{
int n = 12;
(n >= 0)? printf("Positive"): printf("Negative");
getch();
}

9. To read the name and gender of a person from the user as an input and display the
respective details.
#include <stdio.h>
#include <conio.h>
void main()
{
char name[100], g;
printf("Enter the name of the person: ");
fgets(name, sizeof(name), stdin);
printf("Enter the gender (M/F): ");
scanf("%c", &g);
printf("\nPerson Details");
printf("\nName: %s", name);
printf("Gender: %c", g);
getch();
}
OR
#include <stdio.h>
#include <conio.h>
void main()
{
char name[100], g;
printf("Enter the name of the person: ");
fgets(name, sizeof(name), stdin);
printf("Enter the gender (M/F): ");
g = getchar();
printf("\nPerson Details\n");
puts(name);
putchar(g);
getch();
}
OR
#include <stdio.h>
#include <conio.h>
#include <string.h>
void remove_newline(char*str)
{
size_t len = strlen(str);
if (len > 0 && str[len - 1] == '\n')
{
str[len - 1] = '\0';
}
}
void main()
{
char name[100], g;
printf("Enter the name of the person: ");
fgets(name, sizeof(name), stdin);
remove_newline(name);
printf("Enter the gender (M/F): ");
g = getchar();
printf("\nPerson Details\n");
puts(name);
putchar(g);
}
Note1: For question 9, in line 7 of the three programs, the statement:
fgets(var, sizeof(var), stdin);
is safer than the statement: gets(var);. The gets() is not safer to store and read a
string.
Note2: For question 9, the second program gives an odd output with unnecessary
newlines. Hence, it is safer and even to go with first or third program of question 9.
Note3: For question 9, in the third program the following statements:
void remove_newline(char*str)
{
size_t len = strlen(str);
if (len > 0 && str[len - 1] == '\n')
{
str[len - 1] = '\0';
}
}
generally used to remove unnecessary newlines and hence in line 17 of third program we
can see it has been called with variable name in its argument. Thus, the string inputs
which is done with fgets() generally need the group of above statements for
uniformity.
Note4: Question 10 also does the same thing already done in third program of question 9.
10. To read the name, course and city from an user of a person and display it.
#include <stdio.h>
#include <conio.h>
#include <string.h>
void remove_newline(char*str)
{
size_t len = strlen(str);
if (len > 0 && str[len - 1] == '\n')
{
str[len - 1] = '\0';
}
}
void main()
{
char name[100], course[50], city[75];
printf("Enter the name of the person: ");
fgets(name, sizeof(name), stdin);
remove_newline(name);
printf("Enter the course studied by %s: ", name);
fgets(course, sizeof(course), stdin);
remove_newline(course);
printf("Enter the city to which %s belongs: ", name);
fgets(city, sizeof(city), stdin);
remove_newline(city);
printf("--------Person Details--------");
printf("\nName: %s", name);
printf("\nCourse: %s", course);
printf("\nCity: %s", city);
getch();
}
11. To read the name, age and salary from an user of a person and display it.
#include <stdio.h>
#include <conio.h>
#include <string.h>
void remove_newline(char*str)
{
size_t len = strlen(str);
if (len > 0 && str[len - 1] == '\n')
{
str[len - 1] = '\0';
}
}

void main()
{
char name[100];
int age, salary;
printf("Enter the name of the person: ");
fgets(name, sizeof(name), stdin);
remove_newline(name);
printf("Enter the age of %s: ", name);
scanf("%d", &age);
printf("Enter the salary of %s: ", name);
scanf("%d", &salary);
printf("--------Person Details--------");
printf("\nName: %s", name);
printf("\nAge: %d", age);
printf("\nSalary: %d", salary);
getch();
}
12. To read the name and salary of an employee and apply bonus if the basic salary is less
than Rs.20000.
#include <stdio.h>
#include <conio.h>
#include <string.h>
void remove_newline(char*str)
{
size_t len = strlen(str);
if (len > 0 && str[len - 1] == '\n')
{
str[len - 1] = '\0';
}
}
void main()
{
char name[100];
int salary;
printf("Enter the name of the employee: ");
fgets(name, sizeof(name), stdin);
remove_newline(name);
printf("Enter the salary of %s: ", name);
scanf("%d", &salary);
printf("Salary of %s: ", name);
printf("%d", salary);
if (salary < 20000)
{
salary = salary + 10000;
printf("\nRs.10000 Bonus Applied!!");
}
else
{
printf("\nNo Bonus Applied!!");
}
printf("\n--------Updated Details of %s--------", name);
printf("\nName: %s", name);
printf("\nSalary: %d", salary);
getch();
}
Note5: In question 12, in line 25 instead of salary = salary + 10000 it can also
be given as salary += 10000 as that in python.
13. To read the name of a student and his five subject marks along with display his/her total
and average marks.
#include <stdio.h>
#include <conio.h>
#include <string.h>
void remove_newline(char*str)
{
size_t len = strlen(str);
if (len > 0 && str[len - 1] == '\n')
{
str[len - 1] = '\0';
}
}
void main()
{
char name[100];
int phy, chem, mat, eng, cs, total;
float avg;
printf("Enter the name of the student: %s", name);
fgets(name, sizeof(name), stdin);
remove_newline(name);
printf("Enter the mark of %s in Physics: ", name);
scanf("%d", &phy);
printf("Enter the mark of %s in Chemistry: ", name);
scanf("%d", &chem);
printf("Enter the mark of %s in Mathematics: ", name);
scanf("%d", &mat);
printf("Enter the mark of %s in English: ", name);
scanf("%d", &eng);
printf("Enter the mark of %s in CS: ", name);
scanf("%d", &cs);
total = phy + chem + mat + eng + cs;
avg = total/5;
printf("Total mark scored by %s: ", name);
printf("%d", total);
printf("\nAverege of %s: ", name);
printf("%f", avg);
getch();
}
14. To read the name of an employee and his/her salary and apply bonus of Rs.10000 is
salary is less than Rs.20000, else apply bonus of Rs.5000.
#include <stdio.h>
#include <conio.h>
#include <string.h>
void remove_newline(char*str)
{
size_t len = strlen(str);
if (len > 0 && str[len - 1] == '\n')
{
str[len - 1] = '\0';
}
}
void main()
{
char name[100];
int salary;
printf("Enter the name of the employee: %s", name);
fgets(name, sizeof(name), stdin);
remove_newline(name);
printf("Enter the salary of %s: ", name);
scanf("%d", &salary);
if (salary < 20000)
{
salary += 10000;
printf("Rs.10000 Bonus Applied for %s", name);
}
else
{
salary += 5000;
printf("Rs.5000 Bonus Applied for %s", name);
}
printf("\n--------Updated Details of %s--------", name);
printf("\nName: %s", name);
printf("\nSalary: %d", salary);
getch();
}

You might also like