fabtab={}
def fabonacci(n):
fabtab[0]=0
fabtab[1]=1
for i in range(2,n+1):
fabtab[i]=fabtab[i-1]+fabtab[i-2]
print(fabtab[i])
return fabtab[n]
num=int(input("Enter the number to generate fabonacci series : "))
print("The fabonacci series are.....")
print(fabonacci(num))
print(fabtab.values())
Tuesday, 28 November 2017
Python Program to find Fabonacci
Python Program to Find Factorial
fact={}
def factorial(n):
fact[0]=1
fact[1]=1
if n==0 and n==1:
return 1
else:
for i in range(2,n+1):
fact[i]=i*fact[i-1]
return fact[n]
num=int(input("Enter number to find factorial : "))
print("Factorial of ",num," is ",factorial(num))
print("The values")
print(fact)
Output
Wednesday, 22 November 2017
C Program to Delete Element from Array
#include<stdio.h
>
#include<stdlib.h
>
#include<conio.h
>
int arr[100],size;
void del_beg();
void del_pos();
void del_last();
void del();
void display();
int main(){
int ch;
printf("Enter the size of the array : ");
scanf("%d",&size);
printf("Enter the array elements one by one.....\n");
for(int i=0;i<size
;i++){
scanf("%d",&arr[i]);
}
while(1){
printf("\n\t\t1. Deletion at beginning\n\t\t2. Deletion at a given possition\n\t\t3. Deletion at last\n\t\t4. Delete by number\n\t\t5. Display\n\t\t6. Exit\n");
scanf("%d",&ch);
switch(ch){
case 1:
del_beg();
break;
case 2:
del_pos();
break;
case 3:
del_last();
break;
case 4:
del();
break;
case 5:
display();
break;
case 6:
exit(0);
break;
default:
printf("Invalid Choice.....Try Again.....\n");
}
}
}
void del_beg(){
printf("The fisrt element %d of the array is deleted.\n",arr[0]);
for(int i=0;i<size
;i++){
arr[i]=arr[i+1];
}
size--;
printf("Array After deletion.....\n");
for(int i=0;i<size
;i++){
printf("%d\t",arr[i]);
}
printf("\n");
}
void del_pos(){
int pos;
printf("Enter the location which element you want to delete : ");
scanf("%d",&pos);
if(pos<=size){
printf("The element at %d possition i.e. %d is deleted.\n",pos,arr[pos-1]);
for(int i=pos-1;i<=size;i++){
arr[i]=arr[i+1];
}
size--;
printf("Array After deletion.....\n");
for(int i=0;i<size
;i++){
printf("%d\t",arr[i]);
}
printf("\n");
}
else{
printf("Out of array range.\n");
}
}
void del_last(){
printf("The last element i.e. %d is deleted.\n",arr[size-1]);
for(int i=size;i<size+1
;i++){
arr[i]=arr[i+1];
}
size--;
printf("Array After deletion.....\n");
for(int i=0;i<size
;i++){
printf("%d\t",arr[i]);
}
printf("\n");
}
void del(){
int item,x=0,flag=0;
printf("Enter item to delete : ");
scanf("%d",&item);
for(int i=0;i<size
;i++){
if(arr[i]==item){
x=i;
flag=1;
break;
}
else{
flag=0;
}
}
if(flag==1){
printf("The element %d is deleted.\n",item);
for(int i=x;i<=size;i++){
arr[i]=arr[i+1];
}
size--;
printf("Array After deletion.....\n");
for(int i=0;i<size
;i++){
printf("%d\t",arr[i]);
}
printf("\n");
}
else{
printf("The entered element %d is not found in array list.\n",item);
}
}
void display(){
printf("The elements of array.....\n");
for(int i=0;i<size
;i++){
printf("%d\t",arr[i]);
}
printf("\n");
}
Output
Deleting First Element
Deleting Element from a given possition
Deleting Last Element
Deleting Element by Number
C Program to Insert Element into Array
#include<stdio.h
>
#include<stdlib.h
>
#include<conio.h
>
int arr[100], size=0;
void insert_beg();
void insert_pos();
void insert_last();
void display();
int main(){
int choice;
printf("Enter size of the array : ");
scanf("%d",&size);
printf("Enter the array elements one by one.....\n");
for(int i=0;i<size
;i++){
scanf("%d",&arr[i]);
}
while(1){
printf("\n\t\t1. Insert at beginning\n\t\t2. Insert at given possition\n\t\t3. Insert at last\n\t\t4. Display array elements\n\t\t5. Exit\n");
scanf("%d",&choice);
switch(choice){
case 1:
insert_beg();
break;
case 2:
insert_pos();
break;
case 3:
insert_last();
break;
case 4:
display();
break;
case 5:
exit(0);
default:
printf("Invalid Choice.....Please try again.....\n");
}
}
getch();
}
void insert_beg(){
int item;
printf("Enter element to insert : ");
scanf("%d",&item);
for(int i=size;i>0;i--){
arr[i]=arr[i-1];
}
arr[0]=item;
size++;
printf("The entered item %d is inserted at beginnng.\n",item);
for(int i=0;i<size
;i++){
printf("%d\t",arr[i]);
}
}
void insert_pos(){
int item, pos;
printf("Enter item to insert : ");
scanf("%d",&item);
printf("Enter possition at which you want to insert : ");
scanf("%d",&pos);
for(int i=size;i>=pos;i--){
arr[i]=arr[i-1];
}
arr[pos-1]=item;
size++;
printf("The entered element %d is inserted at %d possition.\n",item,pos);
for(int i=0;i<size
;i++){
printf("%d\t",arr[i]);
}
}
void insert_last(){
int item;
printf("Enter element to insert at last : ");
scanf("%d",&item);
for(int i=size;i>=size+1;i--){
arr[i]=arr[i-1];
}
arr[size]=item;
size++;
printf("The entered item %d is inserted at last.\n",item);
for(int i=0;i<size
;i++){
printf("%d\t",arr[i]);
}
printf("\n");
}
void display(){
printf("The array elements.....\n");
for(int i=0;i<size
;i++){
printf("%d\t",arr[i]);
}
printf("\n");
}
Output
Subscribe to:
Posts (Atom)
Python Program to find Fabonacci
fabtab={} def fabonacci(n): fabtab[0]=0 fabtab[1]=1 for i in range(2,n+1): fabtab[i]=fabtab[i-1]+fabtab[i-2] ...
-
#include stdio.h > #include stdlib.h > #include conio.h > int arr[100], size=0; void insert_beg(); void insert_pos(); void inser...
-
#include iostream> #include conio.h > using namespace std; int main() { int n,num,first=0,middle,last; ...
-
#include iostream > #include conio.h > using namespace std; int main() { float radius; double area; ...