Fibonacci series program in c

Here is a simple program that prints out the first n numbers in the Fibonacci series in C:

  1. #include <stdio.h>
  2. int main() {
  3.   int n, i;
  4.   long long int t1 = 0, t2 = 1, nextTerm;

  5.   printf("Enter the number of terms: ");
  6.   scanf("%d", &n);

  7.   printf("Fibonacci Series: ");

  8.   for (i = 1; i <= n; ++i) {
  9.     printf("%lld, ", t1);
  10.     nextTerm = t1 + t2;
  11.     t1 = t2;
  12.     t2 = nextTerm;
  13.   }

  14.   return 0;
  15. }

  • This program first prompts the user to enter the number of terms in the Fibonacci series to be printed. It then uses a for loop to iterate through the terms and prints each one. The loop starts at 1, and it continues until the value of i is greater than n. 
  • On each iteration of the loop, the next term in the series is calculated as the sum of the previous two terms, and the variables t1 and t2 are updated to store the previous two terms. The first two terms of the series are initialized to 0 and 1, respectively.
  • This program uses a for loop to iterate through the first 20 terms in the Fibonacci series. The variables t1 and t2 represent the previous two terms, and the variable nextTerm represents the next term in the series. 
  • The program prints out the value of t1 and then updates the values of t1, t2, and nextTerm for the next iteration of the loop.
Here is a C program that prints the first n numbers of the Fibonacci series:

fibonacci series in c

Enter the number of terms: 12
First 12 terms of Fibonacci series are:
0
1
1
2
3
5
8
13
21
34
55
89
  • This program prompts the user to enter a positive integer n and then generates the first n terms of the Fibonacci series using a loop.  with the first two terms being 0 and 1. The series starts with 0 and 1 and looks like this: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, ...
  • This program prompts the user to enter the number of terms in the Fibonacci series they want to generate, then uses a loop to compute and print out each term. The first two terms of the series are hard-coded as 0 and 1, and the remaining terms are computed by adding the previous two terms together.
  • To understand how this program works, you can read the following explanation:
  • The Fibonacci series is a sequence of numbers in which each number is the sum of the two preceding ones, usually starting with 0 and 1. In this program, the first two numbers of the Fibonacci series are initialized to 0 and 1.
  • The for loop is used to iterate over the numbers of the series. The loop starts from 0 and ends at n-1. At each iteration, the next number in the series is calculated by adding the first and second numbers. The if statement is used to handle the case where the current iteration is 0 or 1, in which case the next number is simply the current iteration.
  • After the next number is calculated, the first and second numbers are updated for the next iteration. Finally, the next number is printed to the screen.
  • I hope this helps! Let me know if you have any questions.

Introduction to C language

Introduction to C


     C is a general purpose programming language. Programming language is the means to communicate with the system to get our thing done.

Need for a programming language:

  We all of us know system can only understand binary representation i.e. in the form of 0's and 1's.

Suppose we have to program which adds two numbers and save the result.

Steps involved:
  • First we should write the values in the memory.
  • Move those values into the registers.(registers are also a kind of memory where the CPU can perform operation on the values stored in the registers.)
  • Add the values in the registers.
  • Save the result in the register to the memory.

Machine Language                                                                                                                                                                                                                                                                               At the beginning machine code is used to write the program. 

Machine level programming consists of binary or hexadecimal instructions on which an processor can respond directly.  

Structure of machine language instruction.
Opcode   Operand 

Opcode represents the operation to be performed by the system on the operands.

In machine code:
Memory     opcode     operand 
location                                                                     
0000:           55            e4     10   // 55 represent move instruction, move 10 to location e4          
0001:           89            e5     20
0003:           83            e4     e5
0006:           54            c4     e4    

Its difficult to read and write opcode for every instruction. So instead of numerical value for instruction there should be an alternative, Then comes the assembly code.

Assembly language:

 Mnemonics are used to represent the opcode.
An Assembly code:
MOV  A, $20           // move value 20 to register A (MOV is a mnemonics)
MOV  B, $10           // move value 10 to register B
ADD   A, B              // Add value in register in A and B
MOV  #30, A           // move value in register A to address 30

Assemblers converts the assembly language into binary instructions.
Assembly language is referred to as low level language,
Assembler languages are simpler than opcodes. Their syntax is easier to read than machine language but as the harder to remember. So, their is a need for other language which is much more easy to read and write the code.
Then comes the C programming language.

C language

In C language:
 int main()
{
     int a,b,c;
     a=10;
     b=20;
     c=a+b;
}

Compiler converts the C language code into assembly language code.

C language is easier to write and read. 

Factorial program in python

#write a python program for factorial of a given number.

num = int(input("Enter a number: "))
factorial = 1
if num < 0:
   print("factorial not exist for negative numbers")
elif num == 0:
   print("The factorial of 0 is 1")
else:
   for i in range(1,num + 1):
       factorial = factorial*i
   print("The factorial of",num,"is",factorial)

  • We store the number we give in the num variable and by using the if, elif, and else loop we are executing the program. if statement executes when the given condition is true and same for elif also. After the iteration, it finally returns the factorial of the number we are given.
  • Normally we write as 12!=12*11*10*9*8*7*6*5*4*3*2*1=479001600




find substring python

 find substring():

  • The find substring is used to find the substring from the string.
  • Substring is the piece of characters from the string.
  • If the string consists of the given string then it returns the index number of the substring. 
  • The string must be enclosed with single or double-quotes.
  • It returns -1 if the substring does not found instead of an error.
  • find() has 3 parameters,they are:
  1. value: The value to be search
  2. start:  from where to search
  3. end:  from where to stop searching.
Syntax: str.find(value,start,end)

#write a python code to demonstrate the find substring function.

  1. txt="learning is a life time process"
  2. txt=txt.find("i")
  3. print(txt)
  4. s="apple"
  5. s=s.find("b")
  6. print(s)

Output: 5
              -1







python replaceall

  • In python, replace all is similar to replace() function. 
  • Here we are going to replace all the characters with new characters.
  • It also consists of 3 parameters like new value, old value, count.
  • The old value is the string that we have already given in string.
  • The new value is the that is to be replaced with.
  • The count is the number of times that string to be replaced in the string.
Syntax:  str.replace(old value,new value,count)

#write a python code to demonstrate the replace function.

  1. s="hello world"
  2. s=s.replace("hello world","my name is hello")
  3. print(s)
Output:    my name is hello


format function in python

  •  The format() function is used to format a specific value and insert that value in string placeholders.
  •  The placeholders are cost{20}, number index{1},empty{}
  • The place holder is defined by" curly braces"{}.
  • The format function consists of parameters called value.
  • It returns the formatted string.
Syntax:  str.format(values)

  • Inside the placeholders, we can add different formatting types
  • :+    indicates positive values.
  • : -    indicates negative values.
  • :      indicates the space before positive and also negative numbers.
  • :_    indicates as a thousand separator.
  • :,     uses as comma thousand separator.
  • :=    left most position.
  • :<    result in left alignment.
  • :>    result in right alignment.
  • :^     result in the center.
  • :%    percentage format.
  • :b     Binary format.
  • :c     Unicode format.
  • :d     Decimal format.
  • :e      format with lower case.
  • :E     format with upper case.
  • :f      format with fixpoint number.
  • :o    format is octal.
  • :x      format is Hexa.
  • :X     format is Hexa in uppercase.
  • :n      number format.

#write a python code to demonstrate format function.

  1. s0="my name is {fname},I m {age}".format("fname=abc",age=19)
  2. s1="my name is{0},I m {1}".format("abc",19)
  3. s2="my name is {},I m {}".format("abc",19)
  4. print(s0)
  5. print(s1)
  6. print(s2)

Output:    my name is abc, I m 19.
                 my name is abc, I m 19.
                 my name is abc, I m 19.


Select Menu