computer programming
computer programming
Aim: write a Program to find the sum of the first n natural numbers.
Algorithm:
In this program:
Include Library:
OUTPUT:
RESULT:
Thus,the sum of n natural numbers have been produced successfully after the execution of the
program.
EXPERIMENT-2
ALGORITHM:
Include Library:
● Include the necessary C++ library (iostream).
Declare Variables:
● Declare integer variables n1, n2, n3, i, and n.
Input:
● Get input from the user for the number of elements ( n) in the Fibonacci series.
Initialize First Two Elements:
● Set n1 to 0 and n2 to 1 as the first two elements of the series.
Output:
● Print the first two elements of the series (n1 and n2).
Generate Fibonacci Series:
● Use a loop starting from i = 2 up to n - 1 to generate the Fibonacci series.
● Calculate the next element (n3) as the sum of the previous two elements ( n1 and
n2).
● Print the calculated element.
● Update n1 and n2 for the next iteration.
End:
● Return 0 to indicate successful program execution.
CODE:
OUTPUT:
RESULT:
ALGORITHM:
Include Library:
● Include the necessary C++ library (iostream).
Declare Variable:
● Declare an integer variable year to store the user input for the year.
Input:
● Get input from the user for the value of the year.
Check Leap Year:
● Use a series of if and else if statements to check if the entered year is a leap
year or not:
● If year is divisible by 400, print that it is a leap year.
● Else if year is divisible by 100, print that it is not a leap year.
● Else if year is divisible by 4, print that it is a leap year.
● Otherwise, print that it is not a leap year.
Output:
● Print the result based on the leap year check.
End:
● Return 0 to indicate successful program execution.
CODE:-
OUTPUT:
RESULT:
Thus,we can successfully determine if the input year is a leap year or not.
EXPERIMENT-4
ALGORITHM:
CODE:
OUTPUT:
RESULT:
Thus,we got to determine if the number is prime or not prime by executing the code.
EXPERIMENT-5
AIM:Write a program to print the array elements in a two dimensional
array, and also calculate its sum and average.
ALGORITHM:
● Input: Get the number of rows and columns from the user.
● Declare and Initialize: Declare a two-dimensional array arr with the given number of
rows and columns.
● Input Elements: Get the elements of the array from the user, using nested loops to iterate
through each position.
● Output Elements: Print the elements of the array using nested loops.
● Calculate Sum and Average:
● Initialize a variable sum to 0.
● Iterate through each element of the array and add it to the sum.
● Calculate the average by dividing the sum by the total number of elements (rows *
columns).
● Output Sum and Average:
● Print the calculated sum and average.
CODE :-
OUTPUT:
RESULT:
Thus, we can determine the sum and average of two dimensional array by getting the input from
the user after successfully running the code.
EXPERIMENT-6
AIM:Write a program to check if the given number is even or odd using “go
to” statement.
ALGORITHM:
CODE :
OUTPUT:
RESULT:
Thus,by using go to statement we got to determine whether the number is odd or even.
After running the code successfully.
EXPERIMENT-7
AIM:Write a program to check whether a person is eligible to vote on the
basis of age.
ALGORITHM:
Input:
Get the user's age (age) from the input.
Check Eligibility:
● Use the if statement to check if the age is greater than or equal to 18.
● If true, go to step 3; otherwise, go to step 4.
Output Eligible:
● Print "You are eligible to vote."
Output Not Eligible:
● Use the else block to handle the case where the age is less than 18.
● Print "You are not eligible to vote yet."
End:
● Return 0 to indicate successful program execution.
CODE:
OUTPUT:
RESULT:
We can successfully run the program and check if the age entered is eligible to vote or not.
EXPERIMENT-8
AIM:Write a program to create a user defined function to calculate square
root of the number.
ALGORITHM:
Include Libraries:
● Include the necessary C++ libraries (iostream and cmath).
Declare Function:
● Declare a function calculateSquareRoot that takes a double parameter num
and returns a double.
● In the function, use the sqrt function from the cmath library to calculate the
square root of num.
● Return the result.
Main Function:
● Declare a variable number of type double.
● Input: Get a number from the user and store it in the variable number.
● Call the calculateSquareRoot function with the input number and store the
result in the variable result.
● Output: Print the square root of the input number.
End:
● Return 0 to indicate successful program execution.
CODE:
OUTPUT:
RESULT:
We can thus, calculate the square root of any given number by the user after running this code.
EXPERIMENT-9
AIM:Write a program to create a user defined function to calculate the area
and circumference of a circle.
ALGORITHM:
Include Library:
● Include the necessary C++ library (iostream).
Declare Variables:
● Declare variables for the radius, area, and circumference of the circle.
Input:
● Get the radius of the circle from the user.
Calculate Area and Circumference:
● Calculate the area using the formula area = 3.14 * radius * radius.
● Calculate the circumference using the formula circumference = 2 * 3.14 *
radius.
Output:
● Print the calculated area and circumference of the circle.
End:
● Return 0 to indicate successful program execution.
CODE:
OUTPUT:
RESULT:
We can successfully find the area and circumference of any given radius value by the user, by
this program.
EXPERIMENT-10
ALGORITHM:
Include Library:
● Include the necessary C++ library (iostream).
Declare Variables:
● Declare integer variables a, b, c, d, and s.
Input:
● Prompt the user to enter a 3-digit number and store it in variable a.
Extract Digits:
● Extract the units digit (b) by taking the remainder when dividing a by 10.
● Extract the tens digit (c) by taking the remainder when subtracting b from the last
two digits of a.
● Extract the hundreds digit (d) by dividing a by 100.
Calculate Reverse:
● Calculate the reversed number (s) by combining the digits in reverse order: s =
(b * 100) + (c) + (d).
Check Palindrome:
● Check if the reversed number s is equal to the original number -a.
● If true, print "Your number is a palindrome."
● If false, print "Your number is not a palindrome."
End:
● Return 0 to indicate successful program execution.
CODE:
OUTPUT:
RESULT:
We can easily determine if the given number is a palindrome or not with the given code.
EXPERIMENT-11
ALGORITHM:
Include Library:
● Include the necessary C++ library (iostream).
Declare Function:
● Declare a function swapNumbers that takes two integer pointers as parameters.
● Inside the function, swap the values pointed to by the pointers.
Main Function:
● Declare integer variables b and c.
● Input: Get values for b and c from the user.
● Call the swapNumbers function, passing the addresses of b and c.
● Output: Print the values of b and c after swapping.
End:
● Return 0 to indicate successful program execution.
CODE:
OUTPUT:
RESULT:
This program can successfully swap the numbers entered by the user.
EXPERIMENT-12
ALGORITHM:
Include Library:
● Include the necessary C++ library (iostream).
Declare Variables:
● Declare variables for studentName (string), rollNumber (integer), mathGrade,
scienceGrade, and englishGrade (float).
Input:
● Get input from the user for studentName, rollNumber, mathGrade,
scienceGrade, and englishGrade.
Calculate Average Grade:
● Calculate the average grade by summing up mathGrade, scienceGrade, and
englishGrade, and then dividing by 3.
Output:
● Print the header for the student report card.
● Print the student's name, roll number, individual grades, and average grade.
Check Overall Performance:
● Use conditional statements to check the average grade and print the corresponding
overall performance message:
● If average grade is >= 80, print "Overall Performance: Excellent".
● If average grade is >= 60, print "Overall Performance: Good".
● If average grade is >= 40, print "Overall Performance: Average".
● Otherwise, print "Overall Performance: Needs Improvement".
End:
● Return 0 to indicate successful program execution
CODE:
OUTPUT:
RESULT:
By this code we can successfully compile the students report card including all the important
details of the students.