C programs Example’s

C program to print 0-100 numbers

#include <stdio.h>

int main()
{
    int n,i;
    printf("enter the count");
    scanf("%d",&n);
    for (i=0;i<=n;i++)
    {
    
        printf("%d",i);
    }
    
}

Output

enter the count 20

0123456789111121314151617181920

C program to check even or odd

#include <stdio.h>

int main()
{
    int n;
    printf("enter number to  check even or odd");
    scanf("%d",&n);
    if(n%2==0)
    {
        printf("number is even");
    }
    else
    {
        printf("number is odd");
    }
    
}

output

enter number to check even or odd 5

number is odd

C program to check prime or not

#include<stdio.h>
int main()
{
int i,n,prime=0;
printf("enter the number");
scanf("%d",&n);
for(i=2;i<n;i++)
{
if(n%i==0)
{
prime=1;
break;
}
}
if(prime==0)
{
printf("Number is prime");
}
else
{
printf("Number is not prime");
}
return 0;
}

Output

enter the number 43

number is prime

Write a program to calculate the Factorial.
Factorial
Example

#include<stdio.h>
int main()
{
int i,n,f=1;
printf("enter the number");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
f=f*i;
}
printf("Factorial of number is=%d",f);
}

Output
enter the number 5
Factorial of number is=120
 
3. Write a program to check whether the given number is palindrome or not.

Example

#include<stdio.h>
int main()
{
int r,n,p=0,temp;
printf("enter the number");
scanf("%d",&n);
temp=n;
while(n!=0)
{
r=n%10;
p=p*10+r;
n=n/10;
}
if(p==temp)
{
printf("Number is palindrome");
}
else
{
printf("Number is not palindrome");
}
return 0;
}

Output
enter the number 111
number is palindrome
 
4. Write a program to check whether the number is Armstrong or not.
Armstrong Number
Example

#include<stdio.h>
int main()
{
int n,r,p=0,temp;
printf("enter the number ");
scanf("%d",&n);
temp=n;
while(n>0)
{
r=n%10;
p=p+(r*r*r);
n=n/10;
}
if(temp==p)
{
printf("Number is Armstrong");
}
else
{
printf("Number is not Armstrong");
}
return 0;
}

Output
enter the number 153
Number is Armstrong
 
5. Write a program to display the Fibonacci Series.
Fibonacci Series
Example

#include<stdio.h> 
int main() 
{ int i=0,j=1,k,p,n; 
printf("Enter the number of elements:");
 scanf("%d",&n);
 printf("\n%d %d",i,j);
 for(p=2;p<n;++p)//loop starts from 2 because 0 and 1 are already printed
 { k=i+j;
 printf(" %d",k); 
i=j; j=k; 
} 
return 0; 
} 

Output
Enter the number of elements:12
0 1 1 2 3 5 8 13 21 34 55 89

Hits: 90

Structures and Unions in C

In C programming , a structure is a collection of heterogeneous elements under a same name.

Define

struct structureName 
{
    dataType member1;
    dataType member2;
    ...
};

Example

struct Person
{
    char name[20];
    int Number;
    float salary;
} person1, person2, p[20];

int main()
{
printf("enter name of person");
scanf("%s",&person1.name);
printf("enter id number");
scanf("%d",&person1.number");
printf("enter salary");
scanf("%f",&person1.salary);
}
return 0;
}

Output

enter name of person paras

enter id number 4819

enter salary 10000

Using same we can store/acess the data of person2 .

Why we use structure?

Suppose , you want to create or store the information of students data base

here if we are not using structure we need to create different variables for each student and this will increase size and complexity of the program

by using structure we can store easily and access the database more easily and efficiently

Unions

  • Unions are also a collection of heterogeneous elements
  • Unions are helpful when you want to store value in single member from different datatype variables
  • In unions only one member of union can be accessed at a time
  • Size of union is defined according to size of largest member data type.

Define

union union_name
{
    union_member1;
    union_member2;
    ...
    ...
    ...
    union_member_N;
};

Example

union mobile
{
  char name[50];
  int price;
} mobile1,mobile2;

int main();
printf("enter the brand name");
scanf("%s",%mobile1.name);

return 0;
}}

Output

enter brand name Samsung

 

Now you will be wondering whats the difference then? we can compile the same program using structure too, then how does it make a difference?

To understand the difference we need to look at the size of memory they are using

you can compare the size of strucuture and union using sizeof operator.

Hits: 108

Lifecycle of C program

A computer program goes through many phases from its development to execution. From the human readable format (source code) to binary encoded computer instructions (machine code).

Source code

Source code is a plain text file containing a programming code(instruction) written by human which can be read and change . It is simple text file written by programmers. source code is shared with anyone who wants to look at it and change it. Source code is later compiled and translated to Object code.

Example

#include
int main()
{
printf("hello");
return0;
}

Object code(OBJ)

Object code is a sequence of statements generated by compiler after the compilation process. The compiler reads source code written in high-level language and translates it to an intermediate language. The file generated by file called object code.

Machine code

Machine code is a set programs written in machine language. It is the only code executed by the CPU.

Its in binary, form of zero’s and ones. which can only be read by cpu present in computers .

As binary is the only languges which computer understands.these binary code can be of same length or can be vary depend’s upon the binary code

Example

01001000 01100101 01101100 01101100 01101111 00100000 01010111 01101111 01110010 01101100 01100100

Hits: 124

Dynamic memory allocation (malloc,calloc,realloc)

In dynamic memory allocation,The required memory size is allocated while program is getting executed. Memory is allocated on demand, under control of the programmer. It exists as long as you need it and is either destroyed sometime after nothing still refers to it – or explicitly when you free it up – depending on the programming language you use.

In dynamic memory allocation,The required memory size is allocated while program is getting executed.

Dynamic memory allocation occurs at runtime, static allocation also but the block of memory is determined by the OS at program start rather than during program run.

. malloc:=>{Allocates the memory of requested size and returns the pointer to the first byte of allocated space.}

2. calloc:=>{Allocates the space for elements of an array. Initializes the elements to zero and returns a pointer to the memory.}

3. realloc:=>{It is used to modify the size of previously allocated memory space.}

4. Free:=>{Frees or empties the previously allocated memory space.}

Use of dynamic memory in c

  • We can dynamically manage memory by creating memory blocks as needed in the heap
  • In dynamic memory allocation, memory is allocated at a run time.
  • Dynamic memory allocation permits to manipulate strings and arrays whose size is flexible and can be changed anytime in your program.
  • It is required when you have no idea how much memory a particular structure is going to occupy.

Hits: 137

Pointers In C

What are Pointers in C

It is a variable which is used to store the address of another variable in other words you can ay it points to the address of the variable. pointers improve the performance of the program also helps to reduce the code and can accessed to any memory location , Pointers can be used with array, functions and structures .

Example pointer

[php]#include<stdio.h>
#include<conio.h>
void main()
{
int a=70 ;
int *p; // pinter declaration;
p=&amp;amp;a;//store the address of a
printf("%x",p);// address of pointer
getch();
}[/php]

Output

fff4

 

Pointers concept and description

Sr.No. Concept & Description
1

Pointer arithmetic

There are four arithmetic operators that can be used in pointers: ++, –, +, –

2

Array of pointers

You can define arrays to hold a number of pointers.

3

Pointer to pointer

C allows you to have pointer on a pointer and so on.

4

Passing pointers to functions in C

Passing an argument by reference or by address enable the passed argument to be changed in the calling function by the called function.

5

Return pointer from functions in C

C allows a function to return a pointer to the local variable, static variable, and dynamically allocated memory as well.

Hits: 243

Strings and string functions In C

What is String in C ?

String is a collection of characters. There are two ways to declare string in c language.

1. By char array

2. By string literal

  • Header file

#include<string.h> // used to perform string functions

  • Declaring string by char array
char c[4]={'g','o','o','d'};

 

  • Declaring string by string literal
char c[]="morning";

Example 

[php]#include <stdio.h>
#include <conio.h>
void main ()
{
char c1[11]={‘g’,’o’,’o’,d’};
char c2[]="morning";
printf("Char Array Value : %s\n", c1);
printf("String Literal Value : %s\n", c2);
getch();
}[/php]

Output

[code]Char Array Value: good  Literal Value is: morning[/code]

String Functions

No. Function Description
1) strlen(string_name) Returns the length of string name.
2) strcpy(destination, source) Copies the contents of source string to destination string.
3) strcat(first_string, second_string) Concats or joins first string with second string. The result of the string is stored in first string.
4) strcmp(first_string, second_string) Compares the first string with second string. If both strings are same, it returns 0.

Hits: 118

Functions In C Programming

What are Functions in C?

Function is the collection of statements which is used to perform some specific task. It provides code reusability and code optimization.

Syntax to declare function in C

[code]return_type function_name(data_type parameter…){
//code
}[/code]

  • return_type indicates the data type of the return value.
  • function_name specify the name of the functions.
  • Parameter are the values which is passed in the function.

A function declaration informs the compiler about a function name and how to call the function. The actual body of the function can be defined separately.

Syntax to call function in C

variable=function_name(arguments...);

Example 

[php]#include <stdio.h>
#include <conio.h>
void sum(int a, int b);
void main()
{
clrscr();
int a, b, c;
c= sum(2,3);
getch();
}
void sum(int a, int b)
{
c = a*b;
printf(“%d”,c)
}[/php]

Output

[code]6[/code]

There are 2 ways of calling function that are

  1. Call by value
  2. Call by reference

1. Call by value
This method copies the actual value of an argument into the formal parameter of the function. In this case, changes made to the parameter inside the function have no effect on the argument.

2. Call by reference[address]

This method copies the address of an argument into the formal parameter. Inside the function, the address is used to access the actual argument used in the call. This means that changes made to the parameter affect the argument.

Recursion 
When function is called within the same function, it is known as recursion in C. A function that calls itself, and doesn’t perform any task after function call, is known as tail recursion.
Syntax

[code]recursionfunction()
{
recursionfunction();//calling self function
}[/code]

Hits: 61

Loops In C programming

In C programs if user want to execute block of code several times here loops are used.in other words loops are used to excecute block of code multiple times until specific condition met.

In C programs we  have 3 types of loops

  1. For Loop
  2. while Loop
  3. Do-while Loop

 

Lets learn more by taking examples for Loops

For Loop

It execute the code until condition is false. it depends on 3 parameters

Syntax

[php]for (Initialization;condition;increment/decrement)

{

//code

}

[/php]

How For loops Works

  1. First initialization executed once
  2. now condition will executed until its false
  3. if condition is true it will increment by 1

Example 

[php]#include&lt;stdio.h&gt;

#include&lt;conio.h&gt;

void main() {

int i;

for( i = 20; i &lt; 25; i++)

{

printf ("%d " , i);

}

getch();}[/php]

while Loop

While loop is executed until condition is false

Synax

[php]while(condition){
//code
}[/php]

Example

 

[php]#include&lt;stdio.h&gt;
#include&lt;conio.h&gt;
void main()
{
int i = 35;
do{
printf ("%d " , i );
i++;
}
while( i &lt; =40 );
getch();
}
[/php]

Output

35
36
37
38
39

Do-while Loop
syntax

[php]do{
//code
}while(condition);
[/php]

Example

[php]
#include<stdio.h>
#include<conio.h>
void main()
{
int i = 10;
do{
printf ("%d " , i );
i++;
}
while( i < =10 );
getch();
}
[/php]

output

10
11

Hits: 26

Flow Charts In C language

Flow Charts Are very Important in C language . These helps in understand the programs  more Easily . Flow charts are meant for this

in C before creating any Program we must have a flow chart for that particular program .

using Flow Charts in Starting helps the program more Understandable

we have some symbols for flowcharts

 

The Oval(The end or beginning)

The Oval Represents  the beginning or the end of program.To start your Program.whenever  oval  symbol is used in the flow charts it will be the End or Start of Program

 

 

The Rectangle 

the rectangle represents the process of the program .

 

 

Arrow

Arrow Represents the directional flow of the program . by this arrow you will stick to the direction of program.

 

Diamond 

Diamond Symbol in Program Flow Charts Represents the  decision that your program will make

Hits: 75

Number System in Computers | Types of Number System | Use and Importance

In Computer Architecture we have 4 Types of Number System and their Conversions . Computer does not understand English or any language .the only language understand by Computers are Binary Numbers or Binary math’s.

In Binary we have only ZERO(0) and One’s (1) . Binary Numbers are used as primary language for Computer’s because binary is easy to store and manipulate . In English we have  26 Alphabets and 0-9 in Numbers  . Every Alphabet of English has different Binary Representation

[su_row][su_column size=”1/4″ center=”no”]ALPHABETS(UPPER)

A 01000001
B 01000010
C 01000011
D 01000100
E 01000101
F 01000110
G 01000111
H 01001000
I 01001001
J 01001010
K 01001011
L 01001100
M 01001101
N 01001110
O 01001111
P 01010000
Q 01010001
R 01010010
S 01010011
T 01010100
U 01010101
V 01010110
W 01010111
X 01011000
Y 01011001
Z 01011010

[/su_column]

[su_column size=”1/4″ center=”no”]ALPHABETS(LOWER)

a 01100001
b 01100010
c 01100011
d 01100100
e 01100101
f 01100110
g 01100111
h 01101000
i 01101001
j 01101010
k 01101011
l 01101100
m 01101101
n 01101110
o 01101111
p 01110000
q 01110001
r 01110010
s 01110011
t 01110100
u 01110101
v 01110110
w 01110111
x 01111000
y 01111001
z 01111010

[/su_column]

[su_column size=”1/4″ center=”no”]DIGITS

0 0
1 1
2 10
3 11
4 100
5 101
6 110
7 111
8 1000
9 1001
10 1010

[/su_column]

[su_column size=”1/4″ center=”no”]SPECIAL

! chr(33) 00100001
chr(34) 00100010
# chr(35) 00100011
$ chr(36) 00100100
% chr(37) 00100101
& chr(38) 00100110
chr(39) 00100111
( chr(40) 00101000
) chr(41) 00101001
* chr(42) 00101010
+ chr(43) 00101011
, chr(44) 00101100
chr(45) 00101101
. chr(46) 00101110
/ chr(47) 00101111
: chr(58) 00111010
; chr(59) 00111011
< chr(60) 00111100
= chr(61) 00111101
> chr(62) 00111110
? chr(63) 00111111
@ chr(64)
[ chr(91) 01011011
chr(92) 01011100
] chr(93) 01011101
^ chr(94) 01011110
_ chr(95) 01011111
` chr(96) 01100000

[/su_column]

[/su_row]

Types Of Number System And Conversions

  • Binary Number System
  • Decimal Number System
  • Octal Number System
  • Hexadecimal Number System

Binary Number System

Binary Number System  is Most popular and is a Base 2 Number System . The basic Number System of Computer is Binary Number System.In term of Electronic circuit or current it can be operate as a On Off Switch

[su_table responsive=”yes”]

ON 1
OFF 0

[/su_table]

Binary Number : (1001)2

[su_table responsive=”yes”]

10012

23+22+21+20

10012

8+0+0+1

10012

9

[/su_table]

Decimal Number System

Decimal Number System  is a Base 10 Number System.it use 10 digits 0-9.reading from left it represents units,ones,tens,hundred,thousand and so on.

[su_table responsive=”yes”]

481910

104+103+102+101

481910

4000+800+10+9

Final Ans

4819

[/su_table]

Octal Number System

Octal Number System is Base 8 Number System. Octal means 8 .Octal Number System is Most Commonly Used By Computers  and its easy to remember in term of computer engineers. it uses 8 digits 0-7.

[su_table responsive=”yes”]

47268

83+82+81+80

47268

4×512+7×16+2×8+6×0

Final Ans

2182

[/su_table]

 

Hexadecimal Number System

Hexadecimal Number system is more Convenient for Computer Engineers as it’s a base 16 Number System and Uses 10 digits and 6 letters

1,2,3,4,5,6,7,8,9,A,B,C,D,E,F

Where is A=10 ,B=11 ………………..F=15

[su_table responsive=”yes”]

45D16

162+161+160

45D16

4×256+5×16+13X0

Final Ans

1117

[/su_table]

 

Number Conversions

[su_row][su_column size=”1/4″ center=”no” class=””]Binary Conversions

  • Binary To Decimal
  • Binary To Octal
  • Binary To HexaDecimal

[/su_column]

[su_column size=”1/4″ center=”no” class=””]Decimal Conversions

 

  • Decimal To Binary
  • Decimal To Octal
  • Decimal To HexaDecimal

[/su_column]

[su_column size=”1/4″ center=”no” class=””]Octal Conversions

 

  • Octal To Binary
  • Octal To Decimal
  • Octal To HexaDecimal

[/su_column]

[su_column size=”1/4″ center=”no” class=””]HexaDecimal Conversions

  • HexaDecimal To Binary
  • HexaDecimal To Octal
  • HexaDecimal To Decimal

[/su_column]

[/su_row]

 

 

Finally we done all the Conversions in Number System .

Hits: 100