Posts

Showing posts from October, 2020

linear search array

 #include <stdio.h> void display(int arr[] , int size){     for (int i=0;i<size;i++){         printf("%d ",arr[i]);     } } void lin_search(int arr[],int size , int element, int capacity ){     int count=0;     for (int i=0;i<size;i++){                  if (arr[i]==element){             count=count+1;                  }         else{             count=count+0;         }              }            if(count>=1){              printf("\nelement found\n");                      }         else{              printf("\nelemen...

deletion in array

 #include <stdio.h> void display(int arr[] , int size){     for (int i=0;i<size;i++){         printf("%d ",arr[i]);     } } int delete(int arr[],int size ,  int capacity , int index){          if (size>=capacity){         return -1;     }     for (int i=index;i<size;i++){         arr[i]=arr[i+1];     }      } int main() {     // Write C code here     int arr[100]={2,34,54,32,34};     int size=5 , capacity=100 , index=2;     printf("Before insertion : \n");     display(arr,size);     delete(arr , size , capacity , index);     size = size-1;     printf("\n");     printf("After deletion : \n");     display(arr,size);                    return 0; }

insertion in array

#include <stdio.h> void display(int arr[] , int size){     for (int i=0;i<size;i++){         printf("%d ",arr[i]);     } } int insert(int arr[],int size , int element , int capacity , int index){     if (size>=capacity){         return -1;     }     for (int i=size-1;i>=index;i--){         arr[i+1]=arr[i];     }     arr[index]=element;      } int main() {     // Write C code here     int arr[100]={2,34,54,32,34};     int size=5 , capacity=100 , element=45 , index=2;     printf("Before insertion : \n");     display(arr,size);     insert(arr , size ,element, capacity , index);     size = size+1;     printf("\n");     printf("After insertion : \n");     display(arr,size);                ...

NO. REVERSER

 #include <stdio.h> int main() {     // Write C code here     int a,b=0,c,d,e;     printf("enter the no : ");     scanf("%d",&a);     while(a/10!=a){     printf("%d\n",a%10);     b=10*(b+(a%10));     a=a/10;     }     printf("%d",b/10); //***actual method*** #include <stdio.h> int main() {     // Write C code here     int a,b=0,c,d,e;     printf("enter the no : ");     scanf("%d",&a);     while(a/10!=a){     printf("%d\n",a%10);     b=b*10+a%10;     a=a/10;     }     printf("%d",b);          return 0; }          return 0; }

function factorial

 #include <iostream> using namespace std; int factorial(int n){     if(n<=0){         return 1;     }     else{         return (n)*factorial(n-1);     } } int main() {     // Write C++ code here     int n1,n2;     cout<<"enter 1 no : ";     cin>>n1;     cout<<"enter 2 no : ";     cin>>n2;     factorial(n1);     factorial(n2);     cout<<factorial(n1)<<endl;     cout<<factorial(n2)<<endl;         return 0; }

prime fn

 #include <iostream> using namespace std; void is_prime(int n){     int counter=0;     if (n==0 || n==1){                  counter=1;     }     for (int i =2;i<n;i++){         if(n%i==0){             counter = counter+1;         }         else{             counter = counter+0;                     }               }     if(counter==0){     printf("it is  prime");         }     else{         printf("It is not prime");     } } int main() {     int a;     cout<<"Enter a no. : ";     cin>>a;     is_prime(a);          return 0; }

HACKERANK 11

  Objective This challenge will help you learn the concept of recursion. A function that calls itself is known as a recursive function. The C programming language supports recursion. But while using recursion, one needs to be careful to define an exit condition from the function, otherwise it will go into an infinite loop. To prevent infinite recursion,   statement (or similar approach) can be used where one branch makes the recursive call and other doesn't. void recurse () { ..... recurse () //recursive call ..... } int main () { ..... recurse (); //function call ..... } Task There is a series,  , where the next term is the sum of pervious three terms. Given the first three terms of the series,  ,  , and   respectively, you have to output the  n th  term of the series using recursion. Recursive method for calculating  n th  term is given below. Input Format The first line contains a sin...

HACKERANK FOR LOOP C

  Objective In this challenge, you will learn the usage of the  for  loop, which is a programming language statement which allows code to be executed until a terminal condition is met. They can even repeat forever if the terminal condition is never met. The syntax for the  for  loop is: for ( <expression_1> ; <expression_2> ; <expression_3> ) <statement> expression_1  is used for intializing variables which are generally used for controlling the terminating flag for the loop. expression_2  is used to check for the terminating condition. If this evaluates to false, then the loop is terminated. expression_3  is generally used to update the flags/variables. The following loop initializes   to 0, tests that   is less than 10, and increments   at every iteration. It will execute 10 times. for(int i = 0; i < 10; i++) { ... } Task For each integer   in the interval   (gi...