Experiment 1
Question :
a. Write a program to display “Hello World”
b. Write a program to add two numbers and display their sum
c. Write a program to read the radius of a circle calculate its area and display it
d. Write a program to evaluate the arithmetic expression [(a-b)/c*d + e)*(f+9)] and
display its solution
Code :
//Name : Bobby Kumar
//Roll No : 241312004
#include <stdio.h>
#include <math.h>
int main() {
printf("Name: Bobby Kumar\nRoll No: 241312004\n");
//a. Program to print Hello World
printf("Hello World\n");
//b. Program to add two numbers and display their sum
int a, b;
printf("Enter two integers to be added: \n");
scanf("%d%d", &a, &b);
printf("%d\n", a+b);
//c. Area of Circle
printf("Enter the radius of the circle and i will tell the area:
\n");
float r;
scanf("%f", &r);
float area = 3.14*pow(r, 2);
printf("The area of circle of radius %.2f is %.2f.\n", r, area);
//d. Arithmetic Equation
//Taking input from the user and storing the values into an array
printf("Enter the numbers one by one: \n");
float arr[7];
for(int i = 0; i < 7; i++){
printf("Enter the value for variable %c: ", 97+i);
Experiment 1
scanf("%f", &arr[i]);
}
//Calculating and printing the output of the equation
printf("\nThe value is going to be calculated by this formula: \n");
printf("([(a-b)/c]d + e)(f+g)\n");
float result = (((arr[0] - arr[1])/arr[2])*arr[3] + arr[4]) *
(arr[5]+arr[6]);
printf("\nThe result is: %.2f\n\n", result);
return 0;
}
Output :
Name: Bobby Kumar
Roll No: 241312004
Hello World
Enter two integers to be added:
1
2
3
Enter the radius of the circle and i will tell the area:
5
The area of circle of radius 5.00 is 78.50.
Enter the numbers one by one:
Enter the value for variable a: 1
Enter the value for variable b: 3
Enter the value for variable c: 5
Enter the value for variable d: 7
Enter the value for variable e: 8
Enter the value for variable f: 9
Enter the value for variable g: 5
The value is going to be calculated by this formula:
([(a-b)/c]d + e)(f+g)
The result is: 72.80
Experiment 2
Question :
Write a program to check whether the entered year is leap year or not
Code :
//Name : Bobby Kumar
//Roll No : 241312004
#include <stdio.h>
int main(){
printf("Name: Bobby Kumar\nRoll No: 241312004\n");
printf("Enter a year and I will tell whether its Leap year or
not:\n");
int year;
scanf("%d", &year);
if(year % 400 == 0 && year%100 == 0){
printf("Leap Year!\n");
}
else if(year % 100 != 0 && year % 4 == 0){
printf("Leap Year!");
}
else{
printf("Not a Leap Year\n");
}
return 0;
}
Output :
Name: Bobby Kumar
Roll No: 241312004
Enter a year and I will tell whether its Leap year or not:
2004
Leap Year!
Experiment 3
Question :
Write a program to check whether the entered number is even or odd
Code :
//Name : Bobby Kumar
//Roll No : 241312004
#include <stdio.h>
int main(){
printf("Name: Bobby Kumar\nRoll No: 241312004\n");
printf("I will tel whether a given number is even or odd\nPlease
enter a number:\n");
int n;
scanf("%d", &n);
if(n % 2){
printf("ODD\n");
}
else{
printf("EVEN\n");
}
return 0;
}
Output :
Name: Bobby Kumar
Roll No: 241312004
I will tel whether a given number is even or odd
Please enter a number:
23
ODD
Experiment 4
Question :
Write a program to print the ASCII value of entered character
Code :
//Name : Bobby Kumar
//Roll No : 241312004
#include <stdio.h>
int main(){
printf("Name: Bobby Kumar\nRoll No: 241312004\n");
printf("Enter the character and i will tell tell you the ASCII code
associated with it: \n");
char c;
scanf("%c", &c);
if(c < 65 || (c > 89 && c <97) || c > 106){
printf("Enter a valid input");
return 0;
}
printf("%d\n", c);
return 0;
}
Output :
Name: Bobby Kumar
Roll No: 241312004
Enter the character and i will tell tell you the ASCII code associated
with it:
F
70
Experiment 5
Question :
Write a program to compute grade of students using if-else statements
Code :
//Name : Bobby Kumar
//Roll No : 241312004
#include <stdio.h>
int main()
{
/* Initialising */
int mark;
/* Get mark from user */
printf("Enter the first mark you want to find grade of:\n");
scanf("%d", &mark);
/* To find grade */
if (mark <= 100 && mark >= 90) {
printf("The grade of %d is A*.\n", mark);
}
else if (mark < 90 && mark >= 80) {
printf("The grade of %d is A.\n", mark);
}
else if (mark < 80 && mark >= 70) {
printf("The grade of %d is B.\n", mark);
}
else if (mark < 70 && mark >= 60) {
printf("The grade of %d is C.\n", mark);
}
else if (mark < 60 && mark >= 50) {
printf("The grade of %d is D.\n", mark);
}
else {
printf("The grade of %d is F.\n", mark);
}
return 0;
}
Output :
Enter the first mark you want to find grade of:
45
The grade of 45 is F.
Experiment 6
Question :
Write a program to print diamond pattern using ‘*’
Code :
// Name : Bobby Kumar
// Roll No : 241312004
#include <stdio.h>
void printStars(int j, int n) {
for (int i = 0; i < (n - j) / 2; i++) {
printf(" ");
}
for (int i = 0; i < j; i++) {
printf("*");
}
printf("\n");
}
int main() {
// Print name and roll number
printf("Name: Bobby Kumar\nRoll No: 241312004\n");
int n;
printf("Enter the number of rows:\n");
scanf("%d", &n);
for (int i = 0; i < n; i++) {
if (i <= n / 2) {
printStars(2 * i + 1, n);
} else {
printStars(2 * (n - i - 1) + 1, n);
}
}
return 0;
}
Output :
Name: Bobby Kumar
Roll No: 241312004
Enter the number of rows:
5
*
***
*****
***
*
Experiment 7
Question :
Write a program to detect whether entered number is prime or not.
Code :
// Name : Bobby Kumar
// Roll No : 241312004
#include <stdio.h>
int main(){
printf("Name: Bobby Kumar\nRoll No: 241312004\n");
printf("I will tell whether a given number is prime or not\nPlease
enter a number:\n");
int n;
scanf("%d", &n);
for(int i = 3; i < n/2; i+=2){
if(n%i == 0){
printf("Not A Prime\n");
return 0;
}
}
if(n%2 == 0 && n > 2 || n ==1){
printf("Not A Prime\n");
return 0;
}
printf("Prime!\n");
return 0;
}
Output :
Name: Bobby Kumar
Roll No: 241312004
I will tell whether a given number is prime or not
Please enter a number:
67
Prime!
Experiment 8
Question :
Write a program to swap two numbers with or without using third variable
Code :
// Name : Bobby Kumar
// Roll No : 241312004
#include <stdio.h>
int main(){
printf("Name: Bobby Kumar\nRoll No: 241312004\n");
printf("Swapping without third variable.\n");
int a, b;
printf("Enter the value of variables a and b.\n");
scanf("%d%d", &a, &b);
a = a+b;
b = a - b;
a = a - b;
printf("The value of a is %d.\n", a);
printf("The value of b is %d.\n", b);
}
Output :
Name: Bobby Kumar
Roll No: 241312004
Swapping without third variable.
Enter the value of variables a and b.
45 67
The value of a is 67.
The value of b is 45.
Experiment 9
Question :
Write a program to calculate the simple interest and compound interest.
Code :
// Name : Bobby Kumar
// Roll No : 241312004
#include <stdio.h>
int main(){
printf("Name: Bobby Kumar\nRoll No: 241312004\n");
while(1){
int option;
printf("Enter 1 for Simple Interest Program\nEnter 2 for
Compound Interest Program:\n");
scanf("%d", &option);
int principal, rate, years;
printf("Enter the principal amount:\n");
scanf("%d", &principal);
printf("Enter rate of interest:\n");
scanf("%d", &rate);
printf("Enter the number of years loan is taken:\n");
scanf("%d", &years);
switch(option){
case 1:
printf("Your simple interest is %d.\n",
principal*rate*years/100);
return 0;
case 2:
printf("Enter the no of times loan is compounded each
year:\n");
int n;
scanf("%d", &n);
printf("Your compound interest is %d.\n",
principal*(1+rate/n)^(n*years));
return 0;
default:
//Invalid input loop the code back
printf("Enter valid option_/\\_\n\n");
}
}
return 0;
}
Output :
Name: Bobby Kumar
Roll No: 241312004
Enter 1 for Simple Interest Program
Enter 2 for Compound Interest Program:
2
Enter the principal amount:
23
Enter rate of interest:
1
Enter the number of years loan is taken:
34
Enter the no of times loan is compounded each year:
4
Your compound interest is 159.
Experiment 10
Question :
Write a program to find roots of a quadratic equation
Code :
// Name : Bobby Kumar
// Roll No : 241312004
#include <stdio.h>
#include <math.h>
int main(){
printf("Name: Bobby Kumar\nRoll No: 241312004\n");
printf("A quadratic equation is defined ax^2+bx+c and I need you to
enter the coeffecients:\n");
float a, b, c;
scanf("%f%f%f", &a, &b, &c);
float first = (-b + sqrt(pow(b,2) - 4 * a *c))/(2*a);
float second = (-b - sqrt(pow(b,2) - 4 * a *c))/(2*a);
printf("The first root is %.2f.\n", first);
printf("The second root is %.2f.\n", second);
return 0;
}
Output :
Name: Bobby Kumar
Roll No: 241312004
A quadratic equation is defined ax^2+bx+c and I need you to enter the
coeffecients:
3 21 1
The first root is -0.05.
The second root is -6.95.
Experiment 11
Question :
Write a program to find largest of three elements with or without using turnary operator
Code :
// Name : Bobby Kumar
// Roll No : 241312004
#include <stdio.h>
int main(){
printf("Name: Bobby Kumar\nRoll No: 241312004\n");
printf("Enter a three numbers and i will tell largest of them all:
\n");
int a, b, c;
scanf("%d%d%d", &a, &b, &c);
if(a > b && a > c){
printf("Largest is %d!\n", a);
}
else if(b >= a && b >= c){
printf("Largest is %d!\n", b);
}
else{
printf("Largest is %d!\n", c);
}
return 0;
}
Output:
Name: Bobby Kumar
Roll No: 241312004
Enter a three numbers and i will tell largest of them all:
-23 45 123
Largest is 123!
Experiment 12
Question :
Write a program to enter marks of 5 subjects of student and display total marks score,
percentage scored and display the result.
Code :
// Name : Bobby Kumar
// Roll No : 241312004
#include <stdio.h>
int main() {
printf("Name: Bobby Kumar\nRoll No: 241312004\n");
float marks[5];
float sum = 0, percentage;
int i, j, k;
for (i = 0; i < 5; i++) {
printf("Enter the mark %d: ", i + 1);
scanf("%f", &marks[i]);
}
for (j = 0; j < 5; j++) {
sum = sum + marks[j];
}
for (k = 0; k < 5; k++) {
printf("\nThe mark %d of the student: %.2f\n", k + 1, marks[k]);
}
printf("\nThe sum of marks that the student got is %.2f\n\n", sum);
percentage = (sum / 5);
printf("The percentage scored by the student is %.2f\n",
percentage);
return 0;
}
Output :
Name: Bobby Kumar
Roll No: 241312004
Enter the mark 1: 78
Enter the mark 2: 69
Enter the mark 3: 89
Enter the mark 4: 90
Enter the mark 5: 77
The mark 1 of the student: 78.00
The mark 2 of the student: 69.00
The mark 3 of the student: 89.00
The mark 4 of the student: 90.00
The mark 5 of the student: 77.00
The sum of marks that the student got is 403.00
The percentage scored by the student is 80.60
Experiment 13
Question :
Write a program to detect whether the entered character is vowel, number or consonant.
Code :
// Name : Bobby Kumar
// Roll No : 241312004
#include <stdio.h>
int main() {
printf("Name: Bobby Kumar\nRoll No: 241312004\n");
char c;
printf("Enter a character:\n");
scanf("%c", &c);
if(c >= 97 && c < 123){
printf("A small alphabetical character\n");
}
else if(c >= 65 && c < 91){
printf("A Capital alphabetical character\n");
}
else if(c >= 49 && c < 59){
printf("An Integer\n");
}
else{
printf("A special character\n");
}
}
Output :
Name: Bobby Kumar
Roll No: 241312004
Enter a character:
4
An Integer
Experiment 14
Question :
Write a program to determine whether entered number is armstrong or not
Code :
// Name : Bobby Kumar
// Roll No : 241312004
#include <stdio.h>
#include <math.h>
int main() {
printf("Name: Bobby Kumar\nRoll No: 241312004\n");
int n;
printf("Enter your number: \n");
scanf("%d", &n);
printf("\n");
int numberOfDigits = 1;
//this loop detects the number of terms in the given integer
int quotient = 1;
while(1){
quotient = n / pow(10,numberOfDigits);
if(quotient != 0){
numberOfDigits++;
// printf("%d\n", quotient);
}
else{
break;
}
}
int m = n;
int sum;
for(int i = 1; i <= numberOfDigits; i++){
int quotient2 = m / pow(10,i);
int remainder = m % (int)pow(10,i);
m -= remainder;
int term = remainder / pow(10,i-1);
sum += pow(term, numberOfDigits);
}
if(sum == n){
printf("Armstrong!\n");
}
else{
printf("Not Armstrong T_T");
}
return 0;
}
Output :
Name: Bobby Kumar
Roll No: 241312004
Enter your number:
153
Armstrong!
Experiment 15
Question :
Write a program to find factorial of a given number using recursive or non-recursive method
Code :
// Name : Bobby Kumar
// Roll No : 241312004
#include <stdio.h>
int facRec(int n);
int facNonRec(int n);
int main(){
printf("Name: Bobby Kumar\nRoll No: 241312004\n");
int n;
printf("Enter the number you want factorial of:\n");
scanf("%d", &n);
printf("This is the factorial of number %d using recursive method :
%d\n", n, facRec(n));
printf("This is the factorial of number %d using non-recursive
method : %d\n", n, facNonRec(n));
return 0;
}
int facRec(int n){
if(n <= 1){
return 1;
}
return n*facRec(n-1);
}
int facNonRec(int n){
int factorial = 1;
for(int i = 1; i <= n; i++){
factorial *= i;
}
return factorial;
}
Output :
Name: Bobby Kumar
Roll No: 241312004
Enter the number you want factorial of:
12
This is the factorial of number 12 using recursive method : 479001600
This is the factorial of number 12 using non-recursive method :
479001600