0% found this document useful (0 votes)
5 views41 pages

EX NO 3 C Programming Lab Programs

Uploaded by

hgvvb89
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views41 pages

EX NO 3 C Programming Lab Programs

Uploaded by

hgvvb89
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 41

Exercise - 3

Question-1- Describe a problem statement in your domain of interest whose


solution involves decision making and provide code as solution involving if-else,
nested if-else and ladder if-else.
Question-1a. To write a C program to find if a given number is even or odd using
if - else statement.
Aim
To write a C program to find if a given number is even or odd using if - else
statement.
Algorithm
Step-1: Start the program.
Step-2: Read number
Step-3: If the number is divisible by 2, print the given number is even.
Step-4: If the number is not divisible by 2, print the given number is odd.
Step-5: Stop the program.
Pseudo code
BEGIN
READ number
IF (number%2==0)
PRINT given number is even
ELSE
PRINT given number is odd
ENDIF
END

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.

Question-1b. To write a C program to find if a given number is positive, negative


or zero using nested if - else statement.
Aim:
To write a C program to find if a number is negative, positive or zero using if ...
else if ...
else statement.
Algorithm:
1. Start the program
2. Get the number
3. Check the number if it is negative, positive or equal to using if statement.
4. If the number is < 0print number is negative, else if the number is >0 print it is
positive else
the number =0.
5. Display the result
6. Stop the program.
Pseudocode:
BEGIN
READ number
IF number >0 THEN
PRINT "Entered number is positive"
ELSE IF number <0 THEN
PRINT "Entered number is negative"
ELSE
PRINT "Entered number is zero"
ENDIF
END
Flowchart:
Program:
#include<stdio.h>
void main ()
{
int number;
printf("Enter a number:");
scanf("%d",& number);
if (number <0)
printf("Number is negative");
else if (number >0)
printf("Number is positive");
else
printf("Number is equal to zero");
}
Output
Enter a number:109
Number is positive
Enter a number: -56
Number is negative
Enter a number:0
Number is equal to zero

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;

printf(" 1.Addition\n 2.Subtraction\n 3.Multiplication\n 4.Division\n


5.Remainder\n");
printf("Enter the values of a & b: ");
scanf("%d %d",&a,&b);
printf("Enter your Choice : ");
scanf("%d",&op);
switch(op)
{
case 1 :
printf("Sum of %d and %d is : %d",a,b,a+b);
break;
case 2 :
printf("Difference of %d and %d is : %d",a,b,a-b);
break;
case 3 :
printf("Multiplication of %d and %d is : %d",a,b,a*b);
break;
case 4:
printf("Division of Two Numbers is %d : ",a/b);
break;
case 5:
printf("Remainder of %d and %d is : %d",a,b,a%b);
break;
default:
printf(" Enter Your Correct Choice.");
break;
}
}

Output
Enter the values of a & b:
5
5

Enter your choice


1
Sum of a and b is : 10

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;

printf("Input the coordinate(x,y): \n");

scanf("%d",&x);

scanf("%d",&y);

if (x>0 && y>0)

printf("The coordinate point (%d,%d) lies in First Quadrant ",x,y);

else if (x<0 && y>0)

printf("The coordinate point (%d,%d) lies in second Quadrant ",x,y);

else if (x<0 && y<0)

printf("The coordinate point (%d,%d) lies in Third Quadrant ",x,y);

else if (x>0 && y<0)

printf("The coordinate point (%d,%d) lies in Forth Quadrant ",x,y);

}
Sample output:

Result:

Thus the program compiled and executed successfully.


Question 3a)
Describe a problem statement in your domain of interest whose solution
involves repetition of same steps and provide code as solution involving
while loop.
Aim:

To write a C program to reverse the given number using while loop.

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()

int n, reverse = 0, remainder;

printf("Enter an integer: ");

scanf("%d", &n);

while (n != 0)

remainder = n % 10;

printf("%d",remainder);

n = n/10;

Sample output:

Result: Thus the program compiled and executed successfully.


Question 3b)
Describe a problem statement in your domain of interest whose solution
involves repetition of same steps and provide code as solution involving
do-while loop.

Aim:

To write a C program to print the multiplication table using do-while loop.

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;

printf("Enter the table number\n");

scanf("%d",&n);

do

printf("%d * %d = %d\n",i,n,i*n);

i++;

while(i<=10);

}
Sample output:

Result:

Thus the program compiled and executed successfully.


Question 3c)
Describe a problem statement in your domain of interest whose solution
involves repetition of same steps and provide code as solution involving
for loop.

Aim:

To write a C program to print Half Pyramid of * using for loop.

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;

printf("Enter the number of rows: ");

scanf("%d", &rows);

for (i = 1; i <= rows; ++i)

for (j = 1; j <= i; ++j)

printf("* ");

printf("\n");

}
Sample output:

Result:

Thus the program compiled and executed successfully.


Question-4- – Given a rose flower to you, dismantle the petals of the flower from
inside, if you notice – it follows the sequence of Fibonacci. Now, try to arrange the
word "PIZZA" in several ways without repeating and calculate number of ways it can
be done using factorial concept. Write a C program to find both Fibonacci and
factorial by getting the mentioned input.

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);
}

// Function to calculate Fibonacci sequence


unsigned long long fibonacci(int n) {
if (n == 0)
return 0;
else if (n == 1)
return 1;
else {
unsigned long long a = 0, b = 1, temp;
for (int i = 2; i <= n; i++) {
temp = a + b;
a = b;
b = temp;
}
return b;
}
}
// Function to calculate permutations of a string
unsigned long long permutations(char str[], int n) {
unsigned long long result = factorial(n);
int count[26] = {0};

// Count the frequency of each character


for (int i = 0; i < n; i++) {
count[str[i] - 'A']++;
}

// Divide by factorial of frequencies


for (int i = 0; i < 26; i++) {
if (count[i] > 1) {
result /= factorial(count[i]);
}
}

return result;
}

int main() {
int n;
printf("Enter an integer (n): ");
scanf("%d", &n);

// Calculate Fibonacci and factorial


unsigned long long fib = fibonacci(n);
unsigned long long fact = factorial(n);

printf("Fibonacci for Rose Flower(%d) = %llu\n", n, fib);


printf("Factorial(%d) = %llu\n", n, fact);

// Calculate permutations of "PIZZA"


char word[] = "PIZZA";
int word_length = sizeof(word) - 1; // Exclude the null terminator
unsigned long long perms = permutations(word, word_length);

printf("Number of permutations of 'PIZZA' = %llu\n", perms);

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

For num from 1 to 100 do


Set isPrime to 1

If num < 2 Then


Set isPrime to 0
Else
For i from 2 to num-1 do
If num is divisible by i Then
Set isPrime to 0
Break out of the loop
End If
End For
End If

If isPrime is true Then


If num > largest1 Then
Set largest2 to largest1
Set largest1 to num
Else If num > largest2 Then
Set largest2 to num
End If
End If
End For

Print "The first largest prime number: largest1"


Print "The second largest prime number: largest2"
END
Flowchart:
Program:
#include <stdio.h>
int main() {
int num, isPrime, largest1 = 0, largest2 = 0;

for (num = 1; num <= 100; num++) {


isPrime = 1;

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;
}
}
}

printf("The first largest prime number: %d\n", largest1);


printf("The second largest prime number: %d\n", largest2);

return 0;
}

Sample output:
The first largest prime number: 97
The second largest prime number: 89

Result:
Thus the program compiled and executed successfully.

You might also like