ViijayScript: 10/15/17

Sunday 15 October 2017

#Program to #Check #Leap #Year in #C #C++ C# #Java #Python #Updated on 2017

Hello netizens, On this post I am going to tell #Programs to #check #leap #year in #C #C++ #Java C# #Python.

Program in C:

#include <stdio.h>
int main()
{
int year;
printf(“Enter a year to check if it is a leap year\n”);
scanf(“%d”, &year);
if ( year%100 == 0)
{
if ( year%400 == 0)
printf(“%d is a leap year.\n”, year);
else
printf(“%d is not a leap year.\n”, year);
}
else if ( year%4 == 0 )
printf(“%d is a leap year.\n”, year);
else
printf(“%d is not a leap year.\n”, year);
return 0;
}

Program in C++:

#include <iostream>
using namespace std ;
int main ()
{
int year ;
cout <> year ;
if ( ( year % 400 == 0 || year % 100 != 0 ) &&( year % 4 == 0 ))
cout << "It is a leap year" ;
else
cout << "It is not a leap year" ;
return 0 ;
}

Program in Java:

public class LeapYear { public static void main(String[] args) {
int year = Integer.parseInt(args[0]);
if ( year%100 == 0)
{
if ( year%400 == 0)
System.out.println(year+“ is a leap year."+year);
else
System.out.println(year+“ is not a leap year.”+year);
}
if( year%4 == 0 )
System.out.println(year+“is a leap year.”+year);
else
System.out.println(year+“ is not a leap year.”+year);
}
}

Program in C#:

1. /*
2. C# Program to Check Whether the Entered Year is a Leap Year or Not
3. */
4. using System ;
5. using System.Collections.Generic ;
6. using System.Linq ;
7. using System.Text ;
8.
9. namespace Program
10. {
11. class leapyear
12. {
13. static void Main (string [] args )
14. {
15. leapyear obj = new leapyear ();
16. obj.readdata ();
17. obj.leap ();
18. }
19. int y ;
20. public void readdata ()
21. {
22. Console.WriteLine ( “Enter the Year in Four Digits : ” );
23. y = Convert .ToInt32 ( Console.ReadLine ());
24. }
25. public void leap ()
26. {
27. if (( y % 4 == 0 && y % 100 != 0 ) || ( y
% 400 == 0 ))
28. {
29. Console . WriteLine (“{0} is a Leap Year” , y );
30. }
31. else
32. {
33. Console . WriteLine (“{0} is not a Leap Year” , y );
34. }
35. Console.ReadLine ();
36. }
37. }
38. }

Program in Python:

def IsLeapYear(year):
if((year % 4) == 0):
if((year % 100) == 0):
if( (year % 400) == 0):
return 1
else:
return 0
else:
return 1
return 0 n = 0
print “Program to check Leap Year”
print “Enter Year: “, n = input()
if( IsLeapYear(n) == 1):
print n, “is a leap year”
else:
print n, “is NOT a leap year”

If our post is helpful then share it with your near and dear ones.

#Program to #find #HCF and #LCM in #C #C++ #Java C# #Python #Updated on 2017

Hello netizens, On this post I am going to tell #Programs to #find #LCM and #HCF of a #number in #C #C++ #Java C# #Python.

Program in C:

#include <stdio.h>
long gcd(long, long);
int main() {
long x, y, hcf, lcm;
printf(“Enter two integers\n”);
scanf(“%ld%ld”, &x, &y);
hcf = gcd(x, y);
lcm = (x*y)/hcf;
printf(“Greatest common divisor of %ld and %ld = %ld\n”, x, y, hcf);
printf(“Least common multiple of %ld and %ld = %ld\n”, x, y, lcm);
return 0;
}
/ if 1st no is 0 then 2nd no is gcd
make 2nd no 0 by subtracting smallest from largest and return 1st no as gcd /
long gcd(long x, long y) {
if (x == 0) {
return y;
}
while (y != 0) {
if (x > y) {
x = x – y;
}
else {
y = y – x;
}
}
return x;
}

Program in C++:

#include <iostream>
#include <string>
using namespace std;
int main()
{
int a,b,c;
cout<< "Enter two nos :" <<endl;
cout<<endl;
cout<>a;
cout<>b;
c=a*b;
while(a!=b)
{
if (a>b)
a=a-b;
else
b=b-a;
}
cout<< "HCF = " << a<<endl;
cout<< "LCM = " << c/a<<endl;
return 0;
}

Program in Java:

import java.util.Scanner;
public class JavaProgram
{
public static void main(String args[])
{
int a, b, x, y, t, hcf, lcm;
Scanner scan = new Scanner(System.in);
System.out.print(“Enter Two Number : “);
x = scan.nextInt();
y = scan.nextInt();
a = x;
b = y;
while(b != 0)
{
t = b;
b = a%b;
a = t;
}
hcf = a;
lcm = (x*y)/hcf;
System.out.print(“HCF = ” +hcf);
System.out.print(“\nLCM = ” +lcm);
}
}

Program in C#:

1. using System ;
2. using System .Text ;
3.
4. namespace forgetCode
5. {
6.
7. class program
8. {
9. public static void Main ()
10. {
11. int a , b , x , y , t , gcd , lcm ;
12. Console. WriteLine (“Enter two integers\n” );
13. x = Convert .ToInt32 ( Console.ReadLine
());
14. y =
Convert .ToInt32 ( Console.ReadLine ());
15.
16. a = x ;
17. b = y ;
18.
19. while (b != 0 )
20. {
21. t = b ;
22. b = a % b ;
23. a = t ;
24. }
25.
26. gcd = a ;
27. lcm = ( x * y ) / gcd ;
28. Console. WriteLine (“Greatest common divisor of {0} and {1}= {2}\n” , x , y , gcd);
29. Console. WriteLine (“Least common multiple of{0} and {1}= {2}\n” , x , y , lcm );
30.
31.
32. }
33. }
34. }

Program in Python:

d1 = int ( raw_input ( “Enter a number:”))
d2 = int ( raw_input ( “Enter another number”))
rem = d1 % d2
while rem!=0:
d1=d2
d2 = rem
rem = d1 % d2
print “gcd of given numbers is : %d” %(d2)
x = int(input(“Enter first number: “))
y= int(input(“Enter second number: “))
if x>y:
g=x
else:
g=y
while(True):
if((g % x ==0) and (g % y==0)):
lcm=g
break
g += 1
print “L.C.M is “,lcm

If our post is helpful then share it with your near and dear ones.

#Programs to #Convert #Celsius to #Fahrenheit in #C #C++ #Java C# #Python #Updated on 2017

Hello netizens, On this post I am going to tell #Programs to #Convert #Celsius to #Fahrenheit in #C #C++ #Java C# #Python.

Program in C:

#include <stdio.h>
#include<conio.h>
void main()
{
float c,f;
/*To convert Centigrade to Fahrenheit*/
printf(“Enter the temperature in centigrade:”);
scanf(“%f”,&c);
f=1.8*c+32;
printf(“\n\t%.2f Centigrade=%.2f Fahrenheit”,c,f);
/*To convert Fahrenheit to centigrade*/
printf(“\n\nEnter the temperature in Fahrenheit:”);
scanf(“%f”,&f);
c=(f-32)/1.8;
printf(“\n\t%.2f Fahrenheit=%.2f Centigrade”,f,c);
getch();
}

Program in C++:

# include<iostream>
using namespace std;
void main()
{
int choice;
float f, c;
cout<<“To convert your temperature from Celsius to Fahrenheit press 1:”<<endl;
cout<<“To convert your temperature from Fahrenheit to Celsius press 2:”<<endl;
cout<<“Now Enter Your Choice: “;
cin>>choice;
if(choice==1)
{
cout<<“enter your temperature in Celsius : “;
cin>>c;
f = (9.0/5.0)*c + 32;
cout<<“Your temperature in Fahrenheit is = “<<f<<“\n”;
}
else if(choice==2)
{
cout<<“Enter Your Temperature In Fahrenheit : “;
cin>>f;
c = (5.0/9.0)*(f-32.0);
cout<<“Your temperature in Celsius is = “<<c<<“\n”;
}
}

Program in Java:

import java.io.*;
class CONVERSION_OF_TEMPERATURE
{
private double fahrenheit,celcius;
public static void main(String args[])throws IOException
{
System.out.println(” THIS PROGRAM IS FOR CONVERSION OF TEMPERATURE FROM .”);
CONVERSION_OF_TEMPERATURE obj=new CONVERSION_OF_TEMPERATURE();
InputStreamReader reader=new InputStreamReader(System.in);
BufferedReader input=new BufferedReader(reader);
System.out.println(“ENTER YOUR CHOICE”);
System.out.println(“1> FAHRENHEIT TO CELCIUS CONVERSION”);
System.out.println(“2> CELCIUS TO FAHRENHEIT CONVERSION”);
int c=Integer.parseInt(input.readLine());
switch(c)
{
case 1:
{
System.out.println(“ENTER THE TEMPERATURE IN FAHRENHEIT”);
System.out.print(“USER INPUT : “);
try
{
obj.fahrenheit=Double.parseDouble(input.readLine());
}
catch(Exception b)
{
System.out.println(“”);
System.out.println(“\t\tERROR FOUND : YOU HAVE TO ENTER ONLY INTEGERS PLEASE RERUN THE PROGRAM”);
System.exit(0);
}
obj.celcius=5/9.0*(obj.fahrenheit-32);
System.out.println(“”);
System.out.println(“THE TEMPERATURE IN CELCIUS IS “+obj.celcius);
break;
}
case 2:
{
System.out.println(“ENTER THE TEMPERATURE IN CELCIUS”);
System.out.print(“USER INPUT : “);
try
{
obj.celcius=Double.parseDouble(input.readLine());
}
catch(Exception b)
{
System.out.println(“”);
System.out.println(“\t\tERROR FOUND : YOU HAVE TO ENTER ONLY INTEGERS PLEASE RERUN THE PROGRAM”);
System.exit(0);
}
obj.fahrenheit=1.8*(obj.celcius+ 32);
System.out.println(“”);
System.out.println(“THE TEMPERATURE IN FAHRENHEIT IS “+obj.fahrenheit);
break;
}
default:
{
System.out.println(“ENTERED WRONG DATA THE PROGRAM WILL TERMINATE”);
}
}
}
}

Program in C#:

1. using System;
2. using System.Collections.Generic;
3. using System.Linq;
4. using System.Text;
5. namespace program
6. {
7. class Program
8. {
9. static void Main(string[] args)
10. {
11. int celsius, faren;
12. Console.WriteLine("Enter the Temperature in Celsius(°C) : ");
13. celsius = int.Parse(Console.ReadLine
());
14. faren = (celsius * 9) / 5 + 32;
15. Console.WriteLine("0Temperature in Fahrenheit is(°F) : " + faren);
16. Console.ReadLine();
17. }
18. }
19. }

Program in Python:

Script to convert Celsius to Fahrenheit:
celsius = float(raw_input(“Enter the temperature in Celsius: “));
Fahrenheit = (9.0/5.0)*celsius + 32.0;
print ‘The temperature in Fahrenheit:’, Fahrenheit;
Script to convert Fahrenheit to Celsius:
Fahrenheit = float(raw_input(“Enter the temperature in Fahrenheit: “));
Celsius = (5.0/9.0)*(Fahrenheit – 32.0);
print ‘The temperature in Celsius:’, Celsius;

If our post is helpful then please share it with your dear and near ones.

Scan this QR Code on LinkedIn

Scan this QR Code on LinkedIn