Sunday, August 20, 2006

Determination of Prime Factors using Functions.

A positive integer is entered through the keyboard.
Write a function to obtain the prime factors of this number.
For example, prime factors of 24 are 2, 2, 2 and 3, whereas prime factors of 35 are 5 and 7.



#include<stdio.h>
main()
{

int number;
int prime(int number);

int primefactor(int number);


printf("Enter the number whose prime factors are to be calculated:");

scanf ("%d", &number);

primefactor(number);

}

//The following function detects a Prime number.

prime(int num)
{
int i, ifprime;


for (i=2; i<=num-1; i++)

{
if (num%i==0)
{

ifprime=0;
}
else
ifprime=1;

}
return (ifprime);
}


//The following function prints the prime factors of a number.

primefactor(int num)
{
int factor,ifprime;
for (factor=2; factor<=num;)

{
prime(factor); //so that the factors are only prime and nothing else.
if (ifprime)

{
if (num%factor==0) //diving by all the prime numbers less than the number itself.
{

printf("%d ", factor);
num=num/factor;

continue;
}
else
{
factor++;//this cannot be made a part of the for loop

}
}
}
return 0;
}





The file can be found at:
Download File

Calculation of A to the power of B using Functions

Write a function power(a,b), to calculate the value of a raised to b.




#include<stdio.h>
main()
{

int power (a,b);
int a, b, result;

printf("Enter the value of a and b:");
scanf ("%d %d", &a, &b);

result=power(a,b);
printf("%d raised to %d is %d", a, b, result);

}

power (int a, int b)
{

int calculation=1, calc;
for (calc=1; calc <=b; calc++)

{
calculation=calculation*a;
continue;
}

return(calculation);
}





The File can be found at:
Download File

Conver the given year to Roman Numerals using Functions.

Write a general-purpose function to convert any given year into its roman equivalent.
The following table shows the roman equivalents of decimal numbers:

Decimal:........Roman
1.....................i
5....................v
10..................x
50..................l
100................c
500...............d
1000.............m

Example:
Roman equivalent of 1988 is mdcccclxxxviii
Roman equivalent of 1525 is mdxxv

This program is a big lengthy owing to the use of Case Statements. This program can also be rewritten using Arrays, which will reduce the length considerably.


#include<stdio.h>
main()
{

int year;
int convert (int year);



{

printf("Note:Enter a four year digit year.\n\n");

printf("Enter the year that you wanna convert to Roman: " );

scanf ("%d", &year);

if (year> 1999)

{
printf("Invalid Year.Please enter again.\n\n");
}
}

convert(year);


}



convert(int year)

{
int i;

printf("\nYear converted to Roman:");


i=(year/1000); //thousands place
if(i==1)

{
printf("m");
}


i=((year/100)%10); //hundreds place

switch (i)
{
case 1:
printf("c");

break;

case 2:
printf("cc");

break;

case 3:
printf("ccc");

break;

case 4:
printf("cd");

break;

case 5:
printf("d");

break;

case 6:
printf("dc");

break;

case 7:
printf("dcc");

break;

case 8:
printf("dccc");

break;

case 9:
printf("dcccc"); //this part you may think is wrong..9 -> cm

break; //but i have taken a hint from the example in the question.

}



i=((year/10)%10); //tens place

switch(i)
{
case 1:
printf("x");

break;

case 2:
printf("xx");

break;

case 3:
printf("xxx");

break;

case 4:
printf("xl");

break;

case 5:
printf("l");

break;

case 6:
printf("lx");

break;

case 7:
printf("lxx");

break;

case 8:
printf("lxxx");

break;

case 9:
printf("lxxxx"); //had it not been for this example, it would have been xc

break;

}



i=year%10; //ones place

switch(i)
{
case 1:
printf("i");

break;

case 2:
printf("ii");

break;

case 3:
printf("iii");

break;

case 4:
printf("iv");

break;

case 5:
printf("v");

break;

case 6:
printf("vi");

break;

case 7:
printf("vii");

break;

case 8:
printf("viii");

break;

case 9:
printf("ix");

break;
}


printf ("\n\n");

return 0;

}







The file can be found at:
Download File

Detection of Leap year using Functions.

Any year is entered through the keyboard.
Write a function to determine whether the year is a leap year or not.



#include<stdio.h>

main()
{

int leap_year(year);
int year, lp;

printf("Enter the year:");
scanf ("%d", &year);

lp=leap_year(year);

if (lp)

{
printf("\nThe entered year is a leap year.");
}
else
{

printf("\nThe entered year is not a leap year.");
}

}


leap_year(int y)

{
int lp;

if (y%4==0)

{
lp=1;
}
else
lp=0;

return(lp);
}









The file can be found at:
Download File

Predict the output of the following Functions.

Predict the output of the following Code Snippets.
This is an exercise based on Functions.
The answers are provided at the bottom of the exercise. Be honest with yourself. :P

//1
main()
{

printf("\nOnly stupids use c?");
display();
}

display()
{
printf("\nFools too use C!");
main();

}





//2
main()
{
printf("\nC to it that C survives.");

main();
}





//3
main()

{
int i=45, c;
c=check(i);

printf("\n%d", c);
}
check (int ch)

{
if (ch>=45)
return (100);

else
return (10*10);
}





//4
main()
{
int i=45, c;

c=check(i*1000);
printf("\n%d", c);

}
check (int ch)
{
if (ch>=40000)

return (ch/10);
else
return (10);

}




//1) infinite loop of both the statements

//2)The message " C to it that C survives" is iterated infinitesimally!

//3) 100

//4) 4500











The file can be downloaded at:
Download File

Calculation of Factorial using Functions.

Write a function to calculate the factorial value of any integer entered through
the keyboard.


#include<stdio.h>
main()
{

int i, f;
int factorial();

printf("Enter the number to evaluate its factorial:");
scanf ("%d", &i);

f=factorial(i);

printf("%d! = %d\n", i, f);

}

factorial(int num)
{
int temp, fact;


for (temp=1,fact=1;temp<=num; temp++)
{

fact=fact*temp;
continue;
}
return (fact);



}






The file can be found at:
Download File

Monday, August 07, 2006

Find the Errors (Switch Case)

These code snippets will help you understand the requirement of debugging a program.
Without a program being debugged, they generate errors. And the heart of debugging lies in the process to find out what causes these errors.

//1.
main()

{
int suite=1;
switch (suite);
{

case 0:
printf("\nClub");
case 1:

printf("\nDiamond");
}
}


//2.



main()
{
int temp;
scanf ("%d", &temp);

switch (temp)
{
case (temp<=20):

printf("\nOooooooohhhhh! Damn Cool!");
case (temp>20 && temp<=30):

printf("\nRain rain here again! ");
case (temp>30 && temp<=40):

printf("\nWish I am on Everest");
default:
printf("\nGood old nagpur weather");

}
}



//3.

main()
{

float a=3.5;
switch (a)
{

case 0.5:
printf("\nThe art of C");
break;

case 1.5:
printf("\nThe spirit of C");
break;

case 2.5:
printf("\nSee through C");
break;

case 3.5:
printf("Simply C");
}
}




//1)the error lies in the semicolon after the switch statement.


//2)This program cannot execute cuz a condition cannot be specified within a case

//statement.

//3)floating point numbers are not allowed in case statements.



The File can be downloaded at:
Download File

Menu driven program - calculates factorial, detects a prime number, even/odd

Write a menu driven program which has the following options:
1. Factorial of a Number
2. Prime or Not
3. Odd or Even
4. Exit

Make use of a switch Statement.


Outline of the program:

//a menu driven program
main()
{
int choice;
while (1)
{
printf("\n1. Factorial");
printf("\n2. Prime");
printf("\n3. Odd/ Even");
printf("\n4. Exit");
printf("\n Your Choice? : ");
scanf ("%d", &choice);

switch (choice)
{
case 1:
//logic for factorial of a number
break;
case 2:
//logic for deciding a prime number
case 3:
//logic for odd/even
break;
case 4:
exit();
}
}
}


NOTES:
1) The statement while(1) puts the entire logic in an infinite loop. This is necessary
since the menu must keep reappearing on the screen once an item is selected and an
appropriate action is taken.
2) exit() is a standard library function. When it gets executed the program execution is
immediately terminated. Note that a break would not have worked here because it would have
taken control only outside the switch and not outside the while.

#include<stdio.h>


main()

{
int choice, factorial, temp, number, i;
//The while(1) is needed so that the program runs in an infinite loop!

while (1)
{
printf("\n1. Factorial");

printf("\n2. Prime");
printf("\n3. Odd/ Even");
printf("\n4. Exit");

printf("\n\nYour Choice? : ");
scanf ("%d", &choice);

switch (choice)
{
case 1:

//The following snippet calculates the factorial of a number.
printf("\nEnter the Number:");

scanf ("%d", &number);
for (temp=1,factorial=1; temp<=number;temp++)

{
factorial=factorial*temp;
continue;
}

printf("Factorial=%d\n\n",factorial);
break;

case 2:

//The following snippet detects a prime number.
printf("Enter the Number:");

scanf ("%d", &number);

if (number==1)

{
printf("The Number 1 is a Unique Number.\n\n ");
}

for (i=2; i<=number-1; i++)

{
if (number%i==0)
{

printf("Number is not prime\n\n");
break;
}


if (number==i || number%i !=0)

{
printf("Number is Prime\n\n");
break;
}


}

break; //break out of this case.



case 3:
//The following snippet detects Even/Odd Numbers.
printf("Enter the Number:");

scanf ("%d", &number);
if (number%2==0)

{
printf("The number is Even\n\n");
}
else

{
printf("The number is Odd\n\n");
}
break;


case 4:
//The Following command comes out of the program.
exit();


default:
printf("You have entered the wrong choice. Please try again. \n");
break;
}

}
}


The file can be found at:
Download File

A Program with and without the go-to statement.

The goto statement makes a C Programmer's life only worse. Because of goto, it becomes hard to debug programs and find out and correct errors. With goto, the sequence of program execution changes abruptly, and so it also becomes very difficult to understand the programs.

Of course, we can do without goto. But the only thing is that we need to combine the other instructions. One such example is provided here. The first program makes use of the goto statement, which appears to be fascinating.
But then, as said earlier, the effect of goto can be realized by using other statements as well. That is being demonstrated in the next program.

Program with goto:

#include<stdio.h>
main()
{

int i, j, k;
for (i=1; i<=3; i++)

{
for (j=1; j<=3; j++)

{
for (k=1; k<=3; k++)

{
if (i==3&& j==3&&k==3)

goto out;
else
printf("%d %d %d \n", i, j, k);

}
}
}

out:
printf("Out of the loop at last!");

}



Program without goto:


#include<stdio.h>
main()
{

int i, j, k;
for (i=1; i<=3; i++)

{
for (j=1; j<=3; j++)

{
for (k=1; k<=3; k++)

{
if (i==3&& j==3&&k==3)

continue;
else
printf("%d %d %d \n", i, j, k);

}
continue;
}
continue;
}

printf("Out of the loop at last!");

}





The File can be downloaded at:
Download File (with goto)

Download File (without goto)

Predict the output (switch case problems)

Following are the problems, whose outputs you should be able to guess without running these code snippets on the computer. Good knowledge of "switch and case" statements is necessary for solving these.

These problems help you know where you stand...

//1

main()

{
char suite=3;
switch (suite)

{
case 1:
printf("\nDiamond");

case 2:
printf("\nSpade");
default:

printf("\nHeart");
}
printf("\nI thought one wears a suite.");

}


//2

main()
{
int k, j=2;

switch(k=j+1)
{
case 0:

printf("\nTailor");
case 1:
printf("\nTutor");

case 2:
printf("\nTramp");
default:

printf("Pure simple Egghead!");

}
}



//3

main()
{
int i=0;
switch (i)

{
case 0:
printf("\nTemple is a non-issue");

case 1:
printf("\nAandhi is never stable");
case 2:

printf("\nMandal will ruin India");
case 3:
printf("\nWe want better politicians");

}
}


//4

main()
{

char ch='a';
switch (ch)
{

case 'a':
case 'b':
printf("\nYou entered b");

case 'A':
printf("\na as in ashar");
}
}



main()
{
char i='1';

switch (i)
{
case 0:
printf("\nFeeding Fish");

case 1:
printf("\nWeeding Grass");
case 2:
printf("\nmending roof");

default:
printf("\nJust to survive");
}
}


Look down for answers.....

















//ANSWERS:

//1. Heart

// I thought one wears a suite.


//2. Pure Simple Egghead!




//3. Temple is a non-issue
//Aandhi is never stable

//Mandal will ruin India
//We want better politicians



//4. You entered b.
// a as in ashar.


//5. Just to survive.



The File can be downloaded at:
Download File