Lab 5
Lab 5
Lab 5
5 To be familiar with
LOOPS
Tribhuvan University
Institute of Engineering
s
Purwanchal Campus , Dharan
#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;
}
#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;
}
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;
}
#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>
return result;
}
int main()
{
double xDegrees;
double xRadians;
int numTerms;
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;
originalNum = num;
originalNum = num;
if (sum == num)
{
printf("%d is an Armstrong number.\n", num);
}
else
{
printf("%d is not an Armstrong number.\n", num);
}
return 0;
}
#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;
#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;
}
#include <stdio.h>
int main() {
int n, reversed = 0, remainder, original;
printf("Enter an integer: ");
scanf("%d", &n);
original = n;
return 0;
}
#include <stdio.h>
int main()
{
int n1, n2, max, i, hcf;
printf("Enter two positive integers: ");
scanf("%d %d", &n1, &n2);
return 0;
}
#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;
return 0;
}