0% found this document useful (0 votes)
3 views

PSPL_C_LAB

The document provides a series of C programs demonstrating various programming concepts such as data types, decision-making constructs, and mathematical calculations. Each program includes an aim, algorithm, code, and a result statement confirming successful execution. Examples include displaying values using data types, calculating the area of a triangle, checking for positive/negative numbers, and creating a simple calculator.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

PSPL_C_LAB

The document provides a series of C programs demonstrating various programming concepts such as data types, decision-making constructs, and mathematical calculations. Each program includes an aim, algorithm, code, and a result statement confirming successful execution. Examples include displaying values using data types, calculating the area of a triangle, checking for positive/negative numbers, and creating a simple calculator.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 14

1a.

C Program to display the values using Data types

Aim:

To write a C program to display the values using Data types.

Algorithm:

Step 1: Start the Program.

Step 2: declare the variables as int, float and char.

Step 3: get the values using the data types.

Step 4: Display the values using data types.

Step 5: Stop the program.

Program:

#include<stdio.h>

int main()

int ivar; floatfvar;

char svar[100];

printf("\nEnter the integer value: ");

scanf("%d",&ivar);

printf("\nEnter the float value : ");

scanf("%f",&fvar);

printf(“\nEnter the string value: ");

scanf("%s",&svar);

printf("\n");

printf("\nInteger value is : %d",ivar);

printf("\nFloat value is : %f",fvar);

printf("\nEntered string is : %s",svar);

return0;

}
Output:

Result:

Thus a C program using I/O statements and expressions to display the values using Data

types has been executed successfully and its output is verified.

1b. C program to evaluate area of triangle with 3 sides given.

Aim:

To evaluate area of a triangle using the formula area=sqrt(s(s-a)(s-b)(s-c)) where a,b,c

are the sides of the triangle and s=(a+b+c)/2.

Algorithm:

Step1:start

Step2:inputa,b,c

Step3:s=(a+b+c)/2

Step4:A=sqrt(s*(s-a)(s-b)(s-c))

Step5:Print A

Step 6:stop

Program:

/*To evaluate area of triangle (sqrt(s(s-a)(s-b)(s-c)*/

#include<stdio.h>

#include<math.h>

voidmain()

int a,b,c;

floats,area;

printf("enter the values of a,b,c");

scanf("%d%d%d",&a,&b,&c);

s=(a+b+c)/2.0;
area=sqrt(s*(s-a)*(s-b)*(s-c));

printf("The area of a trangle is =%f",area);

getch();

Output:

Result:

Thus a C program using I/O statements and expressions to evaluate area of triangle has

been executed successfully and its output is verified.

2.Programs using decision-makingconstructs.

a) C program to check positive negative or zero using simple if statement

Aim:

To write a C program to check positive negative or zero using simple if statement.

Algorithm:

Step 1: Start the Program.

Step 2: Declare the variables as int.

Step 3: Check whether the given number is greater than zero then print Number is
POSITIVE.

Step 4: If the number is less than zero then print Number is NEGATIVE. Step 5: If the
number is

zero then print ZERO.

Step 6: Stop the program.

Program:

#include <stdio.h>

int main()

intnum;

/* Input number from user */


printf("Enter any number: ");

scanf("%d", &num);

if(num > 0)

printf("Number is POSITIVE");

if(num < 0)

printf("Number is NEGATIVE");

if(num == 0)

printf("Number is ZERO");

return 0;

Output:

Result:

Thus a C program using decision making constructs to check whether the number is positive,

negative or zero using simple if statements has been executed successfully and its output is
verified.

b). C program to check greatest among two numbers using if else statement.

Aim:

To write a C program to check greatest among two numbers using if else statement.

Algorithm:

Step 1: Start the Program.


Step 2: Declare the variables x and y as int.

Step 3: Get the values for x and y from the user.

Step 4: Compare the two values x and y

Step 5: If the value of x is greater when compared to y then print x is Greater.

Step 6: If the value of y is greater than x then print y is Greater using Else statement.

Step 7: Stop the program.

Program:

#include <stdio.h>

void main( )

int x, y;

printf("Enter the value of x:\n");

scanf("%d",&x);

printf("Enter the value of y:\n");

scanf("%d",&y);

if (x >y )

printf("x is Greater");

else

printf("y is Greater");

Output:
Result:

Thus a C program using decision making constructs to check greatest among two numbers
using simple if else statement has been executed successfully and its output is verified.

c). C program to check greatest among three numbers using Nested if... else statement.

Aim:

To write a C program to check greatest among three numbers using Nested if.else statement.

Algorithm:

Step 1: Start the program

Step 2: Declare variable a, b, c.

Step 3: If a > b go to step 4 Otherwise go to step 5

Step 4: If a > c then print a is greatest Otherwise print c is greatest

Step 5: If b > c then print b is greatest Otherwise print c is greatest

Step 6: Stop

Program:

#include <stdio.h>

void main( )

int a, b, c;

printf("Enter 3 numbers...");

scanf("%d%d%d",&a, &b, &c);

if(a > b)

if(a > c)

printf("a is the greatest");

else
{

printf("c is the greatest");

else

if(b > c)

printf("b is the greatest");

else

printf("c is the greatest");

Output:

Result:

Thus a C program using decision making constructs to checkgreatestamong three numbers


using simple Nested if else statement has been executed successfully and its output is
verified.

d). C program to check a given number is divisible by both 5 and 8 using elseif ladder
statement.

Aim:

To write a C program to check a given number is divisible by both 5 and 8 using else.if
ladder statement.

Algorithm:
Step 1: Start the program

Step 2: Declare variable a and get the value from the user.

Step 3: If a%5 == 0 && a%8 == 0 then print “Divisible by both 5 and 8” Otherwise go to
step 4

Step 4: If a%8 == 0 then print “Divisible by 8” Otherwise go to step 5

Step 5: If a%5 == 0then print “Divisible by 5” Otherwise go to step 6

Step 6: print "Divisible by none".

Step 7: End.

Program:

#include <stdio.h>

void main( )

int a;

printf("Enter a number...");

scanf("%d", &a);

if(a%5 == 0 && a%8 == 0)

printf("Divisible by both 5 and 8");

else if(a%8 == 0)

printf("Divisible by 8");

else if(a%5 == 0)

printf("Divisible by 5");

}
else

printf("Divisible by none");

Output:

Result:

Thus a C program using decision making constructs to check a given number is divisible by
both 5 and 8 using else.. if ladder statement has been executed successfully and its output is
verified.

3.Write a program to find whether the given year is a leap year or Not? (Hint: not every
centurion year is a leap year. For example, 1700, 1800 and 1900 is not a leap year)

Aim:

To develop a C Program to find whether the given year is a leap year or not.

Algorithm:
Step:1Start.

Step:2 Get the year

Step:3 Check if the year Modulo 4 equal to 0 and Modulo 100 not equal to 0.

Step:4 Check if the year Modulo 400 equal to 0

Step:5 if YES print leap year

Step:6 Else print Not Leap Year

Step:7Stop

Program:

#include<stdio.h>

#include<conio.h>

void main()

int year;

clrscr();

printf(“Enter the Year(YYYY):”);


scanf(“%d”,&year);

if(year%4==0 && year%100!=0||year%400==0)

printf(“\nThe Given year %d is a Leap Year”);

else

printf(“\n The Given year %d is Not a Leap Year”);

getch();

Output:

Result:

Thus a C program to find whether the given year is a leap year or not has been executed successfully
and its output is verified.

4.C program to create Simple Calculator using switch case.

Aim:

To write a C program to create Simple Calculator using switch case.

Algorithm:

Step 1: Start the Program.

Step 2: Input two numbers and a character from user in the given format. Store them in some variable
saynum1, op and num2

Step 3: Switch the value of op i.e. switch(op) and match with cases.

Step 4: For case '+' perform addition and store result in some variable i.e. result = num1 +
num2.Similarly for case '-' perform subtraction and store result in some variable i.e. result = num1 -
num2.Repeat the process for multiplication and division. Finally print the value of result.

Step 5: Stop the program.

Program:

#include<stdio.h>

#include<conio.h>

void main()

char op;
float num1, num2, result=0.0f;

/*Print welcome message*/

printf("\nWELCOME TO SIMPLE CALCULATOR");

printf(“\n \n”);

printf("\nEnter [number 1] [+ - * /] [number2]\n");

/*Input two numbers and operator from user*/

scanf("%f %c %f", &num1, &op, &num2);

/*Switch the value and perform action based on operator*/

switch(op)

case '+':

result = num1 + num2;

break;

case '-':

result = num1 - num2;

break;

case '*':

result = num1 * num2;

break;

case '/':

result = num1 / num2;

break;

default:

printf("Invalid operator");

/*Prints the result*/

printf("%.2f %c %.2f = %.2f", num1, op, num2, result);

getch();
}

Output:

Result:

Thus a C program to create Simple Calculator using switch case has been executed successfully and
its output is verified.

5.Check whether a given number is Armstrong number or not

Aim:

To write a C program to check whether a given number is Armstrong number or not. Algorithm:

Step 1: Start the Program.

Step 2: Get the number to be checked as Armstrong number or not.

Step 3: Check whether the number is Armstrong or not.

Step 4: Print whether the number is Armstrong number or not.

Step 5: Stop the Program.

Program:

#include <stdio.h>

int main() {

int num, originalNum, remainder, result = 0;

printf("Enter a three-digit integer: ");

scanf("%d",&num);

originalNum =num;

while (originalNum!= 0)

// remainder contains the last digit

remainder = originalNum % 10;

result += remainder * remainder * remainder;

//result += pow(remainder, n);

// removing last digit from the orignal number

originalNum /= 10;

}
if (result == num)

printf("%d is an Armstrong number.", num);

else

printf("%d is not an Armstrong number.", num);

return0;

Output:

Result:

Thus a C program to check whether a given number is Armstrong number or not has been executed
successfully and its output is verified.

6.Check whether a given number is odd or even

Aim:

To write a c program to check whether a given number is odd or even and verify the output.

Algorithm:

Step 1 : Start the program

Step 2 : Declare a variable N and get the value from the user

Step 3 : If N%2==0 print "It is an Even Number". Otherwise go to step 4 Step 4 : Print "It is an Odd
Number"

Step 5 : Stop the program

Program:

#include<stdio.h>

#include<conio.h>

void main()

int num;

clrscr();

printf(“\nOdd or Even Number\n”);

printf(“\n\nEnter an integer you want to check:”);

scanf(“%d”,&num);
if((num%2)==0)

printf(“%d is an Even number”,num);

else

printf(“%d is an Odd number”,num);

getch();

Output:

Result:

Thus, a C program has been developed which checks if the given number is odd or even and it’s
output has been verified.

You might also like