Skip to main content

Posts

Showing posts from December, 2014

Program to find sum of first 1000 Primes Numbers

#include <stdio.h> int main (int argc,char *argv[]) {         int i,j,sum = 0,range = 1000;         int flag = 0,count = 0;         for (i = 2 ;count < 1000;i++) {                 //check for prime                 for (j = 2; j < i ;j++) {                         if (i % j == 0) {                                 flag = 1;break;                         }                 }                 if (flag == 0) {                         sum = sum + i;                         count++;                 }                 flag = 0;         }         printf("Sum of First 1000 Prime numbers = %d\n",sum);         return 0; } Output - Sum of First 1000 Prime numbers = 3682913

Program for Largest Prime Palindrome (1 - 1000)

/* Program to print largest prime palindrome with range (1-1000)     Problem - https://www.codeeval.com/open_challenges/3/ */ #include <iostream> using namespace std; int main() {         int num,i,j,digit = 0,rev = 0;         int flag = 0;         for (i = 1000;i >= 2;i--) {                 flag = 0,rev = 0;                 //check for prime                 for ( j =2; j < i /2;j++) {                         if ( i % j == 0) {                                 flag = 1;break;                 }         }         if (!flag) {                 //if prime check for palindrome                 num = i,rev = 0;                 while (num > 0) {                         digit = num % 10;                         rev = rev * 10 + digit;                         num /= 10;                 }         }         if ( i == rev) {                 cout << "Largest Prime Palindrome between (1-1000) = " << i << endl;                

Solution Program for Fizz Buzz Problem

#include <stdio.h> #include <stdlib.h>   /*   * Solution Program for FizzBuzz (codeeval.com) - https://www.codeeval.com/open_challenges/1/   */ int main(int argc,char *argv[]) {         FILE *fp;         int x,y,count,i;         if (argc < 2) {          printf("Error :- File not specified \n");          exit(0);         }         fp = fopen (argv[1],"r");         if (fp == NULL) {                 printf("Error :- Can't open file \n");                 exit(0);         }         while (fscanf(fp,"%d %d %d",&x,&y,&count) != EOF ) {                 printf("Read - X = %d Y = %d Count = %d\n",x,y,count);                 if ( x < 1 || x > 20 || y < 1 || y > 20 || count < 21 || count > 100) {                         printf("Invalid Input - please use Ranges X[1-20] Y[1-20] Z[21-100]\n");                         exit(0);                 }                 fo

Program to print a non repeated character in string

#include<stdio.h> #include<string.h> /* Program to print a non repeated character in string  *  */ int main() {         int count[256] = {0}; /* count of the characters */         char string[20];         int i;         printf("Enter the string \n");         scanf("%s",string);         for (i = 0;i < strlen(string)-1;i++) {                 count[*(string+i)]++;   /* increments the count based on ASCII Range */         }         for (i = 0;i < strlen(string)-1;i++) {                 if (count[*(string+i)]== 1) { /* Count with 1 is the character */                         printf("count = %d,char = %c\n",count[*(string+i)],*(string+i));                         break;                 }         }         return 0; } Input - Enter the string - teeths Output - count = 1,char = h