C Prog
C Prog
C Prog
3
Write a C program to find greatest of 3 numbers
A
using if-else statement.
2
4
Write a C program to check whether the number
B
is prime or not using while loop.
5 Write a C Program to create a simple
3 calculator using switch statement.
8
Write a C program to find GCD of 2 numbers by
A
demonstrating a function using call by value.
4
9 Write a C program to swap 2 numbers by
B demonstrating a function using call by
reference.
10 Write a C program to find the factorial of a given
A integer using recursive function.
5
11 Write a C program to find the Fibonnaci series
B using recursive function.
15
8
16
17
9
18
19
10
20
21
11
Introduction
C is a programming language developed at AT&T’s BELL Laboratory of USA in 1972.
Dennis Ritchie designed it. Because of its reliability. C is very popular. C is highly
Experiment No.1 A
Aim: Write a C program to demonstrate Operators Data Input and Output – getchar( ),
putchar( ), scanf( ), printf( ), gets( ), puts( ).
Program Description:
Functions Used for Input and Output
C language offers us several built-in functions for performing input/output operations.
The following are the functions used for standard input and output:
● printf() function - Show Output
● scanf() function - Take Input
● getchar() and putchar() function
● gets() and puts() function
1. The printf() function
● The printf() function is the most used function in the C language.
● This function is defined in the stdio.h header file and is used to show output on
the console (standard output).
2. The scanf() function
When we want to take input from the user, we use the scanf() function and store the
input value into a variable.
scanf() function is defined in the C stdio.h library.
3. getchar() & putchar() functions
The getchar and putchar functions are used for taking character input from the user and
printing the character as output.
The getchar() function
sum = a+b+c;
average = sum/3;
Experiment No.1 B
Aim: Program to demonstrate Operators-Arithmetic, Relational, Logical and Assignment
operators.
Objectives: Execute a program using Arithmetic, Relational and logical, Assignment,
Unary, Conditional, Bitwise, Comma, other operators.
Algorithm
Step1: Start
Step2: Declare and assign values to variable a and b as a=25, b=5;
Step3: Use arithmetic operators and print results.
Step4: Use Relational operators and print results.
Step5: Use Logical operators and print results.
Step 6: Use Assignment operators and print results.
Step 7: Stop.
Flowchart
The if Statement
Use the if statement to specify a block of code to be executed if a condition is true.
Syntax
if (condition) {
// block of code to be executed if the condition is true
}
Algorithm:
Step 1: Start
Step 2: Input 3 numbers as n1, n2, n3
Step 3: Check if n1 >= n2 && n1 >= n3 then go to step 6.
Step 4: Check if n2 >= n1 && n2 >= n3 then go to step 7.
Step 5: Check if n3 >= n1 && n3 >= n2 then go to step 8.
Step 6: Greatest number is n1. Go to step 9.
Step 7: Greatest number is n2. Go to step 9.
Step 8: Greatest number is n3. Go to step 9.
Program:
#include <stdio.h>
int main() {
Experiment No.2 B
Aim: Write a C program to check whether the number is prime or not using while loop.
Theory: Loops
Loops can execute a block of code as long as a specified condition is reached.
Loops are handy because they save time, reduce errors, and they make code more
readable.
While Loop
The while loop loops through a block of code as long as a specified condition is true:
Syntax
while (condition) {
// code block to be executed
}
The Do/While Loop
The do/while loop is a variant of the while loop. This loop will execute the code block
once, before checking if the condition is true, then it will repeat the loop as long as the
condition is true.
Syntax
do {
// code block to be executed
}
while (condition);
The example below uses a do/while loop. The loop will always be executed at least
once, even if the condition is false, because the code block is executed before the
condition is tested.
Algorithm
Flowchart:
Program:
#include <stdio.h>
int main() {
Experiment No.3
Aim: Write a C Program to create a simple calculator using switch statement.
Theory: Switch Statement
Instead of writing many if..else statements, you can use the switch statement.
The switch statement selects one of many code blocks to be executed:
Syntax
switch (expression) {
case x:
// code block
break;
case y:
// code block
break;
default:
// code block
}
This is how it works:
● The switch expression is evaluated once
● The value of the expression is compared with the values of each case
● If there is a match, the associated block of code is executed
● The break statement breaks out of the switch block and stops the execution
● The default statement is optional, and specifies some code to run if there is no
case match
The example below uses the weekday number to calculate the weekday name:
int day = 4;
switch (day) {
case 1:
printf("Monday");
break;
case 2:
printf("Tuesday");
break;
case 3:
printf("Wednesday");
break;
case 4:
printf("Thursday");
break;
case 5:
printf("Friday");
break;
case 6:
printf("Saturday");
break;
case 7:
printf("Sunday");
break;
}
Program: