We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Beginner-ish way to solve it for C . I am not sure about Running time and Complexity though.
#include<stdio.h>#include<string.h>#include<math.h>#include<stdlib.h>intmain(){// initialize T and dynamically allocate array sizeintT=0;scanf("%d",&T);int*arr=malloc(T*(sizeof(int)));// make a flagintisPrime=1;for(inti=0;i<T;i++){// reset the flag each iterationisPrime=1;scanf("%d",&arr[i]);// check numbers less than 2if(arr[i]<2){isPrime=0;}// else we check numbers between// [2, sqrt(number)]else{for(intj=2;j<=sqrt(arr[i]);j++){// if its divisible// break loopif(arr[i]%j==0){isPrime=0;break;}}}// print the values according to flagif(isPrime){printf("Prime");}else{printf("Not prime");}printf("\n");}return0;}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Day 25: Running Time and Complexity
You are viewing a single comment's thread. Return to all comments →
Beginner-ish way to solve it for C . I am not sure about Running time and Complexity though.