Ex.No.1 Console Input and Output Functions Reg.
No: URK22CS3007
26.09.2022
1. Write a C program to perform addition, subtraction, division and multiplication
of two
numbers.
Aim:
To write a program to perform addition, subtraction, division and multiplication of two numbers.
Algorithm:
Step 1: Start the Program
Step 2: Declare required integer variables num1 and num2.
Step 3: Calculate sum = num1 + num2, diff = num1 - num2
Step 4: Compute mul = num1 * num2 and div = num1 / num2
Step5: Display the results of sum, diff, mul and div.
Program
#include <stdio.h>
void main()
int num1, num2;
int sum, diff,mul,div;
printf("Enter first number: ");
scanf("%d", &num1);
printf("Enter second number: ");
scanf("%d", &num2);
sum = num1 + num2;
diff = num1 - num2;
mul = num1 * num2;
div = num1 / num2;
printf("Sum = %d\n", sum);
printf("Difference = %d\n", diff);
printf("Multiply = %d\n", mul);
printf("Division= %d\n", div);
Output
Result
The program is executed successfully. The sum, difference, multiplication and division of two
numbers are performed using input and output functions.
Ex.No.2 Console Input and Output Functions Reg.No: URK22CS3007
26.09.2022
2. Write a program to display the size of every data type using “sizeof” operator.
Aim: To calculate the memory space occupied by all primitive data types such as
integer, float, char and double.
Algorithm:
Step1 : Start the program.
Step 2: Use the “sizeof()” operator to display the size of each datatype.
Step 3: Print the Fahrenheit value.
Step 4: Stop the program.
Program:
#include<stdio.h>
int main()
int x=25;
float r=3.785;
char s= 'z';
double d=4.89999999;
printf("memory occupied by an integer variable is %dbytes\n",sizeof(x));
printf("memory occupied by an float variable is %dbytes\n",sizeof(r));
printf("memory occupied by an character variable is %dbytes\n",sizeof(s));
printf("memory occupied by an doubled variable is %dbytes\n",sizeof(d));
return 0;
~
Output:
Result: The program is executed successfully by calculating the size of primitive data types.
Ex.No.3 Console Input and Output Functions Reg.No: URK22CS3007
26.09.2022
3. Write a C program to calculate area and circumference of a circle.
Aim:
To write a program to calculate the area and circumference of a circle using the formulae:
area = PI * rad * rad
ci = 2 * PI * rad;
Algorithm:
Step 1: Start the Program
Step 2: Declare required integer variable radius
Step 3: Declare the float variables PI,area,ci
Step 3: Calculate area = PI * rad * rad
Step 4: Calculate ci = 2 * PI * rad
Step 5: Display “Area of Circle”, area.
Step 6: Display “ Circumference of Circle”, ci.
Program:
#include<stdio.h>
int main()
float pi;
pi=3.14;
int r;
float area,circumference;
printf("enter the radius:");
scanf("%d",&r);
area=pi*r*r;
circumference=2*pi*r;
printf("Area of the circle is %f\n",area);
printf("circumference of the circle is %f\n",circumference);
return 0;
Output :
Result:
The program is executed successfully. The area and circumference of a circle are
calculated.
Ex.No.4 Console Input and Output Functions Reg.No: URK22CS3007
26.09.2022
4. Write a program to swap values of two variables with and without using third variable.
Aim:
To write a program to Swap the values of two variables with and without using third variable.
Algorithm: Without using third variable
Step 1: Start the Program
Step 2: Declare required integer variables a, b
Step 3: Calculate
a=a+b
b=a-b
a=a-b
Step 4: Display the value of a and b after swapping
Step 5: Print a and b
Program:
#include<stdio.h>
int main()
int a=10, b=20;
printf("Before swap a=%d b=%d",a,b);
a=a+b;//a=30 (10+20)
b=a-b;//b=10 (30-20)
a=a-b;//a=20 (30-10)
printf("\nAfter swap a=%d b=%d",a,b);
return 0;
With using third variable
Algorithm:
Step 1: Start the Program
Step 2: Declare required integer variables a, b and temp
Step 3: Calculate
Temp=a;
a=b;
b=temp;
Step 4: Display the value of a and b after swapping
Step 5: Print a and b
Program:
#include<stdio.h>
int main()
int a,b,temp;
a=20;
b=10;
printf("%d\n",a);
printf("%d\n",b);
printf("swapped values:\n");
temp=a;
a=b;
b=temp;
printf("%d\n",a);
printf("%d\n",b);
return 0;
Output:
Result:
The program is executed successfully by swapping the given two numbers without and with using third
variable.
Ex.No.5 Console Input and Output Functions Reg.No: URK22CS3007
26.09.2022
5. Write a C program to print the student profile.
Aim:
To write a C program to print the student profile.
Algorithm:
Step 1: Start the Program
Step 2: Declare required integer variable rollnum using struct
Step 3: Declare the character variable name
Step 4: Declare the float variable mark
Step 3: Get the values from user
Step 4: Display the values of rollnum, name and marks of the student
Step 5: Print rollnum, name and mark of the student.
Program:
#include <stdio.h>
struct student
char name[50];
int roll;
float marks;
};
int main()
struct student s;
printf("Enter The Information of Students :\n\n");
printf("Enter Name : ");
scanf("%s",s.name);
printf("Enter Roll No. : ");
scanf("%d",&s.roll);
printf("Enter marks : ");
scanf("%f",&s.marks);
printf("\nDisplaying Information\n");
printf("Name: %s\n",s.name);
printf("Roll: %d\n",s.roll);
printf("Marks: %.2f\n",s.marks);
return 0;
OUTPUT
Result:
Thus the program is successfully executed and the profile of the student is displayed.
Ex.No.6 Console Input and Output Functions Reg.No: URK22CS3007
03.10.2022
6. Write a C program to read in a three digit number and produce the following output
(assuming that the input is 347 and the output is 3 hundreds 4 tens 7 units).
Aim:
To write a c program to read a three digit number and produce the output in hundreds, tens and units.
Algorithm:
Step 1: Start the Program
Step 2: Declare required integer variable rollnum using struct
Step 3: Declare the character variable name
Step 4: Declare the float variable mark
Step 3: Get the values from user
Step 4: Display the values of rollnum, name and marks of the student
Step 5: Print rollnum, name and mark of the student.
Program:
#include<stdio.h>
#include<cono.h>
int main()
int no, hun,ten,unit;
printf("enter three digit number:");
scanf("%d",&n);
hun=no/100;
no=no%100;
ten=no/10;
unit=no%10;
printf("%d hundered \n%d ten \n%d unit",hun,ten,unit);
getch();
return 0;
OUTPUT:
Result:
Thus the program is successfully executed
Ex.No.7 Console Input and Output Functions Reg.No: URK22CS3007
03.10.2022
7. Write a program to input name, marks of 5 subjects of a student and display the name of
the student, the total marks scored, percentage scored and the class of result.
Aim:
To write a C program to input the 5 subject marks of the student and display the result, total
marks scored, percentage scored by the student and the result in class.
Algorithm:
STEP1: Start
STEP2: Accept a five subject marks phy,chem,bio,math,comp
STEP3: Perform addition of 5 subject marks.
STEP4: Display addition
STEP5: Calculate the percentage per = phy+chem+bio+math+comp/5×100
STEP 6: Display the percentage per
STEP 7: Find grade according to the percentage per.
STEP 8: Stop
Program:
#include <stdio.h>
int main()
int phy, chem, bio, math, comp;
float per;
/* Input marks of five subjects from user */
printf("Enter five subjects marks: ");
scanf("%d%d%d%d%d", &phy, &chem, &bio, &math, &comp);
/* Calculate percentage */
per = (phy + chem + bio + math + comp) / 5.0*100;
printf("Percentage = %.2f\n", per);
/* Find grade according to the percentage */
if(per >= 90)
printf("Grade A");
else if(per >= 80)
printf("Grade B");
else if(per >= 70)
printf("Grade C");
else if(per >= 60)
printf("Grade D");
else if(per >= 40)
printf("Grade E");
}
else
printf("Grade F");
return 0;
OUTPUT:
Result:
The five subject marks of the students are calculated and the grade of the student is printed successfully.
Ex.No.8 Console Input and Output Functions Reg.No: URK22CS3007
03.10.2022
8. In this program, accept number of days from user and convert it into years, weeks & days
Aim:
To write a C program to get the number of days from user and convert it into years, weeks and days.
Algorithm:
STEP 1: Start
STEP 2: Get the number of days num.
STEP 3: Number of year = (number of days) / 365. Explanation-: number of years will
be the quotient obtained by dividing the given number of days with 365.
STEP 4: Number of weeks = (number of days % 365) / 7
STEP 5: Number of days = days = days- ((years*365) + (weeks*7));
STEP 6: Stop
Program:
#include <stdio.h>
void main()
int num, years, weeks, days;
printf("\nPlease Enter the Number of days : ");
scanf("%d",&num);
years = num / 365;
weeks = (num % 365) / 7;
days = (num % 365) % 7;
printf("\nYears = %d", years);
printf("\nWeeks = %d", weeks);
printf("\nDays = %d\n", days);
OUTPUT
RESULT:
The program is executed successfully by converting the number of days into years, weeks and days.
Ex.No.9 Console Input and Output Functions Reg.No: URK22CS3007
03.10.2022
9. In this program, we will accept temperature in Fahrenheit then convert it into Celsius using
following formula.
Formula = (F - 32)/1.8
Aim:
To write a C program to covert temperature in Fahrenheit into Celsius.
Algorithm:
STEP 1: Start
STEP 2: Get temperature value in Fahrenheit
STEP 3: Convert temperature in Farenheit into temperature in Celsius by using the formula
Formula=(F-32)/1.8
STEP 4: Print the temperature value in Celsius.
STEP 5: Stop
Program:
#include <stdio.h>
int main()
float celsius, fahrenheit;
/* Input temperature in fahrenheit */
printf("Enter temperature in Fahrenheit: ");
scanf("%f", &fahrenheit);
/* Fahrenheit to celsius conversion formula */
celsius = (fahrenheit - 32) /1.8;
/* Print the value of celsius */
printf("%.2f Fahrenheit = %.2f Celsius", fahrenheit, celsius);
return 0; }
OUTPUT