[go: up one dir, main page]

0% found this document useful (0 votes)
2 views23 pages

C Programming

Uploaded by

yaroli4570
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)
2 views23 pages

C Programming

Uploaded by

yaroli4570
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/ 23

EX.

NO CREATE PROBLEM ANALYSIS CHARTS, DATE


FLOWCHARTS AND PSEUDOCODE FOR ADD TWO
1A NUMBERS C PROGRAMS

AIM:
To Create problem analysis charts, flowchart sand pseudo code for add two numbers c
programs.
PROBLEMANALYSISCHARTS:
Component Description

Input num1,num2

Process Calculate Sum=num1+num2

Output Sum
ALGORITHM:
1. Start
2. Declarethreevariables:num1,num2,andsum.
3. Readthevalueofnum1.
4. Readthevalueofnum2.
5. Compute sum=num1+num2.
6. Display the value of sum.
7. Stop
PSEUDOCODE:
BEGIN
DECLAREnum1,num2,sumASINTEGER
PRINT "Enter first number:"
READnum1
PRINT "Enter second number:"
READnum2
sum ← num1 + num2
PRINT "Sum=",sum
END
FLOWCHART:

OUTPUT:
Enter first number: 5
Entersecondnumber:6
Sum = 11
PROGRAM:
#include<stdio.h>

intmain(){
intnum1,num2,sum;

printf("Enterfirstnumber:");
scanf("%d", &num1);

printf("Entersecondnumber:");
scanf("%d", &num2);

sum = num1 + num2;

printf("Sum=%d:\n",sum);

getch();
}

RESULT:
Thus ,the C program to add two numbers was executed successfully.
EX.NO DATE
TO CONVERT TEMPERATURE FROM CELSIUS TO
1B
FAHRENHEIT

AIM:
To create problem analysis charts, flow charts and pseudo code to convert
temperature from celsius to Fahrenheit C programs.
PROBLEMANALYSISCHARTS:

Required Results
Given Data (Input) Processing(Formula/Steps)
(Output)
1. Read the temperature
value in Celsius.
2. Calculate the Fahrenheit
The equivalent value using the formula:
A temperature value
temperature value in F=C×95+32capFequalscap
in Celsius (°C).
𝐹=𝐶×95+32
Fahrenheit (°F). C cross nine-fifths plus 32

3. Display the calculated


temperature in
Fahrenheit.
ALGORITHM:
Step-1Start
Step-2InputtemperatureinFahrenheitsayF
Step-3 C = 5.0/9.0 (F - 32 )
Step-4DisplayTemperatureinCelsiusC
Step-5 Stop
PSEUDOCODE:
BEGIN
DECLARE Celsius ,Fahrenheit ASREAL

PRINT "Enter temperature in Celsius:"


READ celsius

Fahrenheit ←(celsius*9/5)+32

PRINT "Temperature in Fahrenheit=",fahrenheit


END
FLOWCHART:

OUTPUT:
Enter temperature in Celsius: 20
Temperature in Fahrenheit=68.00
PROGRAM:
#include<stdio.h>

Int main(){
Float celsius,fahrenheit;
printf("Enter temperature in Celsius:");
scanf("%f", &celsius);
fahrenheit=(celsius*9/5)+32;
printf("Temperature in Fahrenheit=%.2f\n",fahrenheit);
getch();
}

RESULT:
Thus, the C program to convert temperature from Celsius to
Fahrenheit was executed successfully.
EX.NO DATE
TO SWAP TWO NUMBERS
1C
AIM:
To create problem analysis charts ,flowchart sandpseu do code to Swap Two
Numbers using C program.
PROBLEMANALYSISCHARTS:

Input Process Output


Two numbers A,B Swap values of A and B using temp variable A and B after swapping

ALGORITHM:
Step-1Start
Step-2 Input Two Numbers Say NUM1,NUM2
Step-3DisplayBeforeSwapValuesNUM1,NUM2
Step-4 TEMP = NUM1
Step-5NUM1=NUM2
Step-6NUM2= NUM1
Step-7DisplayAfterSwapValuesNUM1,NUM
Step-8 Stop
PSEUDOCODE:
BEGIN
DECLARE A,B ,temp ASINTEGER
PRINT "Enter two numbers:"
READ A, B
Temp ←A
A←B
B ←temp
PRINT" After swapping :A=",A,"B=",B END
FLOWCHART:

OUTPUT:
Enter two numbers: 55 25
After swapping: A=25,B=55
PROGRAM:
#include<stdio.h>
int main() {
intA,B,C;
printf("Enter two numbers:");
scanf("%d %d", &A, &B);
C=A;
A= B;
B=C;
printf("After swapping:A=%d,B=%d\n",A, B);
getch();
}

RESULT:
Thus, the C program to swap two numbers was executed successfully.
USAGEOFCONDITIONALLOGICSINPROGRAMS.
EX.NO DATE
CHECK IF A NUMBER IS POSITIVE,
2A
NEGATIVE, OR ZERO
AIM:
To check if a number is positive ,negative, or zero using conditionals
statements.
PROBLEMANALYSISCHARTS:

Input Process Output


If n > 0 → Positive
A number(n) If n<0→Negative Message:“Positive” ,“Negative” or “Zero”
Else → Zero
PSEUDOCODE:
BEGIN
DECLARE n ASINTEGER

PRINT "Enter a number:"


READ n

IF n> 0THEN
PRINT "Number is Positive"
ELSE IF n < 0 THEN
PRINT "Number is Negative"
ELSE
PRINT "Number is Zero"
END IF
END
FLOWCHART:

OUTPUT:
Enteranumber:50
Number is Positive
Enter a number:-23
Number is Negative
Enter a number : 00
Number is Zero
PROGRAM:
#include<stdio.h>
int main() {
Int n;
printf("Enter a number:");
scanf("%d", &n);
if(n>0){
printf("Number is Positive\n");
}
else if(n<0){
printf("Number is Negative\n");
}
else{
printf("Number is Zero\n");
}

getch();
}

RESULT:
Thus, the C program to check whether a given number is positive,
negative, or zero using conditional statements was executed successfully.
EX.NO DATE
2B TO CHECK FOR ODD OR EVEN NUMBER

AIM:
To check for odd or even number using conditionals statements.
PROBLEMANALYSISCHART:
Input Output Processing Logic
A number n A message stating 1. Dividethenumbernby2using the
whether the number modulus operator %
is Odd or Even
2. Ifn%2==0, then the number is
Even
3. Else, the number is Odd
ALGORITHM:
1. Start
2. Input a number n
3. Checkifn%2==0
 If true, then the number is Even
 If false ,then the number is Odd
4. Display the result(Odd or Even)
5. Stop
PSEUDOCODE:
START
READ n
IFn%2== 0THEN
PRINT" Even number"
ELSE
PRINT" Odd number"
ENDIF
STOP
FLOWCHART:

OUTPUT:
Enteranumber:10 10
is Even

Enteranumber:5 5
is Odd
PROGRAM:
#include<stdio.h>
int main() {
int num;
printf("Enter a number:");
scanf("%d", &num);
if(num % 2 == 0)
printf("%d is Even\n",num);
else{
printf("%d is Odd\
n",num); }
getch();
}

RESULT:
Thus, the C program to check whether a number is odd or even using
conditional statements was designed, implemented, and executed successfully
EX.NO DATE
2c TO CHECK ARMSTRONG NUMBER

AIM:
To Check Armstrong Number using conditionals statements.
PROBLEMANALYSISCHART:
Input Output ProcessingLogic
1. Copy then number into a
temporary variable.
2. Count then Number of digits in
A message stating the number.
whether the number is 3. Extract each digit, raise it to the
A number n
Armstrong or Not power of the number of digits ,and
Armstrong add to sum.
4. After processing all digits:
 If sum==original number→
Armstrong Number
 Else→ Not Armstrong Number
ALGORITHM:
1. Start
2. Input a number n
3. Store the number in a temporary variable temp
4. Count the number of digits in n →digits
5. Initialize sum=0
6. Repeatuntilnbecomes0:
 Extract the last digit →digit=n% 10
 Compute digit ^digits and add to sum
 Remove the last digit→ n=n/10
7. If sum==temp ,then the number is an Armstrong number
Else ,it is no tan Armstrong number
8. Stop
PSEUDOCODE:
START
READ n
SET temp=n
SET sum = 0
FIND number of digits=digits
WHILE n > 0 DO
digit=n%10
sum=sum+(digit^digits) n =
n / 10
ENDWHILE
IF sum == temp THEN
PRINT"Armstrongnumber"
ELSE
PRINT"NotArmstrongnumber" END
IF
STOP
FLOWCHART:

PROGRAM:
#include <stdio.h>
#include <math.h>

int main() {
int num, originalNum, remainder, n = 0;
double result = 0.0;
printf("Enter an integer: ");
scanf("%d", &num);

originalNum = num;
while (originalNum != 0) {
originalNum /= 10;
++n;
}

originalNum = num;
while (originalNum != 0) {
remainder = originalNum % 10;
result += pow(remainder, n);
originalNum /= 10;
}

if ((int)result == num) {
printf("%d is an Armstrong number.\n", num);
} else {
printf("%d is not an Armstrong number.\n", num);
}
getch();
}

OUTPUT:
Enter a number:153
153isanArmstrongnumber.

Enter a number:173
173isnotanArmstrongnumber.

RESULT:

Thus, the C program to check whether a number is an Armstrong number


was executed successfully.
EX.NO DATE
2d TO DESIGN A SCIENTIFIC CALCULATOR USING
SWITCH CASE IN C
AIM:
To design a Scientific Calculator using switch case in C

ALGORITHM:
1. Start
2. Display the menu of operations(Addition,Subtraction,Multiplication,
Division, Modulus, Power, Square Root, etc.)
3. Input user's choice
4. Use switch statement to perform the selected operation
5. For each case:
 Input required numbers
 Perform operation
 Display result
6. If in valid choice→display error message
7. End
FLOWCHART:

PROGRAM:
#include <stdio.h>
#include <math.h>
#include <stdlib.h> // For the exit() function

int main() {
int choice;
double num1, num2, result;
char repeat;

do {
printf("\n===== Scientific Calculator =====\n");
printf("1. Addition (+)\n");
printf("2. Subtraction (-)\n");
printf("3. Multiplication (*)\n");
printf("4. Division (/)\n");
printf("5. Modulus (%%)\n");
printf("6. Power (^)\n");
printf("7. Square Root (√)\n");
printf("8. Exit\n");
printf("Enter your choice: ");

// Read user choice as an integer


scanf("%d", &choice);

// Clear the input buffer to prevent issues with future scanf calls
// by consuming the newline character left in the buffer.
while (getchar() != '\n');

switch (choice) {
case 1:
printf("Enter two numbers: ");
scanf("%lf %lf", &num1, &num2);
result = num1 + num2;
printf("Result: %.2lf\n", result);
break;
case 2:
printf("Enter two numbers: ");
scanf("%lf %lf", &num1, &num2);
result = num1 - num2;
printf("Result: %.2lf\n", result);
break;
case 3:
printf("Enter two numbers: ");
scanf("%lf %lf", &num1, &num2);
result = num1 * num2;
printf("Result: %.2lf\n", result);
break;
case 4:
printf("Enter two numbers: ");
scanf("%lf %lf", &num1, &num2);
if (num2 != 0) {
result = num1 / num2;
printf("Result: %.2lf\n", result);
} else {
printf("Error: Division by zero!\n");
}
break;
case 5: {
int a, b;
printf("Enter two integers: ");
scanf("%d %d", &a, &b);
if (b != 0) {
result = a % b;
printf("Result: %.2lf\n", result);
} else {
printf("Error: Division by zero!\n");
}
break;
}
case 6:
printf("Enter base and exponent: ");
scanf("%lf %lf", &num1, &num2);
result = pow(num1, num2);
printf("Result: %.2lf\n", result);
break;
case 7:
printf("Enter a number: ");
scanf("%lf", &num1);
if (num1 >= 0) {
result = sqrt(num1);
printf("Result: %.2lf\n", result);
} else {
printf("Error: Negative number cannot have a real square root!\n");
}
break;
case 8:
printf("Exiting calculator. Goodbye!\n");
exit(0);
default:
printf("Invalid choice!\n");
}

printf("\nDo you want to perform another calculation? (y/n): ");


scanf(" %c", &repeat); // Note the space before %c to handle whitespace

} while (repeat == 'y' || repeat == 'Y');

return 0;
}
OUTPUT:

RESULT:
Thus the C program to design a scientific calculator using
switch case was executed successfully.

You might also like