## Posts

Showing posts with the label Algorithm

### Find number of days in a month in C | Cpp | Java | Go | php

C Program #include<stdio.h> int main() { int m,y; printf("Enter any month, say(1-12): "); scanf("%d",&m); switch(m) { case 1: case 3: case 5: case 7: case 8: case 10: case 12: printf("31 days"); break; case 4: case 6: case 9: case 11: printf("30 days"); break; case 2: printf("Enter the year: "); scanf("%d",&y); if(y%400 == 0 || y%100 != 0 && y%4 == 0) { printf("29 days"); } else { printf("28 days"); } break; default: printf("Invalid input"); } } C++ Program #include<iostream> using namespace std; int main() { int m,y; cout << "Enter any month, say(1-12): "; cin >> m; switch(m) { case 1: case 3: case 5: case 7: case 8: case 10: case 12: cout << "31 days"; break; case 4: case 6: case 9: case 11: cout << "30 day…

### Print Week day program in C | Cpp | Java | php | go

Program Description Program to print weekdays based on the user input, Example: For 0 print Sunday, 1 print Monday and so on. C Program #include<stdio.h> int main() { int day; printf("Enter weekday, say(0-6): "); scanf("%d",&day); switch(day) { case 0: printf("Sunday"); break; case 1: printf("Monday"); break; case 2: printf("Tuesday"); break; case 3: printf("Wednesday"); break; case 4: printf("Thrusday"); break; case 5: printf("Friday"); break; case 6: printf("Saturday"); break; default: printf("Invalid input"); } return 0; } C++ Program #include<iostream&g using namespace std; int main() { int day; cout << "Enter any day, say(0-6): "; cin >> day; switch(day) { case 0: cout << "Sunday"; break; case 1: cout << "Monday"; brea…

### Program to check a year is leap year or not.

Problem DescriptionIf a year is divisible by 4, then it is called leap year.But all the centuries are divisible by 4, so to check a century is a leap year or not, we check divisibility by 400 of a year.If a century is divisible by 400 then the century is called leap year otherwise not. Basic Logic Snippets (In C)if(year % 100 == 0) { if (year % 400 == 0) { printf("%d is a leap year",year); } else { printf("%d is not a leap year",year); } }else { if(year % 4 == 0) { printf("%d is a leap year",year); } else { printf("%d is not a leap year",year); } } OutputTest-1 Enter any year: 1996
1996 is a leap year Test-2 Enter any year: 1600
1600 is a leap year Test-3 Enter any year: 1800
1800 is not a leap year Improved your logicNote: Generate wrong output in some cases, but helpful to understand upcoming logicif(year % 100 == 0 && year % 400 ==0 || year % 4 == 0) { printf("%d is a leap year",year); } else { printf(&qu…

### Program to calculate telephone bill

Program description Program to find the telephone bill as per the following rules -:
Call RangeBill<=100Rs./- 0 per call101-200Rs./- 1 per call201-300Rs./- 0.50 per call301-500Rs./- 0.20 per call>500Rs./- 0.10 per callRental ChargesRs./- 130 C Program#include #define RENTAL 130 int main() { int nc; float bill; printf("Enter the number of calls: "); scanf("%d",&nc); if(nc <= 100) { bill = 0; } else if(nc <= 200) { bill = (nc - 100) * 1 + 0; } else if(nc <= 300) { bill = (nc - 200) * 0.50 + 100 + 0; } else if(nc <= 500) { bill = (nc - 300) * 0.20 + 50 + 100 + 0; } else { bill = (nc - 500) * 0.10 + 40 + 50 + 100 + 0; } //final bill including rental bill += RENTAL; printf("Total bill = %.2f",bill); return 0; } C++ Program#include #define RENTAL 130 using namespace std; int main() { int nc; float bill; cout << "Enter the number of calls: "; cin >> nc; if(nc <= 100) { bill …

### Program to find the max of 4 number

C Program#include int main() { int a,b,c,d,max; printf("Enter any 4 numbers: "); scanf("%d%d%d%d",&a,&b,&c,&d); if(a > b && a > c && a > d) { max = a; } else if(b > c && b > d) { max = b; } else if(c > d) { max = c; } else { max = d; } printf("Max = %d",max); return 0; } C++ Program#include using namespace std; int main() { int a,b,c,d; cout << "Enter any 4 numbers: "; cin >> a >> b >> c >> d; int max; if(a > b && a > c && a > d) { max = a; } else if(b > c && b > d) { max = b; } else if(c > d) { max = c; } else { max = d; } cout << "Max = " << max << endl; return 0; } Java Programimport java.util.Scanner; class Max4 { public static void main(String args[]) { int a,b,c,d; Scanner sc = new Scanner(System.in); System.out.print("…

### Program to find the max of 3 numbers

C Program#include<stdio.h> int main() { int a,b,c,max; printf("Enter any 3 numbers: "); scanf("%d%d%d",&a,&b,&c); if(a > b) { if(a > c) { max = a; } else { max = c; } }else { if(b > c) { max = b; }else { max = c; } } printf("Max = %d",max); } C++ Program#include using namespace std; int main() { int a,b,c; cout << "Enter any 3 numbers: "; cin >> a >> b >> c; int max; if(a > b) { if(a > c) { max = a; } else { max = c; } }else { if(b > c) { max = b; }else { max = c; } } cout << "Max = " << max << endl; } JAvaimport java.util.Scanner; class Max3 { public static void main(String args[]) { int a,b,c; Scanner sc = new Scanner(System.in); System.out.print("Enter any 3 numbers: "); a = sc.nextInt(); b = sc.nextInt(); c = sc.nextInt(); int max; if(a > b…

### Program to find the max of two numbers

C Program#include<stdio.h> int main() { int a,b,max; printf("Enter any two numbers: "); scanf("%d%d",&a,&b); if(a > b) { max = a; } else { max = b; } printf("Max = %d",max); } C++ Program#include<iostream> using namespace std; int main() { int a,b; cout << "Enter any two numbers: "; cin >> a >> b; int max; if (a > b) { max = a; } else { max = b; } cout << "Max = " << max << endl; return 0; } import java.util.Scanner; class Max2 { public static void main(String args[]) { int a,b; Scanner sc = new Scanner(System.in); System.out.print("Enter any two numbers: "); a = sc.nextInt(); b = sc.nextInt(); int max; if(a > b) { max = a; } else { max = b; } System.out.println("Max = "+max); } } package main import "fmt" func main() { var a,b int …

### Program to check a number is even or odd

C Program #include<stdio.h>
int main() {
int n;
printf("Enter any number: ");
scanf("%d",&n);
if(n%2 == 0) {
printf("%d is even",n);
} else {
printf("%d is odd",n);
}
return 0;
} C++ Program #include<iostream>
using namespace std;
int main() {
int n;
cout << "Enter any number: ";
cin >> n;
if(n % 2 == 0) {
cout << n << " is even";
} else {
cout << n << " is odd";
}
return 0;
} Java Program import java.util.Scanner;
class CheckEvenOdd {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter any number: ");
int n = sc.nextInt();
if (n%2 == 0) {
System.out.println(n+" is even");
} else {
System.out.println(n+" is odd");
}
}
} Go Lang package main
import "fmt"
func main() {
var n int
fmt.Print("Enter any number: ")
fmt.Scan(&n)
if n % 2 == 0 {
fmt.Println(n,"is even")
} else {
fmt.Println(n,&q…

### Program to check a number is positive, negative or zero

C Program #include<stdio.h>
int main() {
int n;
printf("Enter any number: ");
scanf("%d",&n);
if( n > 0) {
printf("%d is positive",n);
}
if (n < 0) {
printf("%d is negative",n);
}
if (n == 0) {
printf("%d is zero",n);
}
return 0;
}
C++ Program #include<iostream>
using namespace std;
int main() {
int n;
cout << "Enter any number: ";
cin >> n;
if( n > 0) {
cout << n << " is positive";
}
if (n < 0) {
cout << n << " is negative";
}
if (n == 0) {
cout << n << " is zero";
}
return 0;
} Java Program import java.util.Scanner;
class CheckPositiveNegative {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter any number: ");
int n = sc.nextInt();
if(n > 0) {
System.out.println(n+" is a positive number");
}
if(n < 0) {
System.out.println(n+" is a negative number");
}
if(n == 0) {
Syste…

### Program to compute the distance between two points

Program Description As shown in the above figure, two points p1(x1,x2) and p2(x2,y2) is given as shown in the above figure, The objective of the program to compute the distance of two points p1 and p2. C Program #include<stdio.h> #include<math.h> int main() { float x1,y1,x2,y2,distance; printf("Enter first point x1,y1: "); scanf("%f%f",&x1,&y1); printf("Enter second point x2,y2: "); scanf("%f%f",&x2,&y2); distance = sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1)); printf("Distance = %.2f",distance); return 0; } C++ Program #include<iostream> #include<math.h> using namespace std;
int main() { float x1,y1,x2,y2,distance; cout << "Enter first point x1,y1: "; cin >> x1 >> y1; cout << "Enter second point x2,y2: "; cin >> x2 >> y2; distance = sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1)); cout << "Distance = : " <<distance; return …

### Program to Compute real roots of a quadratic expression

Problem Description Program to find the real roots of a quadratic equation as given below
ax2 + bx + c is a quadratic equation, we need to find the real root for these type of expressions, the formula to find the root is as given below
Where Discriminant of a Quadratic represented by D = b2 – 4ac, which reveals the type of roots as per the following conditions

If D >0, the roots would be realIf D = 0, then both roots would be equalIf D < 0, the roots would be imaginary.
C Program #include<stdio.h>
#include<math.h>
int main() {
int a,b,c;
float d,r1,r2;
printf("Enter 3 constant a,b,c: ");
scanf("%d%d%d",&a,&b,&c);
d = b * b - 4 * a * c;
if (d < 0) {
printf("Roots are imagenary");
}else {
r1 = (b + sqrt(d)) / (2 * a);
r2 = (b - sqrt(d)) / (2 * a);
printf("Root1 = %.2f\nRoot2 = %.2f",r1,r2);
}
return 0;
} C++ program #include<iostream>
#include<math.h>
using namespace std;
int main() {
int a,b,c;
cout << "Enter 3 c…

### Program to Compute Change

Problem ExplainedInput an amount from a user as a decimal number, such as 11.56Conver the amount (e.g, 11.56) as (1156 cents)Calculate the change as per the following rules...
1 Doller100 Cents1 Quarter25 Cents1 Dime10 Cents1 Nickel5 Cents1 Pannie1 Cent
C Programming #include<stdio.h>
int main() {
float amount;
int dollars,quarters,dimes,nickels,cents,pennies;
printf("Enter the amount: ");
scanf("%f",&amount);
cents = (int)(amount * 100);
dollars = cents / 100;
cents = cents % 100;
quarters = cents / 25;
cents = cents % 25;
dimes = cents / 10;
cents = cents % 10;
nickels = cents / 5;
pennies = cents % 5;
printf("Your amount %.2f consists of \n",amount);
printf("%d dollers\n",dollars);
printf("%d quarters\n",quarters);
printf("%d dimes\n",dimes);
printf("%d nickles\n",nickels);
printf("%d pennies\n",pennies);
return 0;
} C++ #include<iostream>
using namespace std;
int main() {
float amount;
cout << "En…

### Program to convert time from seconds to hours, minutes and seconds

C Programming #include<stdio.h>

int main( ) {
int seconds;
int hours,minutes,remainingSeconds;
printf("Enter the time in seconds: ");
scanf("%d",&seconds);

hours = seconds / 3600;
remainingSeconds = seconds % 3600;
minutes = remainingSeconds / 60;
remainingSeconds = remainingSeconds % 60;

printf("%d seconds is %d hours %d minutes %d seconds",seconds,hours,minutes,remainingSeconds);

return 0;
} C++ Programming #include<iostream>
using namespace std;

int main( ) {
int seconds;

cout << "Enter the time in seconds: ";
cin >> seconds;

int remainingSeconds;

int hours = seconds / 3600;
remainingSeconds = seconds % 3600;
int minutes = remainingSeconds / 60;
remainingSeconds = remainingSeconds % 60;

…

### Area of Circle

C Programming #include<stdio.h>
#define PI 3.14156
int main() {
float r,area;

scanf("%f",&r);

area = PI * r * r;

printf("Simple interest = %.2f",area);
return 0;
} C++ Programming #include<iostream>
#define PI 3.14156

using namespace std
int main() {
float r,area;

cin>>r;

area = PI * r * r;

cout << "Area of circle = "<<area<<endl;

return 0;
} Java import java.util.Scanner;

class CircleArea {
public static void main(String args[]) {
final float PI = 3.14156f;

float r,area;

Scanner sc = new Scanner(System.in);
r = sc.nextFloat();

area = PI * r * r;

System.out.println("Area of circle = "+area);
}
} Go Lang package main
import "fmt"

func main() {
cons…

### Swap two numbers

C Programming #include<stdio.h>
int main() {
int a,b,t;
printf("Enter any two numbers: ");
scanf("%d%d",&a,&b);
t = a;
a = b;
b = t;
printf("a = %d \t b = %d",a,b);
} C++ Programming #include<iostream>
using namespace std;
int main() {
int a,b,t;
cout << "Enter any two numbers: ";
cin >> a >> b;
t = a;
a = b;
b = t;
cout << a << "\t" << b;
return 0;
} Java import java.util.Scanner;
class Swap {
public static void main(String args[]) {
int a,b,t;
Scanner sc = new Scanner(System.in);
System.out.print("Enter any two numbers: ");
a = sc.nextInt();
b = sc.nextInt();
t = a;
a = b;
b = t;
System.out.println("a = "+a+" \t b = "+b);
}
} Go Lang package main
import "fmt"
func main() {
var a,b,t int32
fmt.Print("Enter any two numbers: ")
fmt.Scan(&a,&b)
t = a
a = b
b = t
fmt.Println(a," \t ",b)
} PHP <?php
if(isset(\$_POST['sub'])) {
\$a = \$_POST[&#…

### Calculate simple interest

C Programming #include<stdio.h>
int main() {
int p,t;
float r,si;
printf("Enter principal,time and rate: ");
scanf("%d%d%f",&p,&t,&r);
si = p * t * r / 100;
printf("Simple Interest = %.2f",si);
return 0;
} C++ Programming #include<iostream>
using namespace std;
int main() {
int p,t;
float r;
cout << "Enter principal, time and rate: ";
cin >> p >> t >> r;
float si = p * t * r / 100;
printf("Simple Interest = %.2f",si);
return 0;
} Java Programming import java.util.Scanner;
class SimpleInterest {
public static void main(String args[]) {
int p,t;
float r,si;
Scanner sc = new Scanner(System.in);
System.out.print("Enter principal, time and rate: ");
p = sc.nextInt();
t = sc.nextInt();
r = sc.nextFloat();
si = p * t * r / 100;
System.out.println("Simple interest = "+si);
}
} Go lang package main
import "fmt"
func main() {
var p,t int32
var r float32
fmt.Print("Enter principal, time and …

### Sum of two numbers

C Programming #include<stdio.h>
int main() {
int a,b,sum;
printf("Enter any two numbers: ");
scanf("%d%d",&a,&b);
sum = a + b;
printf("Total = %d",sum);
return 0;
} C++ Programming #include<iostream>
using namespace std;
int main() {
int a,b;
cout << "Enter any two numbers: ";
cin >> a >> b;
int sum = a + b;
cout<<"Total = "<<sum;
return 0;
} Java import java.util.Scanner; class Sum { public static void main(String args[]) { int a,b; Scanner sc = new Scanner(System.in); System.out.print("Enter any two numbers: "); a = sc.nextInt(); b = sc.nextInt(); int sum = a + b; System.out.print("Total = "+sum); } } Go Lang package main
import "fmt"
func main() {
var a,b int32
fmt.Print("Enter any two numbers: ")
fmt.Scan(&a,&b)
sum := a + b
fmt.Print("Total = ",sum)
} php index.php <!doctype html>
<html>
<title>Sum of two numbers</tit…

### Say Hello World

C programming #include<stdio.h>
int main() {
printf("Hello World");
return 0;
} C++ Programming #include<iostream>
using namespace std;
int main() {
cout << "Hello World";
return 0;
}  Java Programming class HelloWorld {
public static void main(String args[]) {
System.out.println("Hello, World");
}
}  Go Lang package main
import "fmt"
func main() {
fmt.Print("Hello World")
} php <?php
echo "Hello world";
?>