Friday, August 14, 2015

Write a C program to calculate the sum of all numbers from 0 to 100 that are divisible by 4

#include<stdio.h>
#include<conio.h>
void main()
{
int i, sum = 0 ;
clrscr();
for ( i =0 ; i <= 100 ; i++)
{
if(i%4 == 0)
{
printf("\n %d",i);
sum+=i;
}
}
printf("\n Sum = %d",sum);
getch();
}

Sunday, August 2, 2015

Write a C Program to Accept five numbers

#include<stdio.h>
#include<conio.h>
void main()
{
int a[5],i,j,t;  //Take Array of 5 integers
clrscr();
printf("Enter 5 nos.\n\n");
for (i=0;i<5;i++)     //Loop to fill array with five numbers
scanf("%d",&a[i]);
for (i=0;i<5;i++)
{
for(j=i+1;j<5;j++)  //loop to compare which number is smaller
{
if(a[i]>a[j])
{
t=a[i];           // Swap small to large numbers
a[i]=a[j];
a[j]=t;
}
}
}
printf("Ascending Order is:");    //Print Result
for(j=0;j<5;j++)
printf("\n%d",a[j]);
getch();
}


******Description*******
1. If you wish to process multiple data inputs, use Array (Array is simply a sequence of memory location includes same type of data). Array can be made for int, char, float, etc. But mix form of array is not allowed. Like some array having elements both int and char is not allowed.

2. To put elements in array one should use 'Loop'. And very popular loop is 'FOR' loop. (Syntax of 'FOR' loop is 'for(initialization;condition;increment/decrement) )

3. Iteratively we compare each element with its successor. If we find it small then we will swap them.

4. We will loop this until end of array encounters

5. Now we have result.

6. To print the result we again will need loop.(Remember, to read or write to/from array you will need a loop)

7. And thats it. Post a comment for your queries. 


Tuesday, July 28, 2015

Bags of Marbles Puzzle

You have three bags, each containing two marbles. Bag A contains two white marbles, Bag B contains two black marbles, and Bag C contains one white marble and one black marble.

You pick a random bag and take out one marble. 

It is a white marble.

What is the probability that the remaining marble from the same bag is also white?

Apples and Friends Puzzle

You have a basket containing ten apples. You have ten friends, who each desire an apple. You give each of your friends one apple.

Now all your friends have one apple each, yet there is an apple remaining in the basket.

Five pirates

Puzzle 1:
5 pirates of different ages have a treasure of 100 gold coins. 

On their ship, they decide to split the coins using this scheme: 

The oldest pirate proposes how to share the coins, and ALL pirates (including the oldest) vote for or against it. 

If 50% or more of the pirates vote for it, then the coins will be shared that way. Otherwise, the pirate proposing the scheme will be thrown overboard, and the process is repeated with the pirates that remain.

As pirates tend to be a bloodthirsty bunch, if a pirate would get the same number of coins if he voted for or against a proposal, he will vote against so that the pirate who proposed the plan will be thrown overboard.



Assuming that all 5 pirates are intelligent, rational, greedy, and do not wish to die, (and are rather good at math for pirates) what will happen?
I welcome all the students to my blog. I hope information shared on this blog are useful to you. Please let me know your suggestion by your valuable comments.