D Programming – Arrays

One of the concepts I love is arrays. I really like putting values inside one container. Feels like cheating and magic at the same time. It can store values and even variables, it can also be one-dimensional or multi-dimensional, and it just solves many problems.

Below is the code for declaring and accessing arrays:

import std.stdio;

int main(){
	int numbers[4] = [5,1,3,10];
	int sum = 0;
	
	for(int i = 0; i<numbers.length; i++){
		sum+= numbers[i];
	}
	
	writeln("The sum is ", sum);
	
	return 0;
}

Here is the output:

 

d-arrays

 

I hope you like this post.

Don’t forget to leave a comment.

Thank you very much.

Advertisements

D Programming – Input

Good Day, I was to talking to little kid this morning and he was asking me what color is the best. Every time I said a color he always answered “Really? (color)?”
What’s your favorite color? Yellow
Really? Yello?

haha, his expression made his annoying answer funny.

I adapated that kid to my program here showing basic input of string in D language.

Here is my annoying kid asking for colors program:

import std.stdio;
import std.string;

int main(){
	string input = "";
	
	write("What is your favorite color? ");
	input = strip(stdin.readln());
	
	writeln("Really? ", input, "?");
	return 0;
}

Here is the output:

d-input

 

I hope you like this post. Don’t forget to leave a comment.

Thank you very much.

D – Programming (Strings)

In programming, Strings are group of characters and they are not those strands of fiber that we tie knots or tie somebody’s neck. I often introduce students to this concept by making them count the letters of their name and resite them in reverse. Quite really fun at times. πŸ™‚

Below is a code that shows string initialization and concatenation:

import std.stdio;

int main(){
	string fname = "Mark";
	string lname = "Hay";
	
	writeln("Hello ", fname, " ", lname);
	
	return 0;
}

Here is the output:

d-strings

 

Don’t forget to leave a comment below.

Thank you very much.

D Programming – While Loop

Hello there, below is a program that show a basic while loop. It is very much like the syntax of C++ and Java. It is really good to remember the parts of a loop like the starting value, end statement and the operation of increment or decrement. Enjoy! πŸ™‚

import std.stdio;

int main(){
	int i = 0;
	
	while(i<11){
		writeln(i);
		i++;
	}
	
	return 0;
}

Here is the output:

d-while

 

Don’t forget to leave a comment below.

Thank you very much.

D Programming – Loops(For Loop)

Most of the time, students get confused of the usage of looping. I always say, “Imagine a block of code that prints 10 consecutive numbers. One way of doing it is by printing them one-by-one. That would be easy if printing 10 numbers is the case but if your program needs to print 1000 numbers then that would be a very long program just for printing. The answer to that problem is looping. If you use a loop, you just define a starting point, a condition of where you are going to stop, and the process if it is increasing or decreasing. A block of 1000 codes would just take three to four lines of codes.”

 

In this program we are going to use a loop called a for loop. I used a variable x to hold the value that will be printed then I declared the starting value and ending value which are inside the range.

Below is my program.

import std.stdio;

int main(){
	
	for(int i = 0; i<11; i++){
		writeln(i);
	}
	
	return 0;
}

 

Here is my output:

d-for

 

Thank you for reading this short post. πŸ™‚

Don’t foget to leave a comment.

D Programming – Arithmetic Operations Basics

One of the most used syntax in a language are the arithmetic operations. Operations like addition, subtraction, multiplication, division, and modulus are the basics. Below is a program I created that shows the basic syntax. I first declared two variables named x and y. Β Then I performed the arithmetic operations using each variables in the following lines.

The code is below:

import std.stdio;

int main(){
	int x = 5, y = 2;
	writeln("x = ", x, " and y = ", y);
	writeln("x + y = ", x + y);
	writeln("x - y = ", x - y);
	writeln("x * y = ", x * y);
	writeln("x / y = ", x / y);
	writeln("x % y = ", x % y);
	
	return 0;
}

 

Run the Program.

The output should be same as below:

 

D-arith

 

Thank you for reading this short post.

Don’t forget to leave a comment.