To Display "Hello World"
To Display "Hello World"
#include <stdio.h>
#include <conio.h>
void main()
{
printf("\nHello World");
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();
}