Skip to main content

Posts

Showing posts from April, 2019

Flowchart and Algorithm

Algorithm The step-by-step process of solving any problem is known as algorithm. The algorithm represents logic of any program. It is free from computer languages. Characteristics of algorithm The features of algorithm are : Input Output Defeniteness Effectiveness Finiteness Flowchart It  is the symbolic representation of any algorithm(logic). We use the following symbols in the flowchart : Symbol Name Purpose oval start/stop parallelogram input/output rectangle process diamond decision making flow lines direction connectors connection Illustration of flowchart and algorithm with the help of an example : Read a number having two digits and prints the summation of two digits.

Reversing digits of a number

#include<stdio.h> #include<conio.h> void main() { clrscr(); int ,num,rev=0,rem; printf("Enter the number:"); scanf("%d",&num); while(num!=0) { rem=num%10; rev=rev*10+rem; num=num/10; } printf("Reverse of given Number is:%d",rev); getch(); }

Check prime

#include<stdio.h> #include<conio.h> void main() { clrscr(); int i,num; printf("Enter the number:"); scanf("%d",&num); int countfactor=0; for(i=1;i<=num;i++) { if (num%i==0) countfactor++; } if(countfactor==2) printf("Entered Number is a prime number"); else printf("Entered Number is not a prime number"); getch(); }

series iteration 2

#include<stdio.h> #include<conio.h> void main() { clrscr(); int i,lim,num=1; printf("Enter the limit:"); scanf("%d",&lim); for(i=1;i<=lim;i++) { printf("%d",num); if(i%2==0) num+=4; else num+=2; } getch(); }

Fibbonacci series

#include<stdio.h> #include<conio.h> void main() { clrscr(); int i,lim,a=0,b=1,c; printf("Enter the limit:"); scanf("%d",&lim); printf("%d,%d",a,b); for(i=3;i<=lim;i++) { c = a+b; a = b; b = c; printf(",%d",c); } getch(); }

Calculate x raised to the power n

#include<stdio.h> #include<conio.h> void main() { clrscr(); int i,x,n,res=1; printf("Enter the base number:"); scanf("%d",&x); printf("Enter the power:"); scanf("%d",&n); for(i=1;i<=n;i++) res=res*x; printf("X raised to the power N = %d"fac); getch(); }

series iteration 1

#include<stdio.h> #include<conio.h> void main() { clrscr(); int i,lim; printf("Enter the limit:"); scanf("%d",&lim); for(i=1;i<=lim;i++) { if(i%2==0) printf("0"); else printf("1"); } getch(); }

DataTypes

Each variable in C has an associated data type. Each data type requires different amounts of memory and has some specific operations which can be performed over it. Datatype specifies the type of variable These could be classified into following categories:            Primitive Datatype : Example - char, int, float, double.            Non-primitive Datatype : Example - array, structure, pointer. Following are the examples of some very common data types used in C: ·         char:  The most basic data type in C. It stores a single character and requires a single byte of memory in almost all compilers. ·         int:  As the name suggests, an int variable is used to store an integer. ·         float:  It is used to store decimal numbers (numbers with floating point value) with single prec...

A Simple 'C' Program with step by step demonstration

A Simple 'C' program: #include<stdio.h> #include<conio.h> void main() { clrscr(); printf("Hello All"); printf("\nWelcome to C programming"); getch(); } Step-by-step demonstration of the above program: #include    - This is the pre processor directive and it attached the given header file with our program during compilation time. main()  - It indicates that the program has begun now. {....}  - It indicates a block. clrscr()  - It is a library function , defined inside 'conio' header file which clears the screen at run time. printf()  - This is also a library function defined inside 'stdio' header file which prints the given message on the output window. \n  - This is known as line feed character and it breaks the message into separate lines. getch()  - This is also a library function which pauses our output. ;  - This symbol (semi-colon) is known as statement terminator and it indicates the compiler ...

Basic Rules for programming

Before we move further , let us see some of the rules that are applicable to all C programs: Each instruction in a C program is written as a separate statement. Therefore, a complete C program would comprise a series of statements. The statement in a program must appear in the same order in which we wish them to be executed; unless of course the logic of the problem demands a deliberate 'jump' or transfer of control to a statement, which is out of sequence. Blank spaces may be inserted between two words to improve the readability of the statement. However, no blank spaces are allowed within a variable, constant or keyword. All statement are entered in small case letters. C has no specific rules for the position at which a statement is to be written. That's why it is often called a free-form language. Every C statement must end with a ; . Thus ; acts as a statement terminator.

C Keywords

Keywords are the words whose  meaning has already been explained to the C compiler (or in a broad sense to the computer). The keywords cannot be used as variable names because if we do so, we are trying to assign a new meaning to the keyboard, which is not allowed by the computer. Some C compilers allow you to construct variable names that exactly resemble the keywords. However, it would be safer not to mix up the variable names and the keywords. The keywords are also called 'Reserved words'. There are only 32 keywords in C. The below figure gives a list of these keywords for your ready reference.

Largest value using switch

#include<stdio.h> #include<conio.h> void main() { clrscr(); int n1,n2; printf("Enter a 1st Num:"); scanf("%d",&n1); printf("Enter a 2nd Num:"); scanf("%d",&n2); int r = n1>=n2 switch(r) { case 1: { if(n1==n2) printf("Both numbers are equal"); else printf("First number is greater"); break; } case 0: printf("Second number is greater"); } getch(); }

Even Odd using switch

#include<stdio.h> #include<conio.h> void main() { clrscr(); int num; printf("Enter a Num:"); scanf("%d",&num); int r = num%2==0                                                       //% is used to find remainder switch(r) { case 1: printf("Entered Number is Odd"); break; case 0: printf("Entered Number is Even"); } getch(); }

Checking character

#include<stdio.h> #include<conio.h> void main() { clrscr(); char ch; printf("Enter a character:"); scanf("%c",&ch); if(ch>=65&&ch<=90)                                                          printf("Entered character is an Uppercase character"); else if(ch>=97&&ch<=122) printf("Entered character is a Lowercase character"); else if(ch>=48&&ch<=57) printf("Entered character is a Number"); else printf("Entered character is a Special Symbol"); getch(); }

Largest among three variables using nested if

#include<stdio.h> #include<conio.h> void main() { clrscr(); int n1,n2; printf("Enter 3 Nums:"); scanf("%d%d%d",&n1,&n2,&n3); if(n1>n2) { if (n1>n3) printf("1st number is greater"); else printf("3rd number is greater"); } else { if(n2>n3) printf("2nd number is greater"); else printf("3rd number is greater"); } getch(); }

Largest among three variable

#include<stdio.h> #include<conio.h> void main() { clrscr(); int n1,n2; printf("Enter 3 Nums:"); scanf("%d%d%d",&n1,&n2,&n3); if(n1>n2&&n1>n3)                                                          printf("1st number is greater"); else if(n2>n1&&n2>n3) printf("2nd number is greater"); else printf("3rd number is greater"); getch(); }

Leap Year

#include<stdio.h> #include<conio.h> void main() { clrscr(); int year; printf("Enter a Year:"); scanf("%d",&year); if(year%4==0)                                                         //% is used to find remainder printf("Entered Year is a Leap"); else printf("Entered Year is not a Leap Year"); getch(); }

Compare two numbers

#include<stdio.h> #include<conio.h> void main() { clrscr(); int n1,n2; printf("Enter 1st Num:"); scanf("%d",&n1); printf("Enter 2nd Num:"); scanf("%d",&n2); if(n1>n2)                                                          printf("1st number is greater"); else printf("2nd number is greater"); getch(); }

Even Odd Number

#include<stdio.h> #include<conio.h> void main() { clrscr(); int num; printf("Enter a Num:"); scanf("%d",&num); if(num%2==0)                                                         //% is used to find remainder printf("Entered Number is Even"); else printf("Entered Number is Odd"); getch(); }

Print total marks and percentage

#include<stdio.h> #include<conio.h> { clrscr(); int p,c,m; printf("Enter physics marks:"); scanf("%d",&p); printf("Enter physics marks:"); scanf("%d",&c); printf("Enter physics marks:"); scanf("%d",&m); int sum=p+c+m; float per=sum/3; printf("Total marks = %d",sum); printf("Percentage = %f",per);getch(); }

Reversing three digit number

#include<stdio.h> #include<conio.h> { clrscr(); int num = 123; printf("Original Number=%d",num); int d1 = num%10; num = num/10; int d2 = num%10; num = num/10; int d3 = num%10; int revnum = d1*100+d2*10+d3; printf("Reverse Number=%d",revnum); getch(); }