0% found this document useful (0 votes)
17 views16 pages

C Lab Programs

The document contains multiple C programs demonstrating various programming concepts, including calculating the area and circumference of a circle, implementing a basic calculator, exchanging variable values, converting between integer and float types, solving mathematical expressions, printing month names, summing non-negative numbers, checking for palindromes, generating Fibonacci series, identifying Armstrong numbers, and calculating factorials. Each program includes an aim, algorithm, code, output, and a result confirming successful execution. The document serves as a comprehensive guide for beginners in C programming.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views16 pages

C Lab Programs

The document contains multiple C programs demonstrating various programming concepts, including calculating the area and circumference of a circle, implementing a basic calculator, exchanging variable values, converting between integer and float types, solving mathematical expressions, printing month names, summing non-negative numbers, checking for palindromes, generating Fibonacci series, identifying Armstrong numbers, and calculating factorials. Each program includes an aim, algorithm, code, output, and a result confirming successful execution. The document serves as a comprehensive guide for beginners in C programming.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

EX.NO.1.a.

Program to find area and circumference of circle

AIM:

To write a c program to find the area and circumference of circle.

ALGORITHM:

Step 1: start

Step 2: Declare the variable radius

Step 3: Define PI value as 3.14 using #define and consider float data type for radius

Step 4: area = PI * radius * radius

Step 5: circumference = 2 * PI * radius

Step 6: Print the values of area and circumference

Step 7: stop

PROGRAM:

#include <stdio.h>

#define PI 3.14

int main() {

float radius;

printf("radius : ");

scanf("%f", &radius);

float area = PI * radius * radius;

float circumference = 2 * PI * radius;

printf("area = %.6f\n", area);

printf("circumference = %.6f\n", circumference);

return 0;

OUTPUT:

radius:2.67

area=22.384747

circumference=16.767601
RESULT:

Thus the C program to find area and circumference of circle has been executed successfully and the
output has been verified.

Ex.No.1.b.Program to implement basic calculator

AIM:

To write a C Program to implement basic calculator.

ALGORITHM:

Step 1: Start

Step 2: Declare variables num1 (float) ,num2 (float)

Step 3: Input num1, operator, num2

Step 4: Perform the operation based on the operator:

If operator is '+', then result = num1 + num2

If operator is '-', then result = num1 - num2

If operator is '*', then result = num1 * num2

If operator is '/', then result = num1 / num2

Step 5: Display the result

Step 6: End

PROGRAM:

#include<stdio.h>

void main()

float a,b;

printf("enter number1: ");

scanf("%f",&a);

printf("enter non zero number2: ");

scanf("%f",&b);

printf("%.2f + %.2f = %.2f\n",a,b,a+b);

printf("%.2f - %.2f = %.2f\n",a,b,a-b);


printf("%.2f * %.2f = %.2f\n",a,b,a*b);

printf("%.2f / %.2f = %.2f\n",a,b,a/b);

OUTPUT:

enter number1:2

enter non zero number2: 3

2.00 + 3.00 = 5.00

2.00 - 3.00 = -1.00

2.00 * 3.00 = 6.00

2.00 / 3.00 = 0.67

RESULT:

Thus the basic calculator program has been executed successfully and output has been verified

Ex.No.1.c.Program to exchange the values of variables

AIM:

To write a C Program to exchange the values of variables.

ALGORITHM:

Step 1. Start

Step 2. Declare variables - var1 (datatype) - var2 (datatype) - temp (datatype)

Step 3. Input values for var1 and var2

Step 4. Display the original values of var1 and var2

• Set temp = var1

• Set var1 = var2

• Set var2 = temp

Step 5. Display the new values of var1 and var2

Step 6. End
PROGRAM:

#include<stdio.h>

void main()

int a,b,c,d,temp;

printf("Enter values of a, b, c and d: ");

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

temp=a;

a=b;

b=c;

c=d;

d=temp;

printf("After swapping\na = %d\nb = %d\nc = %d\nd = %d\n",a,b,c,d);

OUTPUT:

Enter values of a, b, c and d: 1 2 3 4

After swapping

a=2

b=3

c=4

RESULT:

Thus the C program for exchanging the values of variables has been executed successfully and the
output has been verified.
Ex.No.1.d. Program to convert int to float and float to int

AIM:

To write a C Program to convert int to float and float to int.

ALGORITHM:

Step 1. Start

Step 2. Declare variables - intValue (int) - floatValue (float)

Step 3. Input an integer value into intValue

Step 4. Convert intValue to a float and store the result in floatValue

Step 5. Display the original integer value (intValue)

Step 6. Display the converted float value (floatValue)

Step 7. Input a float value into floatValue

Step 8. Convert floatValue to an integer and store the result in intValue

Step 9. Display the original float value (floatValue)

Step 10. Display the converted integer value (intValue)

Step 11. End

PROGRAM:

#include <stdio.h>

int main() {

double input;

printf("Enter a value: ");

scanf("%lf", &input);

int intValue = (int)input;

float floatValue = (float)input;

printf("Value as an integer: %d\n", intValue);

printf("Value as a float: %.2f\n", floatValue);

return 0;

}
OUTPUT:

Enter a value: 12345.67890

Value as an integer: 12345

Value as a float: 12345.68

RESULT:

Thus the C Program for conversion of int to float and float has been executed successfully and the output
has been verified.

Ex.No.1.e.Program to solve the expression 3*2/3+5

AIM:

To write a C program to solve the expression as 3*2/3+5

ALGORITHM:

Step 1: start

Step 2: 3 * 2 is calculated first, resulting in 6.

Step 3:6 / 3.0 is then calculated, resulting in 2.0

Step 4: Finally, 2.0 + 5 is calculated, resulting in the final value 7.0.

Step 5: The printf statement is used to display the result of the expression.

Step 6: End

PROGRAM:

#include <stdio.h>

int main()

double result;

result = 3*2/3+5;

printf("Result: %.2lf\n",result );

return 0;

OUTPUT:

Result :7.00
RESULT:

Thus the C Program for solving the expression as 3*2/3+5 has been executed successfully and the
output has been verified.

Ex.No.2.a. Program to print month name based on user choice

AIM:

To write a C Program to print month name based on user choice.

ALGORITHM:

Step 1:Start

Step 2: Read the month_number from the user

Step 3: Check if the month_number is between 1 and 12

a. If yes, go to step Step 4

b. If no, print an error message and go to step 7

Step 4: Create a list of month_names with elements: ["January", "February", "March",


"April", "May", "June", "July", "August", "September", "October", "November",
"December"]

Step 5: Set month_name to month_names[month_number - 1]

Step 6: Print the month_name

Step 7: Print an error message indicating that the month_number is invalid

Step 8: End

PROGRAM:

#include<stdio.h>

int main()

int n;

printf("Enter month number : ");

scanf("%d",&n);

switch(n)

case 1:

printf("January\n");
break;

case 2:

printf("February\n");

break;

case 3:

printf("March\n");

break;

case 4:

printf("April\n");

break;

case 5:

printf("May\n");

break;

case 6:

printf("June\n");

break;

case 7:

printf("July\n");

break;

case 8:

printf("August\n");

break;

case 9:

printf("September\n");

break;

case 10:

printf("October\n");

break;
case 11:

printf("November\n");

break;

case 12:

printf("December\n");

break;

default:

printf("Invalid month number\n");

OUTPUT:

Enter month number : 11

November

RESULT:

Thus the C Program to print month name based on user choice has been executed successfully and the
output has been verified.

Ex.No.2.b.Program to find the sum of nonnegative numbers entered by users

AIM:

To write a C program to find the sum of nonnegative numbers entered by users.

ALGORITHM:

Step1: Start

Step 2: Initialize sum to 0

Step 3: Repeat the following steps until the user enters a negative number:

a. Read a number from the user

b. If the entered number is negative, go to step 5

c. Add the entered number to sum

Step 4: Print the sum

Step 5: End
PROGRAM:

#include<stdio.h>

int main()

int n,i,num,sum=0;

i=0;

scanf("%d",&n);

printf("Enter %d Numbers: ",n);

while(i<n)

scanf("%d",&num);

if(num>=0)

sum=sum+num;

i++;

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

OUTPUT:

Enter 5 Numbers : -1 2 3 4 -7

Sum = 9

RESULT:

Thus the C Program to find the sum of non negative numbers has been executed successfully and the
output has been verified.

Ex.No.2.c.Program to check whether a number is palindrome or not

AIM:

To write a C program to check whether a number is palindrome or not.


ALGORITHM:

Step 1: Start

Step 2: Read a number from the user and store it in the variable 'originalNumber'

Step 3: Initialize variables 'reverseNumber' and 'temp' to 0

Step 4: Set 'temp' to the value of 'originalNumber'

Step 5: Repeat the following steps until 'temp' becomes 0:

a. Extract the last digit of 'temp' and store it in a variable 'digit'

b. Multiply 'reverseNumber' by 10 and add 'digit' to it

c. Remove the last digit from 'temp' by integer division by 10

Step 6: If 'reverseNumber' is equal to 'originalNumber', go to step 7. Otherwise, go to step 8


Step 7: Print "The number is a palindrome."

Step 8: Print "The number is not a palindrome."

Step 9: End

PROGRAM:

#include <stdio.h>

int main() {

int num, originalnum, reversednum = 0, remainder;

printf("Enter an integer : ");

scanf("%d", &num);

originalnum = num;

while (num != 0)

remainder = num % 10;

reversednum = reversednum * 10 + remainder;

num=num/10;

printf("The reverse of a given number : %d\n", reversednum);

if(originalnum == reversednum)

{
printf("%d is a palindrome\n", originalnum);

else

printf("%d is not a palindrome\n", originalnum);

return 0;

OUTPUT:

Enter an integer : 2014

The reverse of a given number : 4102

2014 is not a palindrome

RESULT:

Thus the C Program to check whether a given number is palindrome or not has been executed
successfully and the output has been verified.

Ex.No.2.d.Program to print Fibonacci series

AIM:

To write a C program to print Fibonacci series.

ALGORITHM:

Step 1: Start

Step 2: Read the number of terms 'n' for the Fibonacci series from the user

Step 3: Initialize variables 'first' to 0, 'second' to 1, and 'next' to 0

Step 4: Print "Fibonacci Series:"

Step 5: Repeat the following steps 'n' times:

a. Print the value of 'first'

b. Set 'next' to the sum of 'first' and 'second'

c. Update 'first' to the current value of 'second'


d. Update 'second' to the current value of 'next'

Step 6: End

PROGRAM:

#include <stdio.h>

int main()

int n,first =0, second=1,next;

printf("Enter the maximum limit to generate the Fibonacci series : ");

scanf("%d",&n);

next=first+second;

printf("The Fibonacci series is : %d %d",first,second);

for(int i =3;next<=n;++i)

printf(" %d",next);

first=second;

second=next;

next=first+second;

OUTPUT:

Enter the maximum limit to generate the Fibonacci series : 10

The Fibonacci series is : 0 1 1 2 3 5 8

RESULT:

Thus the C program to print Fibonacci series has been executed successfully and the output has been
verified.

Ex.No.2.e.Program to check whether a given number is Armstrong or not

AIM:

To write a c program to check whether a given number is Armstrong or not.


ALGORITHM:

Step 1: Start

Step 2: Read the range of numbers (start and end) from the user

Step 3: For each number 'num' in the range from start to end (inclusive), repeat steps 4-8:

Step 4: Set 'originalNum' to 'num'

Step 5: Initialize 'sum' to 0

Step 6: Count the number of digits in 'num' and store it in 'numDigits'

Step 7: Repeat the following steps until 'num' becomes 0:

i. Extract the last digit of 'num' and store it in 'digit'

ii. Add 'digit' raised to the power of 'numDigits' to 'sum'

iii. Remove the last digit from 'num' by integer division by 10

Step 8: If 'sum' is equal to 'originalNum', print 'originalNum' as it is an Armstrong number


Step 9: End

PROGRAM:

#include<stdio.h>

int main()

int n, oNumber, R, a = 0, result= 0;

printf("Enter any number : ");

scanf("%d", &n);

oNumber = n;

while (oNumber != 0)

oNumber/= 10;

++a;

oNumber = n;

while (oNumber != 0)

{
R= oNumber % 10;

result += pow(R, a);

oNumber /= 10;

if (result== n)

printf("The given number %d is an armstrong number\n", n);

else

printf("The given number %d is not an armstrong number\n", n);

return 0;

OUTPUT:

Enter any number:370

The given number 370 is an armstrong number.

RESULT:

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

Ex.No.2.f.Program to find the factorial of a given number

AIM:

To write a C program to find the factorial of a given number.

ALGORITHM:

Step 1:Start

Step 2: Read a non-negative integer 'n' from the user

Step 3: If 'n' is 0, set 'factorial' to 1 and go to step 6

Step 4: Initialize 'factorial' to 1

Step 5: Repeat the following steps from 'i' equals 1 to 'n': a. Multiply 'factorial' by 'i'

Step 6: Print 'factorial'

Step 7:End

PROGRAM:
#include<stdio.h>

int main()

int n,i,fact=1;

printf("Num: ");

scanf("%d",&n);

if(n<0)

printf("Invalid\n");

else{

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

fact=fact*i;

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

OUTPUT:

Num : 9

Factorial : 362880

RESULT:

Thus the C program to find factorial of a given number has been executed successfully and the output has been
verified.

You might also like