Sunday, March 13, 2011

C program to print prime numbers in a given range

Program
#include <stdio.h>

int main()
{
  int a,b,num,n,i;

  printf("\nEnter the number range (a b) (a > 1 and b > 1): ");
  scanf("%d %d",&a,&b);

  n=0;
  for(num=a;num<=b;num++)
    {
      for(i=1;i<=num/2;i++)
 {
   if (num%i == 0 && i != 1 && num != 2 && num != 3)
     break;
   else
     {
       if (i == num/2)
  {
    printf("\n%d is a prime number.\n",num);
    n=n+1;
  }
       continue;
     }
 }
    }
  
  if (n == 0) printf("\nNo prime numbers in the range %d - %d\n",a,b);
  else printf("\n%d prime numbers in the range %d - %d\n",n,a,b);

  return 0;
}
Compilation, Run and Output
[sreedhar@manchu2 cprograms]$ gcc prime_range.c -o prime_range
[sreedhar@manchu2 cprograms]$ ./prime_range 

Enter the number range (a b) (a > 1 and b > 1): 2 50

2 is a prime number.

3 is a prime number.

5 is a prime number.

7 is a prime number.

11 is a prime number.

13 is a prime number.

17 is a prime number.

19 is a prime number.

23 is a prime number.

29 is a prime number.

31 is a prime number.

37 is a prime number.

41 is a prime number.

43 is a prime number.

47 is a prime number.

15 prime numbers in the range 2 - 50
[sreedhar@manchu2 cprograms]$

C Program to check whether a given number is a prime number

Program
#include <stdio.h>

int main()
{
  int num,i;

  printf("\nEnter the number: ");
  scanf("%d",&num);

  if (num == 1) printf("\nNumber 1 is neither primie nor composite.\n");
  void exit();

  for(i=1;i<=num/2;i++)
    {
      if (num%i == 0 && i != 1 && num != 2 && num != 3)
 {
   printf("\n%d is not a prime number.\n",num);
   break;
 }
      else
 {
   if (i == num/2) printf("\n%d is a prime number.\n",num);
   continue;
 }
    }
  return 0;
}
Compilation, Run and Output
[sreedhar@manchu2 cprograms]$ gcc prime.c -o prime
[sreedhar@manchu2 cprograms]$ ./prime

Enter the number: 24

24 is not a prime number.
[sreedhar@manchu2 cprograms]$ ./prime

Enter the number: 23

23 is a prime number.
[sreedhar@manchu2 cprograms]$ ./prime

Enter the number: 2

2 is a prime number.
[sreedhar@manchu2 cprograms]$ ./prime

Enter the number: 1

Number 1 is neither primie nor composite.
[sreedhar@manchu2 cprograms]$

The Next Three Days

Watched this movie just now. Loved it. Russel Crowe was simply awesome. I wish Elizabeth Banks' role was played by some other woman. Luckily, she doesn't have that big role even though whole movie revolves around her. Believe me it would make you tense. Yeah, it will. But, it's for good. RedBox has it now. So go and get it soon before you postpone forever.

Saturday, March 12, 2011

C Program to print armstrong numbers in a given range of numbers

Program
#include <stdio.h>

int main()
{
  int num,n,m,digits,sum,a,b,i;
  
  printf("\nEnter the range (a b): ");
  scanf("%d %d",&a,&b);
  
  num=0;
  for(i=a;i<=b;i++)
    {
      if ( i == a)
         {
            digits=0;
            m=i;
            while (m != 0)
               { 
           digits++;
                  m=m/10;
               }
          } 
       
       if ( (i != a) && (i/power(10,digits) == 1)) digits++;
          

      sum=0;
      n=i;
      while (n != 0)
 {
   sum=sum+power(n%10,digits);
   n=n/10;
 }
      if (sum == i)
 {
   printf("\n%d is an Armstrong number\n\n",i);
   num++;
 }
    }
  printf("\nThere are %d Armstrong numbers in the range %d - %d\n\n",num,a,b);
  return 0;
}

int power(int x, int y)
{
  int xpy,i;
  
  xpy=1;
  for (i=1;i<=y;i++)
    xpy=xpy*x;
  
  return(xpy);
}

Compilation, Run and Output
[sreedhar@manchu2 cprograms]$ gcc armstrong_range.c -o armstrong_range
[sreedhar@manchu2 cprograms]$ ./armstrong_range 

Enter the range (a b): 11 1000000

153 is an Armstrong number


370 is an Armstrong number


371 is an Armstrong number


407 is an Armstrong number


1634 is an Armstrong number


8208 is an Armstrong number


9474 is an Armstrong number


54748 is an Armstrong number


92727 is an Armstrong number


93084 is an Armstrong number


548834 is an Armstrong number


There are 11 Armstrong numbers in the range 11 - 1000000

[sreedhar@manchu2 cprograms]$

C Program to check whether a given number is an Armstrong number

Definition: Armstrong number is a number that is the sum of its own digits each raised to the power of the number of digits.

Program
#include<stdio.h>

int main()
{
  int num,n,m,sum,digits;
  printf("\nEnter the number: ");
  scanf("%d",&num);

  digits=0;
  m=num;
  while (m != 0)
    {
      digits++;
      m/=10;
    }

  sum=0;
  n=num;
  while (n != 0)
    {
      sum=sum+power(n%10,digits);
      n=n/10;
    }
  if (sum == num)
    {
      printf("\n%d is an Armstrong number\n\n",num);
    }
  else
    {
      printf("\n%d is not an Armstrong number\n\n",num);
    }
  return 0;
} 

int power(int x, int y)
{
  int xpy,i;
  
  xpy=1;
  for (i=1;i<=y;i++)
    xpy=xpy*x;
  
  return(xpy);
}
Compilation, Run and Output
[sreedhar@manchu2 cprograms]$ cc armstrong.c -o armstrong
[sreedhar@manchu2 cprograms]$ ./armstrong

Enter the number: 152

152 is not an Armstrong number

[sreedhar@manchu2 cprograms]$ ./armstrong

Enter the number: 153

153 is an Armstrong number

[sreedhar@manchu2 cprograms]$ ./armstrong

Enter the number: 1634

1634 is an Armstrong number

[sreedhar@manchu2 cprograms]$ ./armstrong

Enter the number: 54748

54748 is an Armstrong number

[sreedhar@manchu2 cprograms]$

Starting and Quitting Applications on Mac OS X through AppleScript and iCal

This time some useful stuff here unlike my previous posts which were nothing but useless ramblings on my life. At my job I need to be on Adium application all the day. If you don't know what Adium is it's nothing but free IM client for Mac OS X that supports multiple IM networks like Google Talk, Yahoo Messenger, etc.

Early in the morning, as soon as I go to work I don't have problems to remember to log on to Adium. But most of the times I tend to forget to log out of it before I leave at the end of the day. So, I thought of making it automatic through some script. Since my OS is Mac OS X I thought it would be better if I used AppleScript. So started my foray on to it.

My script looks like below to start and quit application Adium. You can pretty much start and stop any application like this by replacing Adium with it's name.

Script to start the application
tell application "Adium"
    activate
end tell

Script to stop the application
tell application "Adium"
    quit
end tell

I don't think there is any need to explain the script as it is self explanatory. This is a very trivial script. You can do crazy stuff with this AppleScript. Just google for it and you find tonnes of pages.

As you can see it doesn't tell anything about the time it's going to start and stop Adium. Good catch! The trick is to use iCal for this purpose. Create a new event and under alarm options choose "run script". It let's you choose your saved above script. In the event you can specify at what time everyday it should start. For example, I set up two recurring events on week days. First event starts at 9AM and runs the first script. I named the script as start_adium. The second event runs at 5PM and runs the second script through alarm settings which I named it as stop_adium.

That's it. Now it's automated to start and stop Adium on every week day. Cool. Ha! But there are disadvantages to it too. Even if you are not on time to the office script gets run exactly at the specified time. Which means people think that you are there in the office. Again I don't care about this and it works for me. I'm sure there must be ways to write a script to make it start and stop as soon as you unlock and lock the screens. I guess no one forgets to lock the screen if he is a system admin. Right? Since I have this idea right now I'll try to look into this and let you know if I find something.

Note: Use shortcut "Command + Space bar" to initiate Spotlight and type applescript. As you type you would see AppleScript Editor next to applications. Hit enter and you see an AppleScript editor to write your script. Just for start, you can copy my script on it and run it to see what happens. Use some other application name in the place of Adium if you don't have it on your system. Hope this helps someone.

Friday, August 27, 2010

Hope for a new beginning

It's been a while since I wrote my last blog. Not much has changed in all this time. I'm still struggling to find a break. I've always thought that it'd be easy to survive once we find some initial break. The trick is finding that break. The only changes that took place in my life since my last blog are I moved to a new place and I've started playing the league again.

My lease for the last place was over last month. I was so broke I clearly understood one thing for sure that it was expensive to stay at that place. It was kind of crazy to pay $1200.00 for studio apartment. Since I'm totally broke, now I'm not in a position to pay for rent. So I'm staying with my friends hoping that one day I'd be able to find a job and get my own place.

Coming to the second change, yep I've started playing the cricket league again. I've always excelled in the cricket field. I'm definitely not a very quick bowler. When I first joined engineering college, I think I was genuinely quick. I could always change my bowling action to generate more pace apart from tricking batsmen. I'm not sure whether it is the age or my back surgery 4 years back that caused drop in my pace. Now I definitely don't bowl that fast anymore. But all my life until now I've always known how to swing a ball. I think it always brought me lot of wickets. That too I always got very good batsmen. When ever I get to bowl these days, with out hesitation I try to bowl variety of balls. It took long time to understand it really doesn't matter even if you get hit while trying to bowl something different.

Anyway, coming back to present situation regarding my job search the only positive thing I've noticed is I'm getting calls from employers. At the beginning of this month I was very excited to get a call from Nvidia. Of course, after that call I was expecting another call but it never materialized. It would have been great if I did. In my opinion GPU computing is the future. CPU days are over when it comes to higher performance computing. May be that's an overstatement. I can say it's going to be GPU in the next few years. I thought if I could get into Nvidia I'd be walking down the brightest path for the future. May be it's not meant to be.

Then there was a call from Cray, Inc. I guess working for any supercomputing company would be awesome. Recently, I've applied for lot of HPC jobs and I'm hoping to get into one of those big companies like Intel, Nvidia, etc. If I could get a chance to attend an interview, I think I can pull it off. It's a matter of how well you can convince them of your abilities. After that it all depends on how good you are at doing stuff. I think I can do good programming. I've never hesitated to say I can be a good programmer. Unfortunately, until now I've never got a chance. Hopefully it should all change in the near future.

This season I've played 4 matches so far. First 3 matches were patchy in terms of my performance. I guess I bowled well if not exceptional. I think I switched gears to an extra level in my last match. I did very well in every department. I batted down the lower order and played a crucial innings to save my team from sinking. I played some audacious shots. In the end it felt great when my team mates and people from other team congratulated on my batting. Then it self I was sure that I'd bowl well.

Luckily, this time I got a chance to open the bowling and I stuck with my 2nd ball it self. For some reason, I've always loved to bowl at left handed batsmen. I've always loved the challenges like bowling to left handed batsmen. It's a challenge because of one simple reason. Most of the people find it very difficult to bowl to left handed batsmen as they'd have to change their side and shooting angle. I've always loved to make my self excel where other people find it difficult. Like I said I can swing both ways and in my last 2 matches I bowled exceptional balls to bowl 2 left handed batsmen. In my third match I bowled an in-swinger so good that it took middle stump from no where. It landed almost a foot away from the off-stump and batsman had no idea when it swung in late to take off his middle stump. Worst thing was it was no-ball. Anyway, by then that match was in our hands and so it didn't cost us.

In my last match opener from the other team was left handed batsman. My friend told me that he was division one player. This time I bowled an out-swinger to him. But the fun part was I made it to land some where in between off and middle stump. Batsman played perfect defensive shot. But I'm sure he didn't expect it to go through his defense. It was beautiful sight. It was great because it had to go to swing out of it trajectory to beat the bails on the off-stump. It was like going towards leg stump and in the final seconds it changed it's trajectory and narrowly beat his bat and then took of the bails. It was just out right beautiful out-swinger I've ever bowled to left handed batsmen in all my life. I honestly believed it would take out any left handed batsman.

Unfortunately, I couldn't continue bowling as I got severe cramps in my second over. I had to leave the field in severe pain. I knew that my team needed me and so took lot of fluids in that break to go back into the field. I went after the first break and had to wait another 10 overs before I could bowl again because of the bowling after retired rule. In a way it was very good match considering it could have swung either way until the end of the match. Finally, I took my chances to start my second over in the 33rd over and the other team had only one wicket to make winning runs of 20. Their plan was to stay there and runs would come on their own. I guess it was a very good plan considering I was the only bowler left with overs. Which means some other guy had to bowl in tandem with me. It would have made easier for them to get those runs considering we didn't have any other good bowler to bowl with me. All other strike bowlers had already finished their quota. So I took the over in the hope of finishing the game. My captain put faith in me to do the task and I duly did. This time it was a very conventional trick that got me the wicket. I'm sure you must have seen this number of times in your life. Bowler bowls 3 out-swingers, good ones, and then finally put an in-swinger. I executed my plan to a T that it took out the middle stump. Some times you bowl an exceptional out-swingers and still don't get a wicket. Even though I love to get wickets with out-swingers, it's always the other one which gives pumps you up with adrenaline.

It was great when I finished the game like that. I didn't care to jump in the air even though I knew it would bring my cramps back. Luckily, nothing happened like that. I guess drinking all those 2 liters of fluids did the trick. This time I make sure that I take enough fluids it won't happen again. I felt it was a very good performance. Worth of man of the match. I got the ball as a souvenir. The only thing that bothered me was my fitness. It was shocking to understand how low it was. While batting I was out of breath through out my innings. But I guess I was the best runner in my team. I mean fast runner. I believe in taking singles to propel an innings. Plain old theory from God of Cricket, Sachin bayya. Some times I wish I could have those big shoulder muscles to hit big sixes. I hit some good shots using timing. I'm sure they would have been big sixes if I had power in them. I just timed them well to make them fours. Still it was good because all those drives gave me an immense confidence as well as satisfaction.

I know it's a kind of overdoing writing all about it. Believe me you'd also feel just like this when you do something that makes you feel awesome like this. More over like I wrote before nothing is moving forward in my life and I think I need these kind of moments. Hopefully, I can help my team keep winning the matches and this would make me feel great too.

Just yesterday I got a call from one of the companies located in LA and I'm hoping to get this offer. If I could get it, then I could stay here in LA and be with my friends and play cricket. Hopefully it should go just like this. Then definitely, it would be a new beginning. Want to write more, but need to take care of something else. I'll write again very soon. Need to clear my mind off lot of things.

Sreedhar.

PBS Script Generator: Interdependent dropdown/select menus in Javascript

PBS SCRIPT GENERATOR
SH/BASH TCSH/CSH
Begin End Abort

About Me

LA, CA, United States
Here I write about the battles that have been going on in my mind. It's pretty much a scribble.

Sreedhar Manchu

Sreedhar Manchu
Higher Education: Not a simple life anymore