Java Stream API — Even Numbers (Full Screen) Java Stream API — Get Even Numbers Example 1 — Filter even numbers from a list Creates a list, uses Stream to filter evens, and prints them. Copy import java.util.*; import java.util.stream.*; public class EvenNumbersStream { public static void main(String[] args) { // Create a list of numbers List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); // Use Stream API to filter even numbers List<Integer> evenNumbers = numbers.stream() .filter(n -> n % 2 == 0) .collect(Collectors.toList()); // Print the even numbers System.out.println( "Even numbers: " + evenNumbers); } } Example 2 — Use IntStream.rangeClosed ...
In this C-program we will calculate the power of a number and print the result. The number will be taken from the user.
We can calculate the power of a number in two ways. In the first way, we will use a loop (while loop) and calculate it.
In the second way, we will calculate it with the help of a function , called "pow()".
First way: Calculate with loop
input:
The number(i.e : base) and the power(i.e : exponent)(i.e : 5,6,9 etc.)output:
The result of it's calculation.
CODE---->
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,r=1,n;
clrscr();
printf("Please, Enter a number (base):");
scanf("%d",&n);
printf("\nPlease, Enter it\'s power (exponent):");
scanf("%d",&a);
b=a;
while(a!=0)
{
r=r*n;
a--;
}
printf("\nThe result of %d^%d = %d ",n,b,r);
getch();
}
RESULT:
*Note : The above program will work only if the number and the exponent are positive integers.
Second way: Calculate with function pow()
input:
The number(i.e : base) and the power(i.e : exponent)(i.e : 5,6,9 etc.)output:
The result of it's calculation.
CODE---->
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
float a,r,n;
clrscr();
printf("Please, Enter a number (base):");
scanf("%f",&n);
printf("\nPlease, Enter it\'s power (exponent):");
scanf("%f",&a);
r=pow(n,a);
printf("\nThe result of %f^%f = %f ",n,a,r);
getch();
}
RESULT:
Thank You So Much for your support. We do this for you and we are very much delighted by such appreciation.
ReplyDeleteThank You Sathya.