Lab 5

Download as pdf or txt
Download as pdf or txt
You are on page 1of 13

LAB SHEET NO.

5 To be familiar with
LOOPS

Tribhuvan University
Institute of Engineering
s
Purwanchal Campus , Dharan

Prepared by: Submitted to:


Name: Lokendra Joshi
Class: BCT AB
Roll no : 36 ___________________________
Pravin Sangroula
IOE, Purwanchal Campus
1. WAP to read 10 numbers from users and find their sum and average.

#include <stdio.h>

int main()
{
float num, sum = 0, average;
printf("Enter 10 numbers separated by spaces:\n");
for (int i = 1; i <= 10; i++)
{
scanf("%f", &num);
sum += num;
}
average = sum / 10;
printf("Sum = %f and Average = %f\n", sum, average);
return 0;
}

2. WAP to display the multiplication table of integer given by the


user.

#include <stdio.h>

int main()
{
int i, mul, num;
printf("Enter a number for multiplication:\n");
scanf("%d", &num);
for (i = 1; i <= 10; i++)
{
mul = num * i;
printf("The multiplication of 2*%d=%d\n", i, mul);
}
return 0;
}
3. WAP to input two integer values from the user and print the even
number between the range of integers. Also count the even number and
display the count as well [Hint: if user enters 10 and 100. The program
should print and count even numbers between 10 and 100].

#include <stdio.h>

int main()
{
int num1, num2, i, temp, count = 0;
printf("Enter two numbers:\n");
scanf("%d%d", &num1, &num2);
if (num1 > num2)
{
temp = num1;
num1 = num2;
num2 = temp;
}

for (i = num1+1; i < num2; i++)


{
if (i % 2 == 0)
{
printf("The even numbers between %d and %d are: %d\n", num1,
num2, i);
count += 1;
}
}
printf("Number of even numbers are: %d", count);

return 0;
}
4. WAP to display sum of series: 1 + 1/2 + 1/3 + 1/4 + 1/5 ... 1/n
#include <stdio.h>

int main()
{
int n;
float sum = 0, i;
printf("Enter number of terms:\n");
scanf("%d",&n);
for (i = 1; i <= n; i++)
{
sum = sum + (1 / i);
}
printf("sum=%.2f", sum);
return 0;
}

5. WAP to display sum of series: 1 + 1/2! + 1/3! + 1/4! + 1/5! ... 1/n!

#include <stdio.h>
long factorial(int i)
{
int j, fact = 1;
for (j = 1; j <= i; j++)
{
fact = fact * j;
}
return fact;
}

int main()
{
int n, i;
long fact;
float sum = 0;
printf("Enter the number of terms:\n");
scanf("%d", &n);
for (i = 1; i <= n; i++)
{
fact = factorial(i);
sum = sum + (1.0 / fact);
}
printf("Sum= %.2f", sum);
return 0;
}

6. WAP to display sum of series: x + x^2/2! + x^3/3! + x^4/4! + x^5/5! ...


x^n/n!

#include <stdio.h>
#include<math.h>
long factorial(int i)
{
int j, fact = 1;
for (j = 1; j <= i; j++)
{
fact = fact * j;
}
return fact;
}
int main()
{
int n, i;
long fact;
float sum = 0,x;
printf("Enter the value of x:\n");
scanf("%f",&x);
printf("Enter the number of terms:\n");
scanf("%d", &n);
for (i = 1; i <= n; i++)
{
fact = factorial(i);
sum = sum + (pow(x,i) / fact);
}
printf("sum=%.2f", sum);
return 0;
}
7. WAP to find the value cos(x) without using cos(x) library function.

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

double calculateCos(double x, int terms)


{
double result = 1.0; // First term of the series
double power = 1.0;
int sign = -1;
int fact = 1;

for (int i = 1; i <= terms; i++)


{
power = power * x * x;
fact = fact * (2 * i) * (2 * i - 1);
result = result + sign * (power / fact);
sign = -sign;
}

return result;
}

int main()
{
double xDegrees;
double xRadians;
int numTerms;

printf("Enter the value of x in degrees: ");


scanf("%lf", &xDegrees);

// Convert degrees to radians


xRadians = xDegrees * (M_PI / 180.0);

printf("Enter the number of terms to approximate cos(x): ");


scanf("%d", &numTerms);

double result = calculateCos(xRadians, numTerms);


printf("cos(%.2lf degrees) = %.4lf\n", xDegrees, result);

return 0;
}
8. WAP to display whether a number is Armstrong or not.

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

int main()
{
int num, originalNum, remainder, n = 0;
double sum = 0.0;

printf("Enter a number: ");


scanf("%d", &num);

originalNum = num;

// Count the number of digits in the given number


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

originalNum = num;

// Calculate the sum of digits raised to the power of count


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

if (sum == num)
{
printf("%d is an Armstrong number.\n", num);
}
else
{
printf("%d is not an Armstrong number.\n", num);
}
return 0;
}

9. WAP to display the first n terms of Fibonacci series.

#include <stdio.h>

int main()
{
int i, num1 = 0, num2 = 1, sum, n;
printf("how many number of terms you want in fibs series?: ");
scanf("%d", &n);
printf("%d ", num1);
for (i = 2; i < n; i++)
{
sum = num1 + num2;

printf("%d ", sum);


num1 = num2;
num2 = sum;
}
return 0;
}

10. WAP to display the number in reverse order.

#include <stdio.h>
int main() {
int n, reversed = 0, remainder, original;
printf("Enter an integer: ");
scanf("%d", &n);
original = n;

while (n != 0) {
remainder = n % 10;
reversed = reversed * 10 + remainder;
n /= 10;
}
printf("the reverse of %d is %d",original,reversed);

return 0;
}

11. WAP to check whether a number is a palindrome or not.

#include <stdio.h>
int main() {
int n, reversed = 0, remainder, original;
printf("Enter an integer: ");
scanf("%d", &n);
original = n;

// reversed integer is stored in reversed variable


while (n != 0) {
remainder = n % 10;
reversed = reversed * 10 + remainder;
n /= 10;
}

// palindrome if orignal and reversed are equal


if (original == reversed)
printf("%d is a palindrome.", original);
else
printf("%d is not a palindrome.", original);

return 0;
}

12. WAP to find HCF and LCM of two numbers.

#include <stdio.h>

int main()
{
int n1, n2, max, i, hcf;
printf("Enter two positive integers: ");
scanf("%d %d", &n1, &n2);

// maximum number between n1 and n2 is stored in max


max = (n1 > n2) ? n1 : n2;
while (1)
{
if ((max % n1 == 0) && (max % n2 == 0))
{
printf("The LCM of %d and %d is %d.", n1, n2, max);
break;
}
++max;
}

for (i = 1; i <= n1 && i <= n2; ++i)


{
// Checks if i is factor of both integers
if (n1 % i == 0 && n2 % i == 0)
hcf = i;
}

printf("HCF of %d and %d is %d", n1, n2, hcf);

return 0;
}

13. WAP to print the following patterns:


1
12
123
1234
12345

#include <stdio.h>

int main()
{
int i, j, row;
printf("ENTER A NUMBER\n");
scanf("%d", &row);
printf("*******************************\n");
for (i = 1; i <= row; i++)
{
for (j = 1; j <= i; j++)
{
printf("%d ", j);
}
printf("\n");
}
printf("*******************************");
return 0;
}

12345
1234
123
12
1

#include <stdio.h>

int main()
{
int i, j, row;
printf("ENTER A NUMBER\n");
scanf("%d", &row);
printf("*******************************\n");
for (i = row; i > 0; i--)
{
for (j = 1; j <= i; j++)
{
printf("%d ", j);
}
printf("\n");
}
printf("*******************************\n");
return 0;
}
*
***
*****
*******
*********

#include <stdio.h>

int main()
{
int rows;
printf("ENTER THE NUMBER OF ROWS\n");
scanf("%d",&rows);
for (int i = 0; i < rows; i++)
{
// Print spaces before the asterisks
for (int j = 0; j < rows - i - 1; j++)
{
printf(" ");
}

// Print asterisks
for (int k = 0; k < 2 * i + 1; k++)
{
printf("*");
}

printf("\n");
}

return 0;
}
54321
5432
543
54
5

#include <stdio.h>

int main() {
int n;

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


scanf("%d", &n);

for (int i = 0; i < n; i++) {


// Print numbers in decreasing order
for (int j = n; j > i; j--) {
printf("%d ", j);
}

// Move to the next line


printf("\n");
}

return 0;
}

You might also like