Posts

Program to Compute real roots of a quadratic expression

Image
Problem Description Program to find the real roots of a quadratic equation as given below ax 2 + 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 = b 2  – 4ac, which reveals the type of roots as per the following conditions If D >0, the roots would be real If D = 0, then both roots would be equal If 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.

Program to Compute Change

Image
Problem Explained Input an amount from a user as a decimal number, such as 11.56 Conver the amount (e.g, 11.56) as (1156 cents) Calculate the change as per the following rules... 1 Doller 100 Cents 1 Quarter 25 Cents 1 Dime 10 Cents 1 Nickel 5 Cents 1 Pannie 1 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",

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

Image
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

Area of Circle

Image
C Programming #include<stdio.h> #define PI 3.14156 int main() {     float r,area;         printf("Enter the radius: ");     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;         cout<<"Enter the radius: ";     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);         System.out.print("Enter the radius: ");         r = sc.nextFloat();                 area = PI * r * r;                 System.out.println(&

Swap two numbers

Image
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: &q

Calculate simple interest

Image
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 =

Sum of two numbers

Image
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

Say Hello World

Image
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"; ?>  

Printing in C programming

Image
Printing in C In C for printing purpose, We Use A Function Called printf( ) . A function is a small program used to perform a specific task. The function receives some input known as an argument and produces some output called return value. Functions are always suffixed with parenthesis ( ). Syntax of printf( ) printf("Control String",[List or Variable]); Note: Control string is a sequence of characters which you want to print. ([ ]) square brackets around the list or variable represent that it is optimal. Example Of printf( ) printf("hello"); //Print hello int a=5,b=6; printf("a=%d b=%d",a,b); //print the value of a and b. int a=5,b=6; //Varialbe Declaration printf("%d",a+b); //Print 11. Note: In printf( ) expression can be written .The example is given above. The first program, Say Hello World in C programming /*program to print the "Hello world"*/ #include<stdio.h> int main() { printf(

Arrays in Go lang

Image
What is Array An array is the collection of similar type of elements. An array is the collection of fixed length. Array elements are stored in contiguous memory locations. All the array elements share the common name, but each element is uniquely identified their index or subscript, so the array is also called a subscript variable. Array index always starts with 0. Creating an array var arr [5]int Note:  In the above program snippet, arr is an array of 5 integers. By default, all the array elements are initialized with a default value. Default Values Data Type Default Value Int(s) 0 Float(s) 0.0 bool(s) false Objects nil Note: Following example demonstrate the default values for array elements. Example-1 package main import "fmt" func main() { var arr1 [5]int var arr2 [5]float32 var arr3 [5]string var arr4 [5]bool fmt.Pr

Decision Making in Go lang

Image
Introduction to decision making Sometimes we need to introduce a power in our program, that our program should automatically make the decision. For example:  if the temperature of the day is more than 20 degree then it's hot otherwise it's cool. To introduce this kind of power in our program, we use decision making statements as the list below... if-else statements nested if-else statements else-if ladder switch/case-control statement Let's create a program to check whether a number is even or odd. Program to check a number is even or odd in Go lang. package main import "fmt" func main() { var n int32 fmt.Print("Enter any number:") fmt.Scan(&n) if n % 2 == 0 { fmt.Println(n,"is even") } else { fmt.Println(n,"is odd") } } Output: Enter any number:55 55 is odd  Explanation: A number is even if it is divisible by 2 otherwise odd. So to che

History of C programming language

History of C C is a general-purpose programming language initially developed by the  Dennis Ritchie  between 1969 to 1973 at AT & T (American Telegraph and telecommunication) Bell Labs. It is named  C  because its features are derived from the earlier language called  B  which is developed by the  Ken Thomson . According to Ken Thomson  B  was the stripped-down version of BCPL (Basic combined programming Language). The origin of c is closely tied up with the Unix operating system development. In 1973 with the addition of  struct  type the c becomes powerful enough that the Unix operating system was designed in C. This was one of the first operating system kernels implemented in a language other than assembly. In 1978, Brian Kernighan and Dennis Ritchie published the first edition of  The C Programming Language. This book, known to C programmers as "K&R", served for many years as an informal specification of the language. The vers

Looping in go lang

Image
Loop is a structure with the block of statements can be repeated for the finite number of times.  Till Now we have learned the programs that execute sequentially, Now we are going to learn control structures which change the normal flow of the program and helps you to create more meaningful and compact program structure. To understand more, suppose we want to print from 1 to 10 number counting, each number should be printed in the new line. Program to print numbers from 1 to 10 package main import "fmt" func main( ) { fmt.Println(1) fmt.Println(2) fmt.Println(3) fmt.Println(4) fmt.Println(5) fmt.Println(6) fmt.Println(7) fmt.Println(8) fmt.Println(9) fmt.Println(10) } Another compact way to create this program use loop. The following example creates this program using the loop. Program package main import "fmt" func main() { i := 1 //initialization for i <

Convert Temperature from Fahrenheit to centigrade

The formula to convert temperature from Fahrenheit to Centigrade is as below... C = ( F - 32 ) * 5 / 9 where C and F are temperature in Centigrade and Fahrenheit in respectively. package main import "fmt" func main() { var f float32 fmt.Print("Enter the temperature in fahrenheit: ") fmt.Scan(&f) c := ( f - 32 ) * 5 / 9 fmt.Printf("Temprature in centigrade: %.2f",c) }

Constant in Go lang

Image
Constant in go lang Constants are basically variable which value can't change once created. Constants are created in the same way as variables, the only difference is that to create a variable use var keyword and to create constant use const keyword. Constant Example-1 package main import "fmt" func main() { const playerName string = "Rahul" fmt.Println(playerName) //playerName = "Micky" //compile time error, cannot assign to playerName. } Constant Example-2 package main import "fmt" func main() { const METER_PER_FEET = 0.3048 var feet,meter float32 fmt.Print("Enter value in feet...") fmt.Scan(&feet) meter = feet * METER_PER_FEET fmt.Printf("Meter %.2f",meter) }  Note: In the above example, METER_PER_FEET is a constant. It is suggested that constant name should be written in uppercase, each word separated by an unders

Popular posts from this blog

String in golang

Inline V/S Block Level Element

Floating point Data types in Go lang

Escape Sequence | Formatted Printing | Interview Questions

Sum of two numbers

Operators in C Language| Part-4

Printing in C programming

Arrays in C Language

Program to check a year is leap year or not.

Data types in Java