Basic program - coderz.py https://coderzpy.com Keep Coding Keep Cheering! Fri, 25 Nov 2022 12:31:31 +0000 en-US hourly 1 https://wordpress.org/?v=6.9.4 https://coderzpy.com/wp-content/uploads/2022/08/cropped-image1-1-32x32.jpg Basic program - coderz.py https://coderzpy.com 32 32 C Program – Structure https://coderzpy.com/c-program-structure/ https://coderzpy.com/c-program-structure/#respond Thu, 03 Feb 2022 08:17:40 +0000 http://coderzpy.com/?p=1140 Before we begin with our basic concepts of the C programming language let's take a look at the C Program -Structure of a simple code for better understanding.

The post C Program – Structure first appeared on coderz.py.

]]>
Before we begin with our basic concepts of the C programming language let’s take a look at the C Program -Structure of a simple code for better understanding.

A C program consists of the following components−

  • standard input-output library functions
  • variables
  • comments
  • statements
  • operators
  • operands
  • functions
Hello World Program
#include <stdio.h>    
    1. int main()
    2.  {    
    3. /* first program in C */
    4. printf("Hell, World!");    
    5. return 0;   
    6. }  

Description of the above code:

  • here the first line of the code #include <stdio.h> is the standard input-output library. printf() function is defined in the stdio.h library file.
  • int main() is the main function where the execution of the code begins.
  • The opening curly brace { symbolizes the beginning of the block.
  • The statement inside /*…*/ is the comment which is ignored by the compiler.
  • The print() function prints the message “Hello World” on the output window/screen.
  • The return 0 statement is the ending of the code, as well as returns the execution state of the OS.
  • The closing curly brace } symbolizes the end of the block.
Compiling and Executing a C Program

Let us take a look at how to compile and run a program:

  • If you are using a C code editor, you may directly click on the compile and run button resulting in the compilation of the program and an output will be generated on the screen.
  • Otherwise, if you are using the command prompt then follow the below-given methods.
  • open an editor and type your code in it
  • save the file with the “.c” extension.
  • open the command prompt go to the directory where you saved your c file
  • type gcc filename.c -o filename and press enter.
Output
$ gcc filename.c -o filename
$ ./filename
Hello, World!

Make sure the gcc compiler is in your path and that you are running it in the directory containing the source file filename.c.

Now you might have a good understanding of the C Program -Structure.

Note: Also read C Programming Language Overview C Programming Language

The post C Program – Structure first appeared on coderz.py.

]]>
https://coderzpy.com/c-program-structure/feed/ 0
Simple Hello World Program https://coderzpy.com/simple-hello-world-program/ https://coderzpy.com/simple-hello-world-program/#respond Tue, 19 Apr 2022 18:09:19 +0000 http://coderzpy.com/?p=1411 Requirement for Java Hello World Program: Install the JDK if you don’t have installed it, download the JDK and install it. Set ...

The post Simple Hello World Program first appeared on coderz.py.

]]>
Requirement for Java Hello World Program:
Hello World Example:
class Hello{  
    public static void main(String args[]){  
     System.out.println("Hello World! Coderzpy");  
    }  
}  

Save the above file as Hello.java.

In the terminal:

To compile:javac Hello.java
To execute:java Hello
Output:
Hello World! Coderzpy
Parameters used:

Let’s look at what class, public, static, void, main, String[], and System.out.println mean ().

  • In Java, the class keyword is used to declare a class.
  • The visibility is represented by the public keyword, which is an access modifier. It means that it is visible to everyone.
  • static is a keyword. Any method that is declared as static is referred to as a static method. The main advantage of the static method is that it does not require the creation of an object to use. Because the main() method is executed by the JVM, there is no need to create an object to call it. As a result, it conserves memory.
  • The method’s return type is void. It indicates that it does not return any information.
  • The program’s starting point is represented by main.
  • The command line argument is String[] args or String args[]. We’ll go over it in the next section.
  • To print a statement, use System.out.println(). System is a class, out is a PrintStream class object, and println() is a PrintStream class method. In the following section, we’ll look at how the System.out.println() statement works internally.
Compilation Flow:

The Java compiler converts source code into byte code when we compile a Java program with the javac tool.

hello world

Note: We can also use other editors like the eclipse(https://www.eclipse.org/downloads/), also read about the JDK vs JRE vs JVM

Follow Me

If you like my post please follow me to read my latest post on programming and technology.

https://www.instagram.com/coderz.py/

https://www.facebook.com/coderz.py

The post Simple Hello World Program first appeared on coderz.py.

]]>
https://coderzpy.com/simple-hello-world-program/feed/ 0
Association, Composition & Aggregation in Java https://coderzpy.com/association-composition-aggregation-in-java/ https://coderzpy.com/association-composition-aggregation-in-java/#respond Sat, 21 May 2022 01:21:47 +0000 http://coderzpy.com/?p=1531 Association: Association is a relation between two separate classes which establishes through their Objects. One-to-one, one-to-many, many-to-one, and many-to-many associations ...

The post Association, Composition & Aggregation in Java first appeared on coderz.py.

]]>
Association:
  • Association is a relation between two separate classes which establishes through their Objects. One-to-one, one-to-many, many-to-one, and many-to-many associations are all possible.
  • An Object communicates with another object to use its functionality and services in Object-Oriented programming.
  • Composition and Aggregation are the two forms of association.
Composition:
  • The strongest type of association is composition.
  • It represents part-of relationship.
  • If an object owns another object and the other object cannot exist without the owner object, the association is said to be composition. 
Aggregation:
  • Aggregation is a weak association.
  • An association is said to be aggregation if both Objects can exist independently.
  • It represents Has-A’s relationship.
Aggregation
Aggregation example:
class Operation{  
 int square(int n){  
  return n*n;  
 }  
}  
  
class Circle{  
 Operation op;//aggregation  
 double pi=3.14;  
    
 double area(int radius){  
   op=new Operation();  
   int rsquare=op.square(radius);//code reusability (i.e. delegates the method call).  
   return pi*rsquare;  
 }  
  
     
    
 public static void main(String args[]){  
   Circle c=new Circle();  
   double result=c.area(5);  
   System.out.println(result);  
 }  
}  
Output:
78.5

We’ve created a reference to the Operation class in the Circle class in this example.

Composition Example:
import java.io.*;   
import java.util.*;   
// class College  
class College {   
    public String name;   
    public String address;   
    College(String name, String address)   
    {   
        this.name = name;   
        this.address = address;   
    }   
}   
// University has more than one college.   
class University {   
    // reference to refer to list of college.   
    private final List<College> colleges;   
    University(List<College> colleges)  
    {  
        this.colleges = colleges;  
    }   
    // Getting total number of colleges  
    public List<College> getTotalCollegesInUniversity()   
    {   
        return colleges;   
    }   
}   
class CompositionExample {   
    public static void main(String[] args)   
    {   
        // Creating the Objects of College class.   
        College c1   
            = new College("XYZ Engineering College", "Kolkata");   
        College c2   
            = new College("ABC Engineering College", "Delhi");   
        College c3 = new College("Coderz College of Engineering & Management Sudies",   
                           "Bombay");   
        // Creating list which contains the no. of colleges.   
        List<College> college = new ArrayList<College>();   
        college.add(c1);   
        college.add(c2);   
        college.add(c3);   
        University university = new University(college);   
        List<College> colleges = university.getTotalCollegesInUniversity();   
        for (College cg : colleges) {   
            System.out.println("Name : " + cg.name   
                               + " and "  
                               + " Address : " + cg.address);   
        }   
    }   
}  
Output:
Name : XYZ Engineering College and  Address : Kolkata
Name : ABC Engineering College and  Address : Delhi
Name : Coderz College of Engineering & Management Sudies and  Address : Bombay

We create the College class, which contains variables such as name and address. We also create a class called University, which has a reference to the college list. There can be multiple collages at a university. As a result, if a university closes permanently, all colleges within that university must close as well, because colleges cannot exist without a university. Composition is the relationship between the university and the colleges.

Note: also read about the Inheritance in Java

Follow Me

If you like my post please follow me to read my latest post on programming and technology.

https://www.instagram.com/coderz.py/

https://www.facebook.com/coderz.py

The post Association, Composition & Aggregation in Java first appeared on coderz.py.

]]>
https://coderzpy.com/association-composition-aggregation-in-java/feed/ 0
Hello World program https://coderzpy.com/hello-world-program/ https://coderzpy.com/hello-world-program/#respond Fri, 04 Nov 2022 19:01:03 +0000 http://coderzpy.com/?p=2013 Program code: Output: #include<iostream>: A number sign (#) at the start of a line instructs the compiler’s pre-processor. The <iostream> ...

The post Hello World program first appeared on coderz.py.

]]>
Program code:

#include<iostream>

using namespace std;

int main()
{
    cout << "Hello World! \n Coderzpy.com!!\n\n";
    return 0;
}
Output:
Hello World! 
Coderzpy.com!!
  • #include<iostream>: A number sign (#) at the start of a line instructs the compiler’s pre-processor. The <iostream> header defines the libraries required to accept and output data.
  •  using namespace std: This instructs the compiler to use the standard (std) namespace, which includes C++ Standard Library features.
  • int main(): C++, like any other programming language, begins program execution with the main function: int main ()
  • cout Statement: cout is used to print output to the screen. (This is similar to the printf statement in C.) The command cin >> is used to accept input from the terminal. (This is analogous to the scanf statement in C.)
  •  return 0: The return type of the main method is int. As a result, in order to terminate the program, we must return 0.

Note: also read about Namespaces in C++

Follow Me

Please follow me to read my latest post on programming and technology if you like my post.

https://www.instagram.com/coderz.py/

https://www.facebook.com/coderz.py

The post Hello World program first appeared on coderz.py.

]]>
https://coderzpy.com/hello-world-program/feed/ 0
Adding two numbers https://coderzpy.com/adding-two-numbers/ https://coderzpy.com/adding-two-numbers/#respond Fri, 04 Nov 2022 19:13:47 +0000 http://coderzpy.com/?p=2016 Program code: Output: where, int sum=0: A variable can be initialized during the declaration process. This is usually done to ...

The post Adding two numbers first appeared on coderz.py.

]]>
Program code:

#include<iostream>

using namespace std;

int main()
{
    //variable declaration
    int x, y;

    //variable declaration and initialization  
    int sum=0;
    //take user input
    cout << "Enter the first Number : ";
    cin >> x;
    cout << "\nEnter the second Number : ";
    cin >> y;

    //Adding the two numbers
    sum = x + y;

    //displaying the final output (sum)
    cout << "\nAddition of the two numbers is : " << sum;

    return 0;
}
Output:
Enter the first Number : 45
Enter the second Number : 12
Addition of the two numbers is : 57

where,

  • int sum=0: A variable can be initialized during the declaration process. This is usually done to ensure that the value to be used in the future is not set to some random value at the start.
  • sum = x + y: The addition operation is performed by this statement.

Note: also read about Hello World program

Follow Me

Please follow me to read my latest post on programming and technology if you like my post.

https://www.instagram.com/coderz.py/

https://www.facebook.com/coderz.py

The post Adding two numbers first appeared on coderz.py.

]]>
https://coderzpy.com/adding-two-numbers/feed/ 0
Even Or Odd number check https://coderzpy.com/even-or-odd-number-check/ https://coderzpy.com/even-or-odd-number-check/#respond Fri, 04 Nov 2022 19:40:40 +0000 http://coderzpy.com/?p=2018 A number is even if it is divisible by two, and odd if it is not divisible by two. Some ...

The post Even Or Odd number check first appeared on coderz.py.

]]>
A number is even if it is divisible by two, and odd if it is not divisible by two.

Some of the even numbers are −

2, 4, 6, 8, 10, 12, 14, 16

Some of the odd numbers are −

1, 3, 5, 7, 9, 11, 13, 15, 17

program code:

#include <iostream>
using namespace std;
int main() 
{
    int num=0;
   //taking user input
    cout << "Enter any non-zero Number to be checked: ";
    cin >> num;
    
   if(num % 2 == 0)
   {
        cout<<num<<" is even";
   }
   else
   {
       cout<<num<<" is odd";
   }
   return 0;
}
Output:
Enter any non-zero Number to be checked: 34
34 is even

In the code, we use the if-else block to determine whether the entered number is even or odd.

Note: also read about Adding two numbers

Follow Me

Please follow me to read my latest post on programming and technology if you like my post.

https://www.instagram.com/coderz.py/

https://www.facebook.com/coderz.py

The post Even Or Odd number check first appeared on coderz.py.

]]>
https://coderzpy.com/even-or-odd-number-check/feed/ 0
Program to Find ASCII Value of a Character https://coderzpy.com/program-to-find-ascii-value-of-a-character/ https://coderzpy.com/program-to-find-ascii-value-of-a-character/#respond Fri, 04 Nov 2022 19:52:19 +0000 http://coderzpy.com/?p=2020 In C++ programming, a character variable stores an ASCII value (an integer number between 0 and 127) rather than the character ...

The post Program to Find ASCII Value of a Character first appeared on coderz.py.

]]>
In C++ programming, a character variable stores an ASCII value (an integer number between 0 and 127) rather than the character itself. This is known as the ASCII value.

For instance, the ASCII value of ‘A’ is 65.

Therefore, to find the ASCII value of the given Character, we make use of the concept of Type-Casting.

program code:

#include <iostream>

using namespace std;

int main()
{
    //variable declaration 
    char c;
    //take user input
    cout << "Enter a character : ";
    cin >> c;

    //printing the ASCII value of the entered character
    cout << "\nThe ASCII value of the entered character is : " << (int)c << "\n\n";

    return 0;
}
Output:
Enter a character : T
The ASCII value of the entered character is : 84

Note: also read about Even Or Odd number check

Follow Me

Please follow me to read my latest post on programming and technology if you like my post.

https://www.instagram.com/coderz.py/

https://www.facebook.com/coderz.py

The post Program to Find ASCII Value of a Character first appeared on coderz.py.

]]>
https://coderzpy.com/program-to-find-ascii-value-of-a-character/feed/ 0
Sum Of Series 1 + 2 + 3 + 4 + 5 + 6 . . . . n https://coderzpy.com/sum-of-series-1-2-3-4-5-6-n/ https://coderzpy.com/sum-of-series-1-2-3-4-5-6-n/#respond Fri, 04 Nov 2022 20:04:00 +0000 http://coderzpy.com/?p=2022 The sum of a series consisting of n terms beginning with “1,” is equivalent to the sum of n natural numbers ...

The post Sum Of Series 1 + 2 + 3 + 4 + 5 + 6 . . . . n first appeared on coderz.py.

]]>
The sum of a series consisting of n terms beginning with “1,” is equivalent to the sum of n natural numbers beginning with 1. There are numerous approaches to solving the same problem, but the ones listed below are the most commonly used by coders.

Program code:

#include<iostream>
using namespace std;
int sumseries(int num)
{
    int sum=0;
    sum=num*(num+1)/2;
    return sum;
}

int main()
{
    int n;
    cout<<"Enter the value of n : ";
    cin>>n;
    cout<<sumseries(n);
    return 0;
}
Output:
Enter the value of n : 6
21

As one can see in the above code, we directly used the formula for the sum of ‘n’ natural numbers.

Note: also read about Program to Find ASCII Value of a Character

Follow Me

Please follow me to read my latest post on programming and technology if you like my post.

https://www.instagram.com/coderz.py/

https://www.facebook.com/coderz.py

The post Sum Of Series 1 + 2 + 3 + 4 + 5 + 6 . . . . n first appeared on coderz.py.

]]>
https://coderzpy.com/sum-of-series-1-2-3-4-5-6-n/feed/ 0
Sum Of Series 1 + 1 / 2 ^ 2 +. . . . 1 / n ^ n https://coderzpy.com/sum-of-series-1-1-2-2-1-n-n/ https://coderzpy.com/sum-of-series-1-1-2-2-1-n-n/#respond Fri, 04 Nov 2022 20:18:58 +0000 http://coderzpy.com/?p=2025 A program to compute the sum of a given series 1 + 1/2^2 + 1/3^3 + …..+ 1/n^n. For this, ...

The post Sum Of Series 1 + 1 / 2 ^ 2 +. . . . 1 / n ^ n first appeared on coderz.py.

]]>
A program to compute the sum of a given series 1 + 1/2^2 + 1/3^3 + …..+ 1/n^n.

For this, we will be given the value of n, and our task will be to add up each term, beginning with the first, to find the sum of the given series.

Program code:

#include <iostream>
#include <math.h>
using namespace std;
//calculating the sum of the series
double calc_sum(int n) 
{
   int i;
   double sum = 0.0;
   for (i = 1; i <= n; i++)
   sum += 1/ pow(i, i);
   return sum;
}
int main() 
{
   int n;
    cout<<"Enter the value of n : ";
    cin>>n;
   double res = calc_sum(n);
   cout << res << endl;
   return 0;
}
Output:
Enter the value of n : 5
1.29126

Note: also read about Sum Of Series 1 + 2 + 3 + 4 + 5 + 6 . . . . n

Follow Me

Please follow me to read my latest post on programming and technology if you like my post.

https://www.instagram.com/coderz.py/

https://www.facebook.com/coderz.py

The post Sum Of Series 1 + 1 / 2 ^ 2 +. . . . 1 / n ^ n first appeared on coderz.py.

]]>
https://coderzpy.com/sum-of-series-1-1-2-2-1-n-n/feed/ 0
Sum Of Series 1 + 2 + 4 + 8 + 16 + 32 + . . n https://coderzpy.com/sum-of-series-1-2-4-8-16-32-n/ https://coderzpy.com/sum-of-series-1-2-4-8-16-32-n/#respond Fri, 04 Nov 2022 20:38:37 +0000 http://coderzpy.com/?p=2027 Program code: Output: Main Logic used here: Note: also read about Sum Of Series 1 + 2 + 3 + 4 ...

The post Sum Of Series 1 + 2 + 4 + 8 + 16 + 32 + . . n first appeared on coderz.py.

]]>
Program code:

#include <iostream>
#include <math.h>
using namespace std;
//calculating the sum of the series
double calc_sum(int n) 
{
   int i;
   double sum = 0.0;
  for(i=0;i<n;++i)
    sum+=pow(2,i);

   return sum;
}
int main() 
{
   int n;
    cout<<"Enter the value of n :";
    cin>>n;
   double res = calc_sum(n);
   cout << res << endl;
   return 0;
}
Output:
Enter the value of n :5
31

Main Logic used here:

for(i=0;i<n;++i)
    sum+=pow(2,i);

Note: also read about Sum Of Series 1 + 2 + 3 + 4 + 5 + 6 . . . . n

Follow Me

Please follow me to read my latest post on programming and technology if you like my post.

https://www.instagram.com/coderz.py/

https://www.facebook.com/coderz.py

The post Sum Of Series 1 + 2 + 4 + 8 + 16 + 32 + . . n first appeared on coderz.py.

]]>
https://coderzpy.com/sum-of-series-1-2-4-8-16-32-n/feed/ 0