[go: up one dir, main page]

0% found this document useful (0 votes)
10 views25 pages

CP Lab Manual, Exp 1 To 7, May 12

The document outlines various programming concepts and algorithms, including input/output operations, conditional statements, loops, arrays, string operations, and function calls. Each section provides an aim, algorithm, and result indicating successful execution of the respective program. The document serves as a comprehensive guide for implementing basic programming tasks in a structured manner.

Uploaded by

Srinithi GL
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)
10 views25 pages

CP Lab Manual, Exp 1 To 7, May 12

The document outlines various programming concepts and algorithms, including input/output operations, conditional statements, loops, arrays, string operations, and function calls. Each section provides an aim, algorithm, and result indicating successful execution of the respective program. The document serves as a comprehensive guide for implementing basic programming tasks in a structured manner.

Uploaded by

Srinithi GL
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/ 25

1.

I/O Statements, Operators, Expressions

Aim: Get input of 2 numbers from the user, and perform simple arithme c
opera ons on them

Algorithm:
Step 1: Start
Step 2: Declare 2 variables of type int
Step 3. Get input of 2 numbers from the user.
Step 4. Perform various arithme c opera ons on those numbers.
Step 5. Display the result.
Step 6. End.

Program:

1
output :

Result: The program to get input of numbers and perform arithme c opera ons has been
successfully executed.

2
2 A) If-else condi on
Aim: Determine if a given number if even or odd.

Algorithm:
Step 1. Start.
Step 2. Get input of an int number from the user.
Step 3. Compute the remainder when the number is divided by 2.
Step 4. If the remainder is 0, the number is even; else, the number is odd.
Step 5. End.

Program:

Output:

Result: The program to determine if a number is even or odd has been successfully
executed.

3
2 B} switch-case

Aim: Perform simple arithme c opera ons based on user choice, using switch-case.
Algorithm:
Step 1. Start.
Step 2. Get input of 2 int values from the user.
Step 3, Get input from the user of what they want to do: addi on or subtrac on or mul plica on.
Step 4. Using switch-case, perform the appropriate opera on based on user choice. Display the
result.
Step 5. End.

Program:

4
Output:

5
Result: The program to perform arithme c opera ons based on user choice using switch-case has
been successfully executed.

6
3 A) for loop
Aim: Display numbers from 1 to n using for loop.
Algorithm:
Step 1. Start.
Step 2. Get input of n from the user.
Step 3. Run a for loop n mes, with loop counter going from 1 to n.
Step 4. In each itera on of the loop, print a number.
Step 5. End.

Program:

Output:

Result:
The program to print numbers using for loop has been successfully executed.

7
3 B) con nue statement

Aim: Display numbers from 1 to 10 skipping one specific number using con nue
statement.

Algorithm:
Step 1. Start.
Step 2, Get input from user of which number to skip.
Step 3. Run for loop with loop counter going from 1 to 10.
Step 4.1. Within the for loop, if loop counter equals the number entered in Step 2, then skip
that itera on (using con nue statement).
Step 4.2. Else, print the number (loop counter)
Step 5. End.

Program:

Output:

8
Result: The program to illustrate con nue statement has been successfully executed.

9
3 C) break statement
Aim: Display numbers, and exit out of loop using break statement
Algorithm:
Step 1 Start.
Step 2. Get input of number from user (the number on which to terminate the loop).
Step 3 Run for loop with loop counter going from 1 to 10.
Step 4.1. Within the loop, if loop counter equals the number entered in Step 2, break out of
the loop.
Step 4.2. Else, print a number.
Step 5. End.

Program:

Output:

Result: The program to illustrate break statement has been successfully executed.

10
3 D) while loop
Aim: Display even numbers from 0 to n using while loop.

Algorithm:
Step 1. Start.
Step 2. Get input of n from user.
Step 3. Ini alise loop counter to zero.
Step 4. Run while loop ll loop counter <= n.
Step 5. In each itera on of the loop, display the loop counter, and increment it by 2.
Step 6. End.

Program:

Output:

Result: Program to print even numbers using while loop has been successfully executed.

11
3 E) do-while loop
Aim: Display odd numbers from 1 to n using do-while loop.
Algorithm:
Step 1. Start.
Step 2. Get input of n from user.
Step 3. Ini alise loop counter to 1.
Step 4. Run do-while loop ll loop counter <= n.
Step 5. In each itera on of the loop, print the loop counter, and increment it by 2.
Step 6. End.

Program:

Output:

Result: Program to display odd numbers using do-while loop has been successfully
executed.

12
4 A) 1D array

Aim: Get input of elements for 1D array, and display the array contents.

Algorithm:
Step 1. Start.
Step 2. Declare an int array.
Step 3. Get input from user of how many elements are there in the array.
Step 4. Run a loop and get input of all elements.
Step 5. Run loop and display the array contents.
Step 6. End.

Program:

13
Output:

Result: Program to illustrate working with 1D array has been successfully executed.

14
4 B} 2D array

Aim: Get input of elements of 2D array, and display the array contents.

Algorithm:
Step 1, Start.
Step 2. Declare a 2D int array.
Step 3. Get input of number of rows and columns in the array from the user.
Step 4. Run nested for loops and get input of the array elements from the user.
Step 5. Again run nested for loops and display the array contents.
Step 6. End.

Program:

15
Output:

Result: The program to illustrate working with 2D arrays has been successfully executed.

16
5 A) String opera ons

Aim: Calculate the length of a string, and copy one string to another.
Algorithm:
Step 1. Include header file string.h for performing string opera ons.
Step 2. Declare 2 strings s1 and s2.
Step 3. Get input of s1 from user.
Step 4. Compute length of s1 using strlen() func on. Display the result.
Step 5. Copy s1 into s2 using strcpy() func on. Display string s2.
Step 6. End.

Program:

Output:

Result: Program to compute length of string and copy one string to another has been
successfully executed.

17
6 B) String opera ons, con nued.

Aim: Join two strings, and compare two strings.

Algorithm:
Step 1. Start.
Step 2. Include header file string.h for performing string opera ons.
Step 3. Declare 2 strings s1 and s2.
Step 4. Get input of both strings from the user.
Step 5. Compare the 2 strings using strcmp() func on, and display the result.
Step 6. Join the 2 strings using strcat() func on, and display the result.
Step 7. End.

Program:

18
Output:

Result: The program to compare 2 strings and join 2 strings has been successfully
executed.

19
7 A) Simple func on call
Aim: Display good morning message using func on.

Algorithm:
Step 1. Start.
Step 2. Define a func on. Give it a name. Use void as return type.
Step 3. Within the func on body, get input of name from user, and display good morning message.
Step 4. Within main() func on, call the func on defined in Step 2.
Step 5. End.

Program:

Output:

Result: The program to display good morning message using func on has been successfully
executed.

20
7 B) Func on with parameters and return values.

Aim: Add and mul ply two numbers using func ons having parameters and return values.

Algorithm:
Step 1. Start.
Step 2. Define a func on to add 2 numbers. Two ints are passed as parameters. The func on returns
their sum.
Step 3. Define a func on to mul ply 2 numbers. Two ints are passed as parameters. The func on
returns their product.
Step 4. Within main() func on, get input of 2 numbers from the user. Call the add and mul ply
func ons, and display the result.
Step 5. End.

Program:

21
Output:

Result: The program to add and mul ply two numbers using func ons having parameters and return
values has been successfully executed.

22
7 C) Passing parameters by reference (call by reference)

Aim: Increment the value of a variable using func on call by reference.


Algorithm:
step 1. Start.
Step 2. Define a func on to increment value of a variable. Have a pointer as parameter to the
func on.
Step 3. Within the func on body, increment the value pointed to by the pointer.
Step 4. Within main() func on, get input of a number from the user, and store it in a variable.
Step 5. Call the func on defined in Step 2. Pass the memory address of the variable declared in Step
4 as parameter to the func on.
Step 6. Display the incremented value.
Step 7. End.

Program:

Output:

Result: The program to increment the value of a variable using func on call by reference has been
successfully executed.

23
7 D) Passing parameters by reference (call by reference)

Aim: Swap the values of 2 variables using func on call by reference.

Algorithm:
Step 1. Start.
Step 2, Define a func on to swap 2 variables. Pass 2 pointers as parameters in the
defini on. Using a 3rd variable, exchange the values of the 2 variables pointed to by the
pointers.
Step 3. Within main() func on, get input values for 2 int variables.
Step 4. Call the func on defined in Step 2 with memory address of the variables declared
in Step 3 as parameters.
Step 5. Display the swapped values.
Step 6. End.

Program:

24
Output:

Result: The program to exchange values of 2 variables using func on call by reference
has been successfully executed.

25

You might also like