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 LCM of two numbers. This is the easiest way to calculate LCM for both Negative and Positive numbers. The numbers will be taken from the user.
input:
The numbers.(i.e : 20,25,40 etc.)output:
The LCM of two numbers given by the user.
CODE---->
#include<stdio.h>
#include<conio.h>
main()
{
int copy_num_1,copy_num_2,num_1,num_2,gcd,lcm=0,i ;
printf("Please, Enter two numbers : ");
scanf("%d%d",&num_1,&num_2);
copy_num_1=num_1;
copy_num_2=num_2;
//copying numbers , (entered by user) for future use.
if(num_1<0)
num_1=(num_1)*(-1);
if(num_2<0)
num_2=(num_2)*(-1);
//here, we are converting negative numbers(if any, entered by user), so that, we can calculate the lcm for both positive and negative numbers.
for(i=1;i<=num_1 && i<=num_2;i++)
{
if(num_1%i==0 && num_2%i==0)
gcd=i;
}
lcm=num_1*num_2/gcd;
//relation between lcm and gcd. so, we can get the lcm using this formula.
printf("\nThe LCM of %d and %d is : %d",copy_num_1,copy_num_2,lcm);
getch();
}
RESULT:
Comments
Post a Comment