EX NO 3 C Programming Lab Programs
EX NO 3 C Programming Lab Programs
Flowchart
Program
#include <stdio.h>
int main()
{
int number;
printf("Enter the number: ");
scanf("%d", & number);
// true if number is perfectly divisible by 2
if (number % 2 == 0) {
printf("The given number is EVEN");
}
else {
printf("The given number is ODD");
}
return 0;
}
Output
Enter a number:10
Number is Even
Enter a number:3
Number is odd
RESULT
Thus, a C Program using decision-making constructs was executed and the output
was obtained.
RESULT
Thus, a C Program using decision-making constructs was executed and the output
was obtained.
Question-1c. To write a C program to print weekday based on given number using
Ladder If -else statement.
Aim:
To write a C program to print weekday based on given number using Ladder If -
else statement.
Algorithm:
1. Start the program
2. Declare the variable day number
3. Take user input for the day number.
4. If day is equal to 1 then print 'Sunday.
5. If day is equal to 2 then print ' Monday'.
6. If day is equal to 3 then print ' Tuesday'.
7. If day is equal to 4 then print ‘Wednesday'.
8. If day is equal to 5 then print ' Thursday'.
9. If day is equal to 6 then print ' Friday'.
10.If day is equal to 7 then print ‘Saturday'.
11.if day is equal to any other value print "Invalid day number."
12.Stop the program.
Pseudocode:
13.BEGIN
14.READ day
15.IF day==1 THEN PRINT Sunday
16.ELSE IF day==2 THEN PRINT Monday
17.ELSE IF day==3 THEN PRINT Tuesday
18.ELSE IF day==4 THEN PRINT Wednesday
19.ELSE IF day==5 THEN PRINT Thursday
20.ELSE IF day==6 THEN PRINT Friday
21.ELSE IF day==7 THEN PRINT Saturday
22.Else
23.PRINT Invalid day
24.ENDIF
25.END
Flowchart:
Program:
#include<stdio.h>
int main()
{
int day;
printf("Enter day number: ");
scanf("%d", &day);
if(day==1)
{
printf("SUNDAY.");
}
else if(day==2)
{
printf("MONDAY.");
}
else if(day==3)
{
printf("TUESDAY.");
}
else if(day==4)
{
printf("WEDNESDAY.");
}
else if(day==5)
{
printf("THURSDAY.");
}
else if(day==6)
{
printf("FRIDAY.");
}
else if(day==7)
{
printf("SATURDAY.");
}
else
{
printf("INVALID DAY.");
}
return(0);
}
Output:
Enter Day Number
4
Wednesday
Enter Day Number
7
Saturday
Enter Day Number
12
INVALID DAY
Result:
Thus the C program using to perform calculator operation has been successfully
verified and executed.
Question-2. Develop a simple scientific calculator using Switch case statement.
Aim:
To write a C program to perform calculator operation using the switch case.
Algorithm:
Step-1: Start the program
Step-2: Read the variables a and b
Step-3: Options Display
Step-4: Read the input choice
Step-5: If Choice is
1 then execute step 6
2 then execute step 7
3 then execute step 8
4 then execute step 9
Other than above mentioned choice than execute step 10
Step-6: Addition of a and b
Step-7: Subtraction of a and b
Step-8: Multiplication of a and b
Step-9: Division of a and b
Step-10: Invalid choice
Step-11: Print the result
Step-12: Stop
Psuedocode:
BEGIN
INPUT a, b
READ input choice
IF the operator is ‘+’, then print the addition operation on the given integer.
IF the operator is ‘-’, then print the subtraction operation on the given integer.
IF the operator is ‘*’, then print the multiplication operation on the given integer.
IF the operator is ‘/’, then print the division operation on the given integer.
IF the operator is ‘%’, then print the remainder operation on the given integer.
ELSE
END
Flowchart:
Program:
#include<stdio.h>
int main ()
{
int a,b;
int op;
Output
Enter the values of a & b:
5
5
Result:
Thus the C program using to perform calculator operation has been successfully
verified and executed.
Question 3)
A Cartesian co-ordinate system has four quadrants. Write a C program to find the
quadrant of the co-ordinate points given by the user using both if-else and nested
if-else control structure.
Aim:
To write a C program to find the quadrant of the co-ordinate points given by the
user using both if-else and nested if-else control structure.
Algorithm:
Step 1: Start.
Step 2: Read two numbers x and y as input.
(x input corresponds to x co-ordinate. y input corresponds to y co-
ordinate.)
Step 3: if x>0 and y>0 print The coordinate point lies in First Quadrant.
Step 4: if x<0 and y>0 print The coordinate point lies in Second Quadrant.
Step 5: if x<0 and y<0 print The coordinate point lies in Third Quadrant.
Step 6: if x>0 and y<0 print The coordinate point lies in Fourth Quadrant.
Step 7: Stop.
Pseudo Code:
BEGIN
READ x AND y
IF x>0 AND y>0 THEN PRINT FIRST QUADRANT
IF x<0 AND y>0 THEN PRINT SECOND QUADRANT
IF x<0 AND y<0 THEN PRINT THIRD QUADRANT
IF x>0 AND y<0 THEN PRINT FOURTH QUADRANT
END
Flowchart:
Program:
#include <stdio.h>
void main()
int x,y;
scanf("%d",&x);
scanf("%d",&y);
}
Sample output:
Result:
Algorithm:
Step 1: Start.
Step 2: Read n as input.
Step 3: while n != 0 find the remainder using mod operator and print.
Step 4: Find quotient using division operator and continue the loop.
Step 5: Stop.
Pseudo Code:
BEGIN
READ n
WHILE n!=0 CONTINUE
remainder=n% 10
PRINT remainder
n=n/10
END
Flowchart:
Program:
#include <stdio.h>
void main()
scanf("%d", &n);
while (n != 0)
remainder = n % 10;
printf("%d",remainder);
n = n/10;
Sample output:
Aim:
Algorithm:
Step 1: Start.
Step 2: Read n as input.
Step 3: Initialize i=1.
Step 4: do print value of i ,n and i+n Increment i.
Step 5: until while i<=10.
Step 6: Stop.
Pseudo Code:
BEGIN
READ n
INITIALIZE i=1
DO CONTINUE
PRINT i,n and i*n.
END
Flowchart:
Program:
#include<stdio.h>
void main()
int n,i=1;
scanf("%d",&n);
do
printf("%d * %d = %d\n",i,n,i*n);
i++;
while(i<=10);
}
Sample output:
Result:
Aim:
Algorithm:
Step 1: Start.
Step 2: Read number of rows as input.
Step 3: for i=1 to i<=rows increment i.
Step 4: for j=1 to j<=i increment j.
Step 5: print the character *.
Step 6: Stop.
Pseudo Code:
BEGIN
READ rows
FOR i=1 TO i<=ROWS INCREMENT i
FOR j=1 TO j<=i INCREMENT j
PRINT *
END
Flowchart:
Program:
#include <stdio.h>
void main()
int i, j, rows;
scanf("%d", &rows);
printf("* ");
printf("\n");
}
Sample output:
Result:
Aim:
To write a C program to the sequence of Fibonacci for a given rose flower and
arrange the word "PIZZA" in several ways without repeating and calculate number
of ways it can be done using factorial concept.
Algorithm:
Start
Step:1- Receive a rose flower
Step:2- Disassemble the petals
Step:3- Check if the petal sequence is Fibonacci
Step:4- If Yes: Calculate Fibonacci and Display Fibonacci sequence
Step:5- If No: Display "Not Fibonacci sequence"
Step:6 - Receive word "PIZZA"
Step:7 - Generate permutations of the word
Step:8- Calculate the factorial of the word length
Step:9- Display the number of permutations
End
Pseudo Code:
BEGIN
procedure permutation()
Define n and r
P = factorial(n) / factorial(n-r)
DISPLAY P
end procedure
END
Flowchart:
Program:
#include <stdio.h>
// Function to calculate factorial
unsigned long long factorial(int n) {
if (n == 0 || n == 1)
return 1;
else
return n * factorial(n - 1);
}
return result;
}
int main() {
int n;
printf("Enter an integer (n): ");
scanf("%d", &n);
return 0;
}
Sample output:
Enter an integer (n): 5
Fibonacci for Rose Flower(5) = 5
Factorial(5) = 120
Number of permutations of 'PIZZA' = 60
Result:
Thus the program compiled and executed successfully.
Question –5- Product of two large prime numbers is used as encryption key in
encryption algorithms. Write a C program to display all the prime numbers between
1 to 100 and give the first two largest numbers as the output.
Aim:
To write a C program to display all the prime numbers between 1 to 100 and
give the first two largest numbers as the output.
Algorithm:
Start
Step:1- Initialize variables: num, isPrime, largest1, largest2
Step:2- Set largest1 and largest2 to 0
Step:3- Loop through numbers from 1 to 100 (num = 1 to 100)
Step:4- Initialize isPrime as true
Step:5- Check if num is less than 2, if true, mark isPrime as false
Step:6 - Continue to the next iteration
Step:7 - Loop through numbers from 2 to (num-1)
Step:8- Check if num is divisible by any number in the loop
Step:9- If true, mark isPrime as false and break the loop
Step:10 - Continue to the next iteration
Step:11 - If isPrime is true, check if num is greater than largest1
Step:12 - If true, assign num to largest2 and largest1 to num
Step:13- Else if num is greater than largest2, assign num to largest2
Step:14 - Display largest1 and largest2 as the first two largest prime numbers
End
Pseudo Code:
BEGIN
Initialize largest1 to 0
Initialize largest2 to 0
if (num < 2) {
isPrime = 0;
} else {
for (int i = 2; i < num; i++) {
if (num % i == 0) {
isPrime = 0;
break;
}
}
}
if (isPrime) {
if (num > largest1) {
largest2 = largest1;
largest1 = num;
} else if (num > largest2) {
largest2 = num;
}
}
}
return 0;
}
Sample output:
The first largest prime number: 97
The second largest prime number: 89
Result:
Thus the program compiled and executed successfully.