Saturday, May 20, 2017

Core Java

Java Programming

In 1991, the team "Green Team" led by James Gosling at Sun Microsystems created a programming language for digital consumer devices. The language was called Oak then. Why Oak? Because there was an oak tree outside Gosling's office.
The "Green Team" demonstrated the use of the language with an interactive television. However, it was too advanced for the digital cable television at the time, and more suitable a technology that was starting to take off, the Internet.
Later, the language was renamed Green and finally renamed Java from Java coffee; hence the coffee-cup logo.
Since C/C++ was popular back then, James Gosling designed the language with C/C++ style syntax, and philosophy "write once, run anywhere". After years, Sun Microsystems released the first public implementation of Java in 1995. It was announced that the Netscape Navigator Internet browser would incorporate Java technology.
In 2010, Sun Microsystems was completely acquired by Oracle Corporation along with Java.
Java Version History
  1. June 1991 - Java language project was initiated
  2. JDK 1.0 - January, 1996
  3. JDK 1.1 - February, 1997
  4. J2SE 1.2 - December, 1998
  5. J2SE 1.3 - May, 2000
  6. J2SE 1.4 - February, 2002
  7. J2SE 5.0 - September, 2004
  8. Java SE 6 - December, 2006
  9. Java SE 7 - July, 2011
  10. Java SE 8 (latest version) - March 18, 2014
  11. Java SE 9 - July, 2017 (announced release date)

Features of Java Programming Language

  1. Java is platform independent

    Java was built with the philosophy of "write once, run anywhere" (WORA). The Java code (pure Java code and libraries) you write on one platform (operating system) will run on other platforms with no modification.

    To run Java, an abstract machine called Java Virtual Machine (JVM) is used. The JVM executes the Java bytecode. Then, the CPU executes the JVM. Since all JVMs works exactly the same, the same code works on other operating systems as well, making Java platform-independent.
  2. An object-oriented Language

    There are different styles of programming. Object-oriented approach is one of the popular programming styles. In object-oriented programming, a complex problem is divided into smaller sets by creating objects. This makes your code reusable, has design benefits, and makes code easier to maintain.

    Many programming languages including Java, Python, and C++ has object-oriented features. If you are serious about programming, you should definitely learn object-oriented style of programming.
  3. Java is fast

    The earlier versions of Java were criticized for being slow. However, things are completely different now. The new JVMs are significantly faster. And, the CPU that executes JVM are also getting more and more powerful.

    Now, Java is one of the fastest programming languages. Well optimized Java code is nearly as fast as lower level languages like C/C++, and much faster than Python, PHP etc.
  4. Java is secure

    The Java platform provides various features for security of Java applications. Some of the high-level features that Java handles are:

    - provides secure platform for developing and running applications
    - automatic memory management, reduces memory corruption and vulnerabilities
    - provides secure communication by protecting the integrity and privacy of data transmitted
  5. Large Standard Library

    One of the reasons why Java is widely used is because of the availability of huge standard library. The Java environment has hundreds of classes and methods under different packages to help software developers like us. For example,

    java.lang - for advanced features of strings, arrays etc.
    java.util - for data structures, regular expressions, date and time functions etc.
    java.io - for file i/o, exception handling etc.
  6. Applications of Java

    Java technology is everywhere, powering 3 billion devices worldwide. It's more than likely that you have used Java one way or the other. Here are some of the applications of Java.
    1. Android apps - Java programming language using Android SDK (Software Development Kit) is usually used for developing Android apps.
    2. Web apps - Java is used to create Web applications through Servlets, Struts or JSPs. Some of the popular web applications written in Java are: Google.com, Facebook.com, eBay.com, LinkedIn.com etc.
      It's important to note that, these sites may not be entirely written in Java, and may use other programming languages along with Java.
    3. Software Development - Softwares like Eclipse, OpenOffice, Vuze, MATLAB etc use Java.
    4. Big Data Processing - You can use popular software framework like Hadoop (which itself is written in Java) to process Big Data. To use Hadoop, you need to understand Java programming.
    5. Trading System - You can build trading applications having low latency using the Oracle Extreme Java Trading Platform.
    6. Embedded Devices - While C/C++ programming languages are still popular choices for working with embedded systems, Oracle's Java Embedded technologies provide platform and runtime for billions of embedded devices like: televisions, SIM card, Blu-ray Disc players etc.
    Besides these applications, Java is also used for game development, scientific applications (like natural language processing), and many others.
  7. Learning a new programming language can be challenging. You will hear a lot of new terms which can be overwhelming for a newbie. So, we have decided to explain a few terms that you are likely to hear in the world of Java programming language.
    Note, this is not the complete list of Java Glossary. Instead, it's the list of terms you should know before you start learning Java programming.
    Java - Java is a set of technologies (programming language and computing platform) for creating and running software. However, Java is often used to refer Java programming language for simplicity.
    Java programming language - A powerful, general-purpose, platform-independent, object-oriented programming language.
    Java 8 - Java 8 is the latest major release for Java. Our Java tutorial will include all major features of Java 8.
    Java EE, Java ME and Java SE - Java EE, Java ME and Java SE stands for Java Platform Enterprise Edition, Micro Edition, and standard edition respectively.
    Java EE is targeted for applications which run on servers. Java ME is targeted for resource limited devices like: embedded devices. And, Java SE is the basic Java environment used for creating standard programs.
    If you are a java programming newbie, we recommend you to start with J2SE.
    JVM - JVM (Java Virtual Machine) is an abstract machine that enables your computer to run a Java program.
    JRE - JRE (Java Runtime Environment) contains JVM, supporting libraries, and other components to run a Java program. However, it doesn't contain any compiler and debugger.
    JDK - JDK (Java Development Kit) contains JRE and tools such as compilers and debuggers for developing Java applications.
  8. Often, a program called Hello World is used to introduce a new programming language to beginners. It is a simple program that outputs Hello, World!. In this section, you will learn to write the same program in Java using Neatbeans
    1. Open Eclipse.
    2. Go to File > New > Java Project
    3. Give the Project Name. We will call it Hello World for now and click Finish
    4. Now, you need to create a new Java class.
    5. Select Hello World project in the left sidebar. When the project is selected, go to File > New > Class and give Name to the class and click Finish. We will call it HelloWorld.
    6. Copy the following code in HelloWorld.java file and save it. (Shortcut: Ctrl +S, or Cmd +S for Mac)

      public class HelloWorld {
       
          public static void main(String[] args) {
              // prints "Hello, World!"
              System.out.println("Hello, World!");
          }
      }
    7. Finally, hit run button. If everything goes right, you will see Hello, World! output on the screen.
    8. INTRODUCTION
      • Java Hello World
      • Java JVM, JRE and JDK
      • Java Data Types
      • Java Operators
      • Java Input and Output
      • Java Expressions & Blocks
      • Java Comment
      FLOW CONTROL
      • Java if...else
      • Java switch Statement
      • Java for Loop
      • Java for-each Loop
      • Java while Loop
      • Java break Statement
      • Java continue Statement
      JAVA OOP NEXT
      • Classes & Objects
      • Java Methods
      • Java Constructor
      • Java Recursion
      • Java this Keyword
      • Java Access Modifiers
      • Java garbage collection

Learn C++ Programming

“C++ is a statically-typed, free-form, (usually) compiled, multi-paradigm, intermediate-level general-purpose middle-level programming language.”
In simple terms, C++ is a sophisticated, efficient and a general-purpose programming language based on C. It was developed by Bjarne Stroustrup in 1979.
Many of today’s operating systems, system drivers, browsers and games use C++ as their core language. This makes C++ one of the most popular languages today.
Since it is an enhanced/extended version of C programming language, C and C++ are often denoted together as C/C++.

History of C++

While Bjarne Stroustrup was working in AT&T Bell Labs in 1979, he faced difficulties in analyzing UNIX kernel for distributed systems. The current languages were either too slow or too low level. So, he set forward to create a new language.
For building this language, he chose C. Why C? Because it is a general purpose language and is very efficient as well as fast in its operations.
He used his knowledge of object-oriented model from SIMULA and began working on class extensions to C. His aim was to create a language with far higher level of abstraction while retaining the efficiency of C.
This new programming language was named C withClasses, but was later renamed to C++ (++ refers to the increment operator in C).

  important features you should know.

  1. C++ is fast

    Since, C++ is an extended version of C, the C part of it is very low level.

    This offers a huge boost in speed that high level languages like Python, Java don’t give you.
     
  2. C++  is statically typed

    C++ is a statically typed programming language.

    In simple terms, C++ doesn’t allow the compiler to make assumptions about the type of data e.g. 10 is different from “10” and you have to let C++ know which one you are talking about.

    This helps the compiler catch errors and bugs before execution of the program.
     
  3. C++ is a multi-paradigm programming language

    C++ supports at least 7 different styles of programming and gives developers the freedom to choose one at their will.

    Unlike Java and Python, you don’t need to use objects to solve every task (if it’s not necessary).

    You can choose the programming style that fits your use case.
     
  4. Object oriented programming with C++

    Object oriented programming helps you solve a complex problem intuitively.

    With its use in C++, you are able to divide these complex problems into smaller sets by creating objects.
     
  5. Power of standard library (Standard template library - STL)

    The power of C++ extends with the use of standard libraries contained in it.

    These libraries contain efficient algorithms that you use extensively while coding.
  6. Now you have installed the compiler based on your OS, it’s time to write your first C++ program.

    “Hello World!”

    Your first C++ program will be a “Hello World!” program.
    You might have noticed “Hello World!” being the first program while starting out with any programming language. This is because:
    • It is a standard check to see whether everything is working fine or not.
    • There will be very less code to start with.
    • The less code makes it intuitive for the beginners to get acquainted with the language.
    • The code is enough to learn the basic syntax and semantics of the language.
    So, let’s get coding.
    #include <iostream>
    using namespace std;
    int main()
    {
        cout<<"Hello World!";
        return 0;
    }
    The program prints Hello World! in the output screen.

    How the program works?

    Now, let’s dissect the above code. The code is divided into six major parts:
    • #include <iostream>
    • using namespace std
    • ;
    • int main() { }
    • cout << “Hello World!”;
    • return 0;
    1. What is #include <iostream>?

      If you’ve already written code in C language before, you might seen this line of code before. If you haven’t, don’t worry we’ll cover it now.

      This statement includes the header file into the application so that you are able to use the operations included in them. Also, you can create your own header files and include them in your program using the #include.

      What is iostream?
      iostream is what you call the header file. It is a standard C++ input/output library file.
      It comes packaged with the compiler/IDE and contain mechanisms to get the information from the user and print same or added information to a file, screen or any other media.
      What is #include?
      The #include iostream file, into the program. This ensures that now you’re able to use the operations, iostream operations (like: taking input from user, displaying output on the screen), in the program.
    2. What is using namespace std;”?

      The statement is intuitive in itself, you are “using” the “namespace” “std” in your file.
      We use the namespace std to make it easier to reference operations included in that namespace.
      If we hadn’t used the namespace, we’d have written std::cout instead of cout. This tells the compiler that every cout is actually std::cout.

      What’s a namespace?

      It’s a region where your code resides. It limits or expands the scope of your code to one or more files.

      Why do you use namespace?

      Like two persons can have the same name, variables and functions in C++ can have same names as well. The use of namespace is to avoid the confusion of which variables/functions you are referencing to.

      What is std?

      std is a standard namespace used in C++.
    3. Semicolon ”;”

      Ask any C++ programmer and they will tell you at least one horror story related to the semicolon ; .

      The semicolon is a terminal. It terminates a statement. When missed or incorrectly used, it will cause a lot of issues.
    4. int main() { }

      As the name suggests, it is the main function of the program. The code inside { } is called the body and is executed first when you run your C++ program.

      It is one code that is mandatory in a C++ program. If you just have this line of code alone, your program will be valid.
    5. cout << “Hello World!”;

      This statement prints “Hello World!” onto the output screen.

      The cout is an object of standard output stream. What this means is, it outputs/prints the data after << , i.e. Hello World! into a stream (in this case, the output screen).

      What is a stream?

      Stream is basically a sequence of objects, usually bytes. It can describe files, input/output terminal, sockets, etc.
      What is <<?

      << is the insertion operator used to write formatted data into the stream.
    6. What is return 0;?

      This statement returns 0 ‘zero’.

      This is called a return statement. It isn’t mandatory to return anything from the main() function but is rather a convention. If not return, the compiler returns a status automatically.

      Why zero in return statement?

      It denotes Exit status of the application that basically the tells system “The program worked fine.”

FLOW CONTROL
  • if...else Statement
  • for Loop
  • do...while Loop
  • break & continue
  • switch Statement
  • goto Statement
FUNCTIONS
  • C++ Functions
  • Function Types
  • Function Overloading
  • Default Argument
  • Storage Class
  • C++ Recursion
STRUCTURES
  • Structure
  • Structure and Function
  • Pointers to Structure
  • Enumeration
ARRAYS & STRING
  • C++ Arrays
  • Multidimensional Arrays
  • Function and Array
  • C++ String
OBJECT & CLASS
  • Objects and Class
  • C++ Constructors
  • Objects & Function
  • Operator Overloading
INHERITANCE
  • C++ Inheritance
  • Function Overriding
  • Multilevel Inheritance
  • Friend Function
  • Virtual Function
  • Templates

C++ "Hello, World!" Program
C++ Program to Print Number Entered by User
C++ Program to Add Two Numbers
C++ Program to Find Quotient and Remainder
C++ Program to Find Size of int, float, double and char in Your System
C++ Program to Swap Two Numbers
C++ Program to Check Whether Number is Even or Odd
C++ Program to Check Whether a character is Vowel or Consonant.
C++ Program to Find Largest Number Among Three Numbers
C++ Program to Find All Roots of a Quadratic Equation
C++ Program to Calculate Sum of Natural Numbers
C++ Program to Check Leap Year
C++ Program to Find Factorial
C++ Program to Generate Multiplication Table
C++ Program to Display Fibonacci Series
C++ Program to Find GCD
C++ Program to Find LCM
C++ Program to Reverse a Number
C++ Program to Calculate Power of a Number
Increment ++ and Decrement -- Operator Overloading in C++ Programming
C++ Program to Subtract Complex Number Using Operator Overloading
C++ Program to Find ASCII Value of a Character
C++ Program to Multiply two Numbers
C++ Program to Check Whether a Number is Palindrome or Not
C++ Program to Check Whether a Number is Prime or Not
C++ Program to Display Prime Numbers Between Two Intervals
C++ Program to Check Armstrong Number
C++ Program to Display Armstrong Number Between Two Intervals
C++ Program to Display Factors of a Number
C++ Programs To Create Pyramid and Pattern
C++ Program to Make a Simple Calculator to Add, Subtract, Multiply or Divide Using switch...case
C++ Program to Display Prime Numbers Between Two Intervals Using Functions
C++ Program to Check Prime Number By Creating a Function
C++ Program to Check Whether a Number can be Express as Sum of Two Prime Numbers
C++ program to Find Sum of Natural Numbers using Recursion
C++ program to Calculate Factorial of a Number Using Recursion
C++ Program to Find G.C.D Using Recursion
C++ Program to Convert Binary Number to Decimal and vice-versa

C++ Program to Find Largest Element of an Array
C++ Program to Find the Number of Vowels, Consonants, Digits and White Spaces in a String
C++ Program to Remove all Characters in a String Except Alphabets.
C++ Program to Find the Length of a String
C++ Program to Concatenate Two Strings
C++ Program to Copy Strings
C++ Program to Add Complex Numbers by Passing Structure to a Function
C++ Program to Calculate Difference Between Two Time Period
C++ Program to Store and Display Information Using Structure

Friday, May 19, 2017

 Q-BASIC PROGRAMS 

 1)Write a program to enter your name and print it .


CLS
Input 'Enter you name';n$
Print 'The name is';n$
End




2)Write a program to enter your name, city, country, age and print them.


CLS
Input " Enter the name ";N$
Input " Enter the city";C$
Input " Enter the country";CO$
Input " Enter the age";A
Print " The name is ";N$
Print " The city is ";C$
Print " The country is ";CO$
Print " The age is ";A
End



3)Write a program to find the area of rectangle.


Cls
Input " enter the length " ;l
Input " enter the breadth " ;b
let A = l*b
Print" the area of rectangle=" ;a
End




4)Write a program to find the area of the triangle.


Cls
Input " enter the base" ;b
Input " enter the height" ;h
let T = 1/2*b*h
Print" The area of triangle=" ;T
End




5)Write a program to find the area of the circle.


Cls
Input" Enter the radius " ;R
Let C=22/7*R^2
Print " The area of circle =" ;C
End




6)Write a program to find the circumference of the circle.


Cls
Input" Enter the radius " ;R
Let Circumference=22/7*R*2
Print " The area of circle =" ;Circumference
End




7)Write a program to find the area of the square.


Cls
Input" Enter the number" ;n
Let square= n^2
Print" The area of square=" ;Square
End




8)Write a program to find the area of the square and cube.


Cls
Input" Enter the number" ;n
Let square= n^2
Let Cube = n^3
Print" The area of square=" ;Square
Print" The area of cube=" ; Cube
End




9)Write a program to find the volume of the box.


Cls
Input " enter the length " ;l
Input " enter the breadth " ;b
Input " enter the height " ;h
Let Volume= l*b*h
Print" The volume of box =" ;Volume
End




10)Write a program to convert the weight from kilogram to pounds.


CLS
Input"Enter the weight in kilogram";K
Let P=K*2.2
Print "The pound is ";P
End




11)Write a program to convert the distance from kilometer to miles.


Cls
Input"Enter the length in kilometer";K
Let M= K / 1.6

Print "The length in miles =";M
End




12)Write a program to convert the distance from miles to kilomiles.


Cls
Input " Enter the length in miles";M
Let K=M*1.6
Print" The length in kilo miles=";K
End




13)Write a program to enter the initial mileage(m1) and final mileage (m2) then calculate the distance traveled.


CLS
Input "Enter the Initial Mileage";M1
Input "Enter the Final Mileage";M2
Let D= M2-M1
Print " The distance covered=";D
End




14)Write a program to find out the simple Interest.


Cls
Input " Enter the Principal";P
Input " Enter the Rate";R
Input " Enter the Time";T
Let I = P*T*R/100
Print " The simple Interest = ";I
End




15)Write a program to find out the simple Interest and the Amount.


Cls
Input " Enter the Principal";P
Input " Enter the Rate";R
Input " Enter the Time";T
Let I = P*T*R/100
Let A= P + I
Print " The simple Interest = ";I
Print " The amount=";A
End




16)Write any number and find it's half.
Cls
Input "Enter the desired number "; N
Let H = N/2
Print "The half of the number = ";H
END




17)Write a program to find the area of four walls of a room.


Cls
Input"Enter the height ";H
Input"Enter the length "; L
Input"Enter the Breadth";B
Let A= 2 * H * (L+B)
Print " The area of four walls =";A
End




18)Write a program to find the perimeter of a rectangle.


Cls
Input " enter the length " ;l
Input " enter the breadth " ;b
Let P=2*(l+b)
Print" The perimeter of rectangle=" ;P
End




19)Write a program to enter any three numbers,sum and the average.


Cls
Input " Enter any number" ;A
Input " Enter any number" ;B
Input " Enter any number" ;C
Let Sum = A+B+C
Let Average =Sum/3
Print" The sum=" ;Sum
Print" The Average is " ;Average
End




20)Write a program to enter any two numbers their Sum,Product and the Difference.


CLS
Input " Enter any number" ;A
Input " Enter any number" ;B
Let Sum = A+B
Let Difference= A-B
Let Product = A*B
Print" the sum =" ;Sum
Print" the Difference =" ;Difference
Print" the Product =" ; Product
End




21)Write a program to find the average of three different numbers.


Cls
Input" Enter the number " ;A
Input" Enter the number " ;B
Input" Enter the number " ;C
Let Avg= (A+B+C)/3
Print" The average=" ;Avg
End




22)Write a program to input student's name,marks obtained in four different subjects,find the total and average marks.


Cls
Input" Enter the name " ;N$
Input" Enter the marks in English" ;E
Input" Enter the marks in Maths" ;M
Input" Enter the marks in Science" ;S
Input" Enter the marks in Nepali" ;N
Let S=E+M+S+N
Let A=S/4
Print " The name of the student is" ;N$
Print " The total marks is" ;S
Print " The Average marks" ;A
End




23)Write a program to find 10%,20% and 30% of the input number.


Cls
Input" Enter any number" ;N
Let T=10/100*N
Let Twe=20/100*N
Let Thi=30/100*N
Print " 10%of input number=" ;T
Print " 20%of input number=" ;Twe
Print " 30%of input number=" ;Thi
End


24)Write a program to convert the distance to kilometer to miles.


Cls
Input" Enter the kilometer" ;K
Let M=K/1.6
Print " The miles = " ;M
End


25)Write a program to find the perimeter of square.


Cls
Input “Enter the length”; L
Let P =4 * L
Print “ The perimeter of square=”;P
End




26)Write a program to enter the Nepalese currency and covert it to Indian Currency.


CLS
Input “Enter the Nepalese currency” ;N
Let I = N * 1.6
Print “the Indian currency=”;I
End


27)Write a program to enter the Indian currency and covert it to Nepalese Currency.


CLS
Input “Enter the Indian currency” ;N
Let N = I / 1.6
Print “the Nepalese currency=”;I
End




28)Write a program to enter any number and find out whether it is negative or positive.


CLS
Input “Enter the number”; N
If N>0 Then
Print “ The number is positive”
Else
Print “The number is negative”
EndIf
End




29)Write a program to enter any number and find out whether it is even or odd using select case statement.


Cls
Input “Enter any number” ;N
R=N mod 2
Select case R
Case = 0
Print “The number is Even number”
Case Else
Print “The number is odd number”
End Select
End




30)Write a program to check the numbers between 1 & 3.


Cls
Input “Enter the numbers between 1-3”;N
Select case N
Case 1
Print “It’s number 1”;
Case 2
Print “It’s a number 2”;
Case 3
Print “It’s a number 3”
Case else
Print “It’s out of range”;
End select
End




31)Write a program to enter any alphabet and test alphabet is ‘a’ or not using the select case statement.


Cls
Input “Enter the alphabet”;A$
A$=UCase$ (A$)
Select Case A$
Case ‘A’
Print “It’s alphabet A”
Case Else
Print “It’s not alphabet A”
End Select
End




32)Write a program to enter any alphabet and find out whether the number is vowel or alphabet.


Cls
Input “Enter Letters/Alphabet”;A$
A$ = UCase $ (A$)
Select case A$
Case “A”, “E”, “I”, “O”, “U”
Print “Its’ a vowel”
Case Else
Print “ It’s not a vowel”
End Select
End




33)Generate the following numbers using For….Next…..Loop.
1,2,3,4,…,50


CLS
For I = 1 to 50 Step 1
Print I
Next I
End

34)Generate the following numbers using For….Next…..Loop.
1,3,5,7,9,....99


Cls
For I = 1 to 99 Step 2
Print I
Next I
End



35)Generate the following numbers using For….Next…..Loop.
2,4,6,8,10,…50

Cls
For I = 2 to 50 Step 2
Print I
Next I
End




36)Generate the following numbers using For….Next…..Loop.
1,3,5,7,…99

ClsFor I = 1 to 99 Step 2

Print I
Next I
End




37)Generate the following numbers using For….Next…..Loop.
5,10,15,…90


Cls
For I = 5 to 90 Step 5
Print I
Next I
End




38) Generate the following numbers using For….Next…..Loop.
10,20,30,40,…100.


Cls
For I = 10 to 100 Step 10
Print I
Next I
End




39)Write a program to print numbers stated below USING WHILE…WEND STATEMENT.


Cls
I = 1
While I<=100
Print I ;
I = I + 1
WEND
END




40)Generate the following numbers using For….Next…..Loop.
2,4,6,8,10….50


CLS
I = 2
While I < =50
Print I;
I = I + 2
WEND
END




41)Write a program to print numbers stated below USING WHILE…WEND STATEMENT.
1,3,5,7,9,…99


CLS
I = 1
While I <=99
Print I;
I = I + 2
WEND
END




42)Write a program to print numbers stated below USING WHILE…WEND STATEMENT.
1,4,9,…upto 10th term.


CLS
I=1
While I < =10
Print I^2;
I = I + 1
WEND
END

Thursday, May 18, 2017

C Programming

C is a general-purpose programming language used for wide range of applications from Operating systems like Windows and iOS to software that is used for creating 3D movies.
C programming is highly efficient. That’s the main reason why it’s very popular despite being more than 40 years old.
Standard C programs are portable. The source code written in one system works in another operating system without any change.
As mentioned, it’s a good language to start learning programming. If you know C programming, you will not just understand how your program works, but will also be able to create a mental picture on how a computer works.
C is closely associated with Unix Operating system

Development of Unix System

The PDP-11 version of Unix system was written in assembly language. Assembly languages are low-level programming languages that are specific to a particular computer architecture. They are hard to write and understand.
The developers of Unix Operating system (including Dennis Ritchie and Stephen C. Johnson) decided to rewrite the system in B language. However, B couldn’t suffice some of the features of PDP-11, which led to the development of C.
In 1972, the development of C started on the PDP-11 Unix system. A large part of Unix was then rewritten in C. By 1973, C was powerful enough to be used in Unix Kernel. Dennis Ritchie and Stephen C. Johnson made further changes to the language for several years to make it portable in Unix Operating system.

First Book on C Programming

In 1978, the first book of C programming, The C Programming Language, was published. The first edition of the book provided programmers informal specification of the language. Written by Brian Kernighan and Dennis Ritchie, this book is popular among C programmers as "K&R".

ANSI C

With the rapid growth of C language for several years, it was time for language to get it standardized.
C89. The first standard of C was published by American National Standards Institute (ANSI) in 1989. This version is commonly popular as C89.
C99. In late 1990’s, several new features like inline functions, several new data types and flexible array-members were added to the C standard. This is commonly known as C99.
C11. The C11 standard has new features like type generic macros, atomic operations, anonymous structures that doesn’t exist in C99.
All these three standards are also known by the name of ANSI C.
“Standard C programs are portable”. This means, the programs that follow ANSI C standard are portable among operating systems.
If you are new to programming, it’s advisable to follow the standard (ANSI C in case of C programming) that is accepted everywhere. It will help you learn the language the way it was intended.
You will learn to write a “Hello, World!” program in this section.

Why “Hello, World!” program?

“Hello, World!” is a simple program that displays “Hello, World!” on the screen. Since, it’s a very simple program, it is used to illustrate the basic syntax of any programming language.
This program is often used to introduce programming language to a beginner. So, let’s get started.
#include <stdio.h>
int main()
{
    printf("Hello, World!\n");
    return 0;
}

How “Hello, World!” program works?

Include stdio.h header file in your program.
C programming is small and cannot do much by itself. You need to use libraries that are necessary to run the program. The stdio.h is a header file and C compiler knows the location of that file. To use the file, you need to include it in your program using #include preprocessor.
Why do you need stdio.h file in this program?
In this program, we have used printf() function which displays the text inside the quotation mark. Since printf() is defined in stdio.h, you need to include stdio.h.
The main() function
In C programming, the code execution begins from the start of main() function (doesn’t matter if main() isn’t located at the beginning).
The code inside the curly braces { } is the body of main() function. The main() function is mandatory in every C program.
int main() {
}
This program doesn’t do anything but, it’s a valid C program.
The printf() function
The printf() is a library function that sends formatted output to the screen (displays the string inside the quotation mark). Notice the semicolon at the end of the statement.
In our program, it displays Hello, World! on the screen.
Remember, you need to include stdio.h file in your program for this to work.
The return statement
The return statement return 0; inside the main() function ends the program. This statement isn’t mandatory. However, it’s considered good programming practice to use it.

Key notes to take away

  • All C program starts from the main() function and it’s mandatory.
  • You can use the required header file that’s necessary in the program. For example: To use sqrt() function to calculate square root and pow() function to find power of a number, you need to include math.h header file in your program.
  • C is case-sensitive; the use of uppercase letter and lowercase letter have different meanings.
  • The C program ends when the program encounters the return statement inside the main()function. However, return statement inside the main function is not mandatory.
  • The statement in a C program ends with a semicolon.
INTRODUCTION
  • Keywords & Identifier
  • Variables & Constants
  • C Data Types
  • C Input/output
  • C Operators
  • Basic Examples
FLOW CONTROL
  • if...else Statement
  • C for Loop
  • C while Loop
  • break and continue
  • switch Statement
  • Decision Examples
FUNCTIONS
  • Functions Introduction
  • User-defined Function
  • Function Types
  • Recursion in C
  • Variable Scope
  • Function Examples
ARRAYS
  • C Arrays Introduction
  • Multidimensional Array
  • Arrays & Functions
  • Strings in C
  • String Functions
  • Array Examples
C POINTERS
  • C Pointers
  • Pointers & Arrays
  • Pointers & Functions
  • Memory Management
  • Pointer Examples
STRUCTURE & FILE
  • C Structure
  • Structure & Pointers
  • Structure & Functions
  • C Unions
  • Structure Examples
  • Files Handling

     

Programs to print triangles using *, numbers and characters


Example 1: Program to print half pyramid using *

*
* *
* * *
* * * *
* * * * *
Source Code
#include <stdio.h>
int main()
{
    int i, j, rows;

    printf("Enter number of rows: ");
    scanf("%d",&rows);

    for(i=1; i<=rows; ++i)
    {
        for(j=1; j<=i; ++j)
        {
            printf("* ");
        }
        printf("\n");
    }
    return 0;
}

Example 2: Program to print half pyramid a using numbers

1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
Source Code
#include <stdio.h>
int main()
{
    int i, j, rows;

    printf("Enter number of rows: ");
    scanf("%d",&rows);

    for(i=1; i<=rows; ++i)
    {
        for(j=1; j<=i; ++j)
        {
            printf("%d ",j);
        }
        printf("\n");
    }
    return 0;
}

Example 3: Program to print half pyramid using alphabets

A
B B
C C C
D D D D
E E E E E
Source Code
#include <stdio.h>
int main()
{
    int i, j;
    char input, alphabet = 'A';

    printf("Enter the uppercase character you want to print in last row: ");
    scanf("%c",&input);

    for(i=1; i <= (input-'A'+1); ++i)
    {
        for(j=1;j<=i;++j)
        {
            printf("%c", alphabet);
        }
        ++alphabet;

        printf("\n");
    }
    return 0;
}

Programs to print inverted half pyramid using * and numbers


Example 4: Inverted half pyramid using *

* * * * *
* * * *
* * * 
* *
*
Source Code
#include <stdio.h>
int main()
{
    int i, j, rows;

    printf("Enter number of rows: ");
    scanf("%d",&rows);

    for(i=rows; i>=1; --i)
    {
        for(j=1; j<=i; ++j)
        {
            printf("* ");
        }
        printf("\n");
    }
    
    return 0;
}

Example 5: Inverted half pyramid using numbers

1 2 3 4 5
1 2 3 4 
1 2 3
1 2
1
Source Code
#include <stdio.h>
int main()
{
    int i, j, rows;

    printf("Enter number of rows: ");
    scanf("%d",&rows);

    for(i=rows; i>=1; --i)
    {
        for(j=1; j<=i; ++j)
        {
            printf("%d ",j);
        }
        printf("\n");
    }

    return 0;
}

 Programs to display pyramid and inverted pyramid using * and digits


Example 6: Program to print full pyramid using *

        *
      * * *
    * * * * *
  * * * * * * *
* * * * * * * * *
Source Code
#include <stdio.h>
int main()
{
    int i, space, rows, k=0;

    printf("Enter number of rows: ");
    scanf("%d",&rows);

    for(i=1; i<=rows; ++i, k=0)
    {
        for(space=1; space<=rows-i; ++space)
        {
            printf("  ");
        }

        while(k != 2*i-1)
        {
            printf("* ");
            ++k;
        }

        printf("\n");
    }
    
    return 0;
}

Source Code

Example 7: Program to print pyramid using numbers


        1
      2 3 2
    3 4 5 4 3
  4 5 6 7 6 5 4
5 6 7 8 9 8 7 6 5
#include <stdio.h>
int main()
{
    int i, space, rows, k=0, count = 0, count1 = 0;

    printf("Enter number of rows: ");
    scanf("%d",&rows);

    for(i=1; i<=rows; ++i)
    {
        for(space=1; space <= rows-i; ++space)
        {
            printf("  ");
            ++count;
        }

        while(k != 2*i-1)
        {
            if (count <= rows-1)
            {
                printf("%d ", i+k);
                ++count;
            }
            else
            {
                ++count1;
                printf("%d ", (i+k-2*count1));
            }
            ++k;
        }
        count1 = count = k = 0;

        printf("\n");
    }
    return 0;
}

Example 8: Inverted full pyramid using *


* * * * * * * * *
  * * * * * * *
    * * * * *
      * * *
        *
Source Code
#include<stdio.h>
int main()
{
    int rows, i, j, space;

    printf("Enter number of rows: ");
    scanf("%d",&rows);

    for(i=rows; i>=1; --i)
    {
        for(space=0; space < rows-i; ++space)
            printf("  ");

        for(j=i; j <= 2*i-1; ++j)
            printf("* ");

        for(j=0; j < i-1; ++j)
            printf("* ");

        printf("\n");
    }

    return 0;
}

Example 9: Print Pascal's triangle


           1
         1   1
       1   2   1
     1   3   3    1
   1  4    6   4   1
 1  5   10   10  5   1 
Source Code
#include <stdio.h>
int main()
{
    int rows, coef = 1, space, i, j;

    printf("Enter number of rows: ");
    scanf("%d",&rows);

    for(i=0; i<rows; i++)
    {
        for(space=1; space <= rows-i; space++)
            printf("  ");

        for(j=0; j <= i; j++)
        {
            if (j==0 || i==0)
                coef = 1;
            else
                coef = coef*(i-j+1)/j;

            printf("%4d", coef);
        }
        printf("\n");
    }

    return 0;
}

Example 10: Print Floyd's Triangle.

1
2 3
4 5 6
7 8 9 10
Source Code
#include <stdio.h>
int main()
{
    int rows, i, j, number= 1;

    printf("Enter number of rows: ");
    scanf("%d",&rows);

    for(i=1; i <= rows; i++)
    {
        for(j=1; j <= i; ++j)
        {
            printf("%d ", number);
            ++number;
        }

        printf("\n");
    }

    return 0;
}


JDBC  Connectivity With MySQL