Showing posts from February, 2019

Looping in C Programming

Introduction of loopLooping is a way to repeat a block of statement for the finite number of times. Here finite means that a loop should be terminated automatically or whenever the user wants. We also learn that a program is a collection of instruction. In C there are various control instructions that control the way to execute the program. Sequence control instruction: In which the program statements are executes in the order in which they are written.Decision making control instruction: In which we make a choice by using the if-else construct.Switch/case control instruction: Alternate choice of if-else construct.Loop control instruction: Used to perform the task repeatedly.We have already learned first 3 control instruction, and now we are going to discuss the last one loop control instruction. Let's start the discussion with an example.Suppose you want to print "Hello", then you write print("Hello"); once. But if I tell you, print "Hello" for 100…

Escape Sequence | Formatted Printing | Interview Questions

Escape sequenceEscape sequence are some non-printable character that shows the effect while printing the output. The usage and example of escape sequence are given below.These are the combination of two characters backslash(\) and one more character but treated as a single character occupy only one byte as other characters.Escape Sequence CharacterMeaningUsage\aBell alertUsed to make a sound.\bbackspaceUsed to move the cursor to the one character back.\nnewlineUsed to change the line\rCarriage ReturnUsed to move the cursor to the beginning of the current line.\"Double QuotesUsed to print the double quotes\'Single QuotesUsed to print the single quotes\fForm feedUsed to change the page\ttabUsed to print the tabProgram to demonstrate the use of escape sequence#include<stdio.h> int main() { printf("Hello\a\a"); printf("Hello\nworld\n"); printf("Hello\tWorld\n"); printf("Hello\"Alex \"\n"); printf("Hello \'Cl…

Decision Making Statement | Part-2

Check the year is leap year or not.#include<stdio.h> int main() { int year; printf("Enter any year: "); scanf("%d",&year); 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); } } return 0; } OutputEnter any year: 1996 1996 is leap year Enter any year: 2001 2001 is no a leap year Enter any year: 1800 1800 is not a leap year Program ExplainedYou know the year which is divisible by 4 is called leap year, but what about the century? Each century is divisible by 4. but to check whether the century is a leap year or not we have to check its divisibility by 400. So the logic to check the year is a leap or not:First check the year is a century if the year is century then check its divisibility by 400. if it …

Decision Making Statements

As in the real world we need to make decision according to the situation. In the same sense we need to make decision while creating program.For decision making C provides various construct as follows:if statementif else statementnested if else statementelse if Laddercondional operatorswitch/case constructBy using the above-given decision-making statement we can implement the sense in a program that program can create decision automatically.if-else statementSyntax-1if(condition) { statement1; statement2; statement3; ........ ........ } else { statement1; statement2; statement3; ........ ........ } Syntax-2if(condition) statement; else statement; Syntax ExplainedThe block which is specified with if is called if-block and the block with else is called else block. If there is a single statement in if block or else block then no need to enclose in {} (curly braces). But in the case of more than one statement and if a…

Operators in C Language| Part-4

Unary operators Unary operator are those which only require one operand(argument) to operate. + , - , ++, --, typecast and sizeof are some unary operators in C programming. +5, -5 are the example of +,- unary operators. which represents +ve and -ve values.Unary operators have high priority than binary operators, so unary operators are always evalutes first.Increment / Decrement operator++, -- are called increment and decrement operators.Increment and decrement operators are available in two versionsPrefixPostfixDifference b/w prefix and postfixPrefixPostfix int a = 5; ++a; printf("%d",a); // 6 int a = 5; a++; printf("%d",a); // 6 int a = 5,b; b = ++a; printf("%d %d",a,b);// 6 6 int a = 5,b; b = a++; printf("%d %d",a,b);//6 5 Example of increment and decrement operatorsExample-1: int a = 3, b = 4,c; c = ++a * ++b; printf("%d %d %d",a,b,c); //4 5 20 Example-2: int a = 3,b = 4, c; c = a++ * b++; printf("%d %d %d",a,b,c);…

Operators in C Language | Part-3

Relational OperatorRelational operators are used to perform a logical test or create a condition. List of the logical operator is as given below...Relational opertorMeaning>Greater Than<Less Than>=Greater Than or Equal to<=Less Than or Equal to==Equal to!=Not euqalEvery relational operator returns true or false.In C any non-zero is treated as true and zero is treated as false.In C if any expression evaluated true then system returns 1 otherwise return 0.Relational operator are evaluated from left to right.Example-1 printf("%d",5>2); //print 1 Example-2 printf("%d",5>2>1); //print 0 //first 5>2 evaluated true and returns 1 and then result of this expression is compared with next 1, that is evaluated false. Logical Operator&&(And) ||(Or) !(not) are the 3 logical operators.&&(and), || (or) are used to combine two or more conditions.Each condition can be true or false, so what will be the output of the combination made by logical…

Operators in C Language | Part-2

Assignment operator= operator is called assignment operator.It is used to move the value or expression result in a variable.Assignment operator are evaluated from right to left order.Example of assignment:a = b = 5, In this example the 5 is assignment in b then b's value is moved to a.a = b + 5, Assignment operator have the lowest precedence so all other operator are evaluated first and then result is moved to the variable.5 = a, This is an error, because in this example we are trying to move the value of variable into constant and constant can't be changed.The same error will be prompted if you are try the expression 6 + b = a.Compound assignment+=, -=, *=, /=, %= are called compound assignment operator. It combine the Arithmetic as well as assignment simultaneously.Compound AssignmentEquivalent Expressiona += 5a = a + 5a -= 5a = a - 5a *= 5a = a * 5a /= 5a = a / 5a %= 5a = a % 5PreviousBack to C Programming indexNext

Operators in C Language

Observe the given expression y = x + 5. In the above given expression +, = are the operators and x, y, 5 are called operands / argument.Types of operatorAccording to the required argument the operator can be classified into 3 categories:unary operator: require only one argument. example(-5)binary operator: require two argument. example(5+8)ternary operator: require 3 argument. example(?:)According to the purpose operator are again classified into 8 types:Arithmetic operatorUnary operatorAssignment operatorRelational operatorLogical operatorBitwise operatorConditional operator(ternary operator)Comma operatorArithmetic operator+,-,*,/,%(mod) are called the arithmetic operator.The +,-,* are same as you learn in mathematical Arithmetic.To more understand take an example.5 + 3 * 4In the above given expression, which operator should be evaluates first? As in mathematics you learn the rule of BODMAS,which tells first brackets should be evaluated, then divide,after that Multiply,addition and …

Data types in C language

What is data type As you have seen in the previous article, a variable is declared as given below...
int a; //variable declaration In the above program code snippet, int is called a data type and a is a variable of that data type.Data type describe the content a variable can store.Data type also explains how data will be stored (the organization) in memory. Data types can be classified into two broad categories... Primitive (Basic) data typeComposite (Secondary) data types / User-defined data types In this article we will only discuss primitive data types, User-defined data types will be discussed later. Primitive data types Data types which are defined by the system/programming language are called primitive data type. These data type defines a single entity, which can't be divided further. List of primitive data types along with their memory occupied and the allowed range are described as below... Integral data typesReal data typesCharacter data typesIntegral data typesData type…

Variable in C programming

What is variable?Variable is an entity which can change during the program execution.ORVariable is the name of the memory place, where data is stored.ORVariable is just like a cup of tea.How to create a variable int x;
float y;
char z; General Syntaxdatatype variable_name;As shown in the above snippet, To create a variable two things are needed...Data typeVariable nameRules for creating variable nameVariable name can contains Alphabets(A-Z / a-z), Digits (0-9), and Underscore (_).Variable name must begins with an Alphabet or UnderscoreSpaces and Special symbols are not allowed.Keywords / Reserved words are not allowed as a variable name.Variable name should be meaningful.C is a case sensitive programming language, so age, Age, AGE all are different variables.Some valid / invalid variable names are listed below...Variable nameValid / Invalidnamevalidfather_namevalidfather's nameinvalidfather's_nameinvalidavg.invalid2personsinvalidperson1validintinvalidintegervalidintnumvalidFLO…

Constant in C Programming

Constant is an entity which never changes in the given problem or program.Types of ConstantThere are basically two types of constants in C.Scalar/ Primitive/ PrimaryCompositeThe Scalar types of constant are those which can't be divided any more and the composite constant are those which can be divided into multiple scalar constants. In this article, we only discuss the Scalar/Primitive constant.The Composite constants are discussed later.1. Integer ConstantA number without any fractional part is called Integer constant.Example: 25, 0, -44, 456 etcRules of Integer ConstantNo decimal points are allowed. Example 25 is correct but 25.0 is incorrect.Rules of Integer Constant +ve/-ve both types of values are allowed.Special, Comma and other characters are not allowed. Example 2 3 4 and 12,500 are incorrect.Valid Range: -32768 to 327672. Floating point constant or real constantCan contain decimal point +ve/-ve both types of values are allowed.Special, Comma and other characters are not a…

Constant and Variable

y = x + 5, See the expression carefully.It is clear that if we change the value of x the value of y is also changed but the 5 remains the same every time.So, x and y are the variables but 5 is a constant.PreviousBack to C Programming indexNext

Keywords in C language

Keywords are some words, which meaning is already reserved by the compiler. So these words are also called reserved words. In C, there are 32 keywords as listed below.autodoubleintstructbreakelselongswitchcaseenumregistertypedefcharexternreturnunsignedconstfloatshortunioncontinueforsignedvoiddefaultgotosizeofvolatiledoifstaticwhilePreviousBack to C Programming indexNext

Character Set

The allowed set of characters is called the character set.C character set consist of the following character.AlphabetsA-Z , a-zDigits0 - 9White SpacesSpaces, tabs, newlines etc.Special Symbols+, -, *, /, %, _,; and many more discussed as needed.PreviousBack to C Programming indexNext

Steps of learning C Programming

Steps of learning EnglishAlphabets => Words => Sentence => ParagraphSteps of learning C languageCharacter Set => Keywords/ Constant/ Variable => Instruction => Program PreviousBack to C Programming indexNext

Types of programming languages

Computer programming language can be classfied into two broad categories as follows:Machine oriented Langauge Binary Language/Machine LanguageAssembly LanguageProblem Oriented LanguageMachine Oriented LanguageIn the machine oriented langauage the programm is written in the form in which machine can understand.Machine language are also known as low level language.These language provides the better machine efficiency and fast execution of the program.Machine oriented language again classified into two categories as follows: Binary Language/Machine LanguageAs the name suggests in the binary language only two entity 0/1 (zero/one) are allowed. Where 0 and 1 can be understood in many ways by the computer.As a computer is a digital machine consists of billions of transistors which can be thought like an electronic switch and have two states on/off. The 0 represent the off or false whereas 1 represent the on/true.Assembly LanguageBecause of the complexity of machine language, A new lang…

Why C language used

Influences C Programming LanguageThe question is very simple before learning the C. That is why we learn C?C is very simple, small and fast.C is a programming language which has only 32 words with them you can build your own programming code.Compare to other programming languages the c program executes very fast.The most popular languages derived from C Programming such as C++, Java, C#(C sharp), PHP, Objective-C, Go lang, Python and many more. So if you want to learn those languages the C provide the basic foundation.The most popular operating systems such as Windows, Unix, Linux build with C.So sometimes people say that C is very old and now it is not used. Then I have to say, it is not the truth.Gaming environments are also written in C. More correctly we can say it has an indirect relationship with C.C programming offers better interaction with hardware.PreviousBack to C Programming indexNext

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.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.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.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: ");
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.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: ")
if n % 2 == 0 {
fmt.Println(n,"is even")
} else {

Popular posts from this blog

Program to calculate telephone bill

Arrays in C Language

String in golang

Sum of two numbers in go lang

Pointers in C Language

Decision Making Statements

Say Hello world in Go lang

Decision Making in Go lang

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

Installing go lang