C programs

June 29, 2017 | Autor: Sathish Ravi | Categoria: Programming Languages
Share Embed


Descrição do Produto

Fibonacci Series in C
Fibonacci Series in C: In case of fibonacci series, next number is the sum of previous two numbers for example 0, 1, 1, 2, 3, 5, 8, 13, 21 etc. The first two numbers of fibonacci series are 0 and 1.
There are two ways to write the fibonacci series program:
Fibonacci Series without recursion
Fibonacci Series using recursion
Fibonacci Series in C without recursion
Let's see the fibonacci series program in c without recursion.
#include
#include
void main()
{
 int n1=0,n2=1,n3,i,number;
 clrscr();
 printf("Enter the number of elements:");
 scanf("%d",&number);
 printf("\n%d %d",n1,n2);//printing 0 and 1

 for(i=2;i0)
{
r=n%10;
sum=(sum*10)+r;
n=n/10;
}
if(temp==sum)
printf("palindrome number ");
else
printf("not palindrome");
getch();
}
Output:
enter the number=151
palindrome number
enter the number=5621
not palindrome number
Factorial Program in C
Factorial Program in C: Factorial of n is the product of all positive descending integers. Factorial of n is denoted by n!. For example:
5! = 5*4*3*2*1 = 120
3! = 3*2*1 = 6
Here, 5! is pronounced as "5 factorial", it is also called "5 bang" or "5 shriek".
The factorial is normally used in Combinations and Permutations (mathematics).
There are many ways to write the factorial program in c language. Let's see the 2 ways to write the factorial program.
Factorial Program using loop
Factorial Program using recursion
Factorial Program using loop
Let's see the factorial Program using loop.
#include
#include
void main(){
int i,fact=1,number;
clrscr();
printf("Enter a number: ");
scanf("%d",&number);

for(i=1;i0;i++)
{
a[i]=n%2;
n=n/2;
}
printf("\nBinary of Given Number is=");
for(i=i-1;i>=0;i--)
{
printf("%d",a[i]);
}
getch();
}
Output:
Enter the number to convert: 5
Binary of Given Number is=101
C Program to print Alphabet Triangle
There are different triangles that can be printed. Triangles can be generated by alphabets or numbers. In this c program, we are going to print alphabet triangles.
Let's see the c example to print alphabet triangle.
#include
#include

void main(void)
{
int ch=65;
int i,j,k,m;
clrscr();
for(i=1;i=i;j--)
printf(" ");
for(k=1;k0)
{
r=n%10;
sum=sum*10+r;
n=n/10;
}
n=sum;
while(n>0)
{
r=n%10;
switch(r)
{
case 1:
printf("one ");
break;
case 2:
printf("two ");
break;
case 3:
printf("three ");
break;
case 4:
printf("four ");
break;
case 5:
printf("five ");
break;
case 6:
printf("six ");
break;
case 7:
printf("seven ");
break;
case 8:
printf("eight ");
break;
case 9:
printf("nine ");
break;
case 0:
printf("zero ");
break;
default:
printf("tttt");
break;
}
n=n/10;
}
getch();
}
Output:
enter the number=4321
four three two one
Sum of digits program in C
C program to sum each digit: We can write the sum of digits program in c language by the help of loop and mathematical operation only.
Sum of digits algorithm
To get sum of each digits by c program, use the following algorithm:
Step 1: Get number by user
Step 2: Get the modulus/remainder of the number
Step 3: sum the remainder of the number
Step 4: Divide the number by 10
Step 5: Repeat the step 2 while number is greater than 0.
Let's see the sum of digits program in C.
#include
#include
void main()
{
int n,sum=0,m;
clrscr();
printf("Enter a number:");
scanf("%d",&n);
while(n>0)
{
m=n%10;
sum=sum+m;
n=n/10;
}
printf("Sum is=%d",sum);
getch();
}
Output:
Enter a number:654
Sum is=15
Enter a number:123
Sum is=6
1) What is C language?
C is a mid level and procedural programming language. More details...

2) Why C is known as a mother language?
C is known as a mother language because most of the compilers, kernals and JVMs are written in C language. More details...

3) Why C is called a mid level programming language?
It supports the feature of both low-level and high level languages that is why it is known as a mid level programming language. More details...

4) Who is the founder of C language?
Dennis Ritchie. More details...

5) When C langauge was developed?
C language was developed in 1972 at bell laboratories of AT&T. More details...

6) What are the features of C language?
The main features of C language are given below:
Simple
Portable
Mid Level
Structured
Fast Speed
Memory Management
Extensible
More details...

7) What is the use of printf() and scanf() functions?
The printf() function is used for output and scanf() function is used for input.
More details...

8) What is the difference between local variable and global variable in C?
Local variable: A variable which is declared inside function or block is known as local variable.
Global variable: A variable which is declared outside function or block is known as global variable.
int value=50;//global variable
void function1(){
int x=20;//local variable
}
More details...

9) What is the use of static variable in C?
A variable which is declared as static is known as static variable. The static variable retains its value between multiple function calls.
void function1(){
int x=10;//local variable
static int y=10;//static variable
x=x+1;
y=y+1;
printf("%d\n",x);//will always print 11
printf("%d\n",y);//will always increment value, it will print 11, 12, 13 and so on
}
More details...

10) What is the use of function in C?
A function in C language provides modularity. It can be called many times. It saves code and we can reuse the same code many times. More details...

11) What is the difference between call by value and call by reference in C?
We can pass value to function by one of the two ways: call by value or call by reference. In case of call by value, a copy of value is passed to the function, so original value is not modified. But in case of call by reference, an address of value of passed to the function, so original value is modified. More details...

12) What is recursion in C?
Calling the same function, inside function is known as recursion. For example:
void function1(){
function1();//calling same function
}
More details...

13) What is array in C?
Array is a group of similar types of elements. It has contiguous memory location. It makes the code optimized, easy to traverse and easy to sort. More details...

14) What is pointer in C?
A pointer is a variable that refers to the address of a value. It makes the code optimized and makes the performance fast. More details...

15) What are the usage of pointer in C?
Accessing array elements
Dynamic memory allocation
Call by Reference
Data Structures like tree, graph, linked list etc.

16) What is NULL pointer in C?
A pointer that doesn't refer to any address of a value but NULL, is known as NULL pointer. For example:
int *p=NULL;
More details...

17) What is far pointer in C?
A pointer which can access all the 16 segments (whole residence memory) of RAM is known as far pointer.

18) What is dangling pointer in C?
If a pointer is pointing any memory location but meanwhile another pointer deletes the memory occupied by first pointer while first pointer still points to that memory location, first pointer will be known as dangling pointer. This problem is known as dangling pointer problem.

19) What is pointer to pointer in C?
In case of pointer to pointer concept, one pointer refers to the address of another pointer. More details...

20) What is static memory allocation?
In case of static memory allocation, memory is allocated at compile time and memory can't be increased while executing the program. It is used in array. More details...

21) What is dynamic memory allocation?
In case of dynamic memory allocation, memory is allocated at run time and memory can be increased while executing the program. It is used in linked list. More details...

22) What functions are used for dynamic memory allocation in C language?
malloc()
calloc()
realloc()
free()
More details...

23) What is the difference between malloc() and calloc()?
malloc(): The malloc() function allocates single block of requested memory. It has garbage value initially.
calloc(): The calloc() function allocates multiple block of requested memory. It initially initializes all bytes to zero.
More details...

24) What is structure?
Structure is a user-defined data type that allows to store multiple types of data in a single unit. It occupies the sum of memory of all members. More details...

25) What is union?
Like Structure, union is a user-defined data type that allows to store multiple types of data in a single unit. But it doesn't occupies the sum of memory of all members. It occupies the memory of largest member only. More details...


Lihat lebih banyak...

Comentários

Copyright © 2017 DADOSPDF Inc.