Friday, July 28, 2006

Write a program to print all prime numbers from 1 to 300.
(Hint: Use nested loops, break and continue statements.)

Improvements:
The work done by the computer is considerably used..if we chuck out all the even numbers..cuz even numbers cannot be prime numbers (except the number 2)

Also, 1 is not a prime number. Instead..it is a Unique Number.



#include<stdio.h>

main()
{

int number, div, ifprime;



for (number=2;number<=300;number++)

{

for (div=2; div<number; div++)

{
if (number%div==0)
{

ifprime=0;
break;
}

ifprime=1;

}

if (ifprime)
{
printf("\n%d", number);

}
}
}




The File can be found at:
Download File
Write a program to add first seven terms of the following series using a for loop:

(1/1!) + (2/2!) + (3/3!) + .....


#include<stdio.h>
main()
{
float result, temp_calc, num, temp, factorial;

result=0;

for (num=1; num<=7; num++)

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

{

factorial = factorial*temp;

temp_calc = (num/factorial);



}

result=result+temp_calc;


}



printf("(1/1!) + (2/2!) + (3/3!) + (4/4!) + (5/5!) + (6/6!) + (7/7!) = %f", result);

}



The file can be found at:
Download File

Fill the screen with a smiling face

Write a program to fill the entire screen with a smiling face.
The smiling face has an ASCII value 1.

Other ideas..
An infinite loop like the following can also do the job.
for (i=1;i<=1000;)

#include<stdio.h>

main()
{
int i, j;

for (i=1, j=1; j<=5000; j++)

{
printf("%c", i);
}
}


Sunday, July 23, 2006

Printing ASCII Table with Numbers and corresponding characters.

The following program prints the ASCII Table with the numbers from 0 to 255 and their corresponding equivalent characters in ASCII.

Write a program to print all the ASCII values and their equivalent
characters using a while loop. The ASCII values vary from 0 to 255.

#include<stdio.h>
main()
{
int num;

printf("Printing ASCII values Table...\n\n");

num = 1;

while(num<=255)

{

printf("\nValue:%d = ASCII Character:%c", num, num); /*This change has been made as per the comment. Thank you anonymous Blog Viewer ... */

num++;
}

printf("\n\nEND\n");

}


Mistake made in this program was two variables one with int and one with char type were taken...the other being redundant.
The change has been made here on the blog, but it still remains to be changed in the file to be downloaded. Will do that soon.


The file can be downloaded at:
Download File

Calculation of One Number Raised to Another.

Two numbers are entered through the keyboard. Write a program to find the value of one number raised to the power of another.




#include<stdio.h>
main()
{
int num1, index, result, temp; /* num2=index*/

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

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

result=1;
temp=1;

while(temp<=index)

{
result = result*num1;

temp++;

}

printf("%d raised to %d is: %d ", num1, index, result);

}



The file can be found at:
Download File.

Detection of a Prime Number

Write a program to determine whether a number is prime or not.
A prime number is one, which is divisible only by 1 or itself.



#include<stdio.h>
main()
{
int num, i;

printf ("Enter a number:");
scanf ("%d", &num);

i=2;
while (i<=num - 1)

{
if (num % i ==0)

{
printf ("Not a prime number");
break;
}

i++;
}

if (i == num)

printf ("Prime Number");
}



Printing Armstrong numbers from 1 to 500.

Write a program to print out all Armstrong numbers between 1 and 500.
If sum of cubes of each digit of the number is equal to the number itself,
then the number is called an Armstrong number.
For example,
153 = (1*1*1) + (5*5*5) + (3*3*3)




#include<stdio.h>
main()
{
int number, temp, digit1, digit2, digit3;

printf("Printing all Armstrong numbers between 1 and 500:\n\n");

number = 001;

while (number <= 500)
{
digit1 = number - ((number / 10) * 10);

digit2 = (number / 10) - ((number / 100) * 10);

digit3 = (number / 100) - ((number / 1000) * 10);

temp = (digit1*digit1*digit1) + (digit2*digit2*digit2) + (digit3*digit3*digit3);

if (temp == number)
{
printf("\nAmstrong Number:%d", temp);

}

number++;
}
}



The file can be found at:
Download File.

Calculation of Overtime pay (using while loop)


Write a program to calculate overtime pay of 10 employees. Overtime is paid at

the rate of Rs. 12.00 per hour for every hour worked above 40 hours. Assume that
employees do not work for fractional part of an hour.


#include<stdio.h>
main()
{
int hours, employees;

float overtime_rate, overtime_pay, overtime, break_time;


employees=1;

while (employees<=10)
{

printf ("\nEnter the number of Hours you have worked: " );

scanf ("%d", &hours);

while (hours>40)

{

printf ("\nEnter the fractional part of overtime you haven't worked (in hours):");
/*This will take care of the break. The fractional part for which the employee
did not work!!! */

scanf ("%f", &break_time);

overtime = hours - 40 - break_time;

overtime_rate = 12.00;

overtime_pay = overtime_rate * overtime;

printf ("Your overtime pay is:%f", overtime_pay);

break;


}

employees++;

if (hours<40)
{
printf("You have not worked hard enough. \nYou won't get any overtime! ");

}

}

}




The file can be found at:
Download File


Friday, July 21, 2006

Calculation of Factorial of a Number

This program calculates the Factorial of a number.

Factorial is represented by the ! sign..

For E.g. 5! = 5*4*3


Write a program to find the factorial value of any number entered
through the keyboard.


#include<stdio.h>
main()
{
int number, factorial, temp;

printf("Enter the number whose factorial is to be calculated:");
scanf ("%d", &number);

temp=1;

factorial=1;

while (temp <= number)

{

factorial=factorial*temp;

temp++;


}

printf("The factorial of the entered number is:%d", factorial);
}



The file can be found at:
Download File.

[2/2] Calculation of Simple Interest for 3 sets of P, N, R (using For loop)

orkut.com
This program calculates the Simple Interest for 3 different values of P, N, R.
This program makes use of the For Loop, which is by far the most efficient loop used in C Programming.


#include<stdio.h>
main()
{

int p, n, count;
float r, si;

for (count=1;count<=3;count++)

{
printf ("Enter the values of p, n and r:");
scanf ("%d %d %f", &p, &n, &r);

si = p*n*r / 100 ;

printf ("Simple Interest = Rs. %f\n", si);
}

}



The file can be downloaded at:
Download File

[1/2] Calculation of Simple for 3 sets of PNR (using While loop)

The following program is used to calculate 3 Sets of values of P, N, R.
This is made possible using the While Loop.



#include<stdio.h>
main()
{
int p, n, count;

float r, si;

count = 1;

while (count <= 3)
{
printf ("\nEnter values of p, n and r");

scanf ("%d %d %f", &p, &n, &r);
si = p*n*r / 100;

printf ("Simple Interest = Rs. %f", si);

count = count + 1;

}
}


You can download the File at:
Download File.

Friday, July 14, 2006

Calendar Program - Detecting the day on 1st Jan between 1900-19xx using if-else

This was the most tricky problem, and took me a lotta days to figure out what the solution would be like...

According to the gregorian calendar, it was Monday on the date 01/01/1900.
If any year is input through the keyboard write a program to find out what
is the day on 1st January of this year.



The following is an idea that was proposed for the solution:

Assuming that you are not expected to come up with any date earlier than the one given.
Your calculations can be fairly simple.

* Calculate the number of days between your start date and the requested date
* Find the remainder when you divide by seven
* That will give you the day of the week, where monday == 0, tuesday == 1,..., Sunday == 6.

So the crux of the problem is in step 1
Break this down into three parts

* How many whole years between the start and and end dates
* How many whole months between the start and end months
* How many day between the start and end days

Again the only tricky part is in the first step.
Each year has 365 days so multiple the number by 365. Then think about the number
of leap years, a leap year occurs every four years, so divide the number of years that you have by four.



#include<stdio.h>
main()
{

int yr, lp_yrs, difference, total_days, day_of_week, weekday;

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

difference = (yr%100); /*difference between entered year and reference year*/

lp_yrs = (yr%100) / 4; /*no. of leap years between concerned year and reference year*/

total_days = (difference*365) + lp_yrs ;

day_of_week = total_days % 7;

if (day_of_week == 0)

{
printf("The day on Jan 1st of this year is Monday");
}
else if (day_of_week ==1)

{
printf("The day on Jan 1st of this year is Tuesday");
}
else if (day_of_week == 2)

{
printf("The day on Jan 1st of this year is Wednesday");
}
else if (day_of_week == 3)

{
printf("The day on Jan 1st of this year is Thursday");
}
else if (day_of_week ==4)

{
printf("The day on Jan 1st of this year is Friday");
}
else if (day_of_week ==5)

{
printf("The day on Jan 1st of this year is Saturday");
}
else if (day_of_week ==6)

{
printf("The day on Jan 1st of this year is Sunday");
}

}



The file can be downloaded at:
Download File

Evaluation of Steel Grades

A certain grade of steel is graded according to the following conditions:

1)Hardness must be greater than 50
2)Carbon content must be less than 0.7
3)Tensile strength must be greater than 5600

The grades are as follows:
Grade is 10 if all three conditions are met
Grade is 9 if (1) and (2) are met
Grade is 8 if (2) and (3) are met
Grade is 7 if (1) and (3) are met
Grade is 6 if only one condition is met
Grade is 5 if none of the conditions is met

Write a program which will require the user to give the values of hardness,
carbon content and tensile strength of the steel under consideration and output
the grade of the steel.


#include<stdio.h>
main()
{
int hardness, ts, grade;

float carbon;

printf("Enter the values of hardness, tensile strength and carbon content in the steel:");
scanf("%d %d %f", &hardness, &ts, &carbon);

if ((hardness>50) && (carbon<0.7) && (ts>5600))

printf("Grade 10");
else if ((hardness>50) && (carbon<0.7))

printf("Grade 9");
else if ((carbon<0.7) && (ts>5600))

printf("Grade 8");
else if ((hardness>50) && (ts>5600))

printf("Grade 7");
else if ((hardness>50) || (carbon<0.7) || (ts>5600))

printf("Grade 6");
else
printf("Grade 5");

}



The file can be downloaded at:
Download File

Calculation of Insurance using Logical Operators.



An Insurance company follows following rules to calculate premium.

(1) If a person's health is excellent and the person is between 25 and 35 years of age and lives in a city and is a male then the premium is Rs.4 per thousand and his policyamount cannot exceed Rs.2 Lakhs.

(2) If a person satisfies all the above conditions except that the sex is female then the premium is Rs.3 per thousand and her policy amount cannot exceed Rs. 1 lakh.

(3) If a person's health is poor and the person is between 25 and 35 years of age and lives in a village and is a male then the premium is Rs. 6 per thousand and his policy cannot exceed Rs. 10,000.

(4) In all the other cases the person is not insured.



#include<stdio.h>
main()
{
int age,premium, max_amount;

char health, location, sex;


printf("Enter Health - g for good / b for bad\nEnter Location - c for city / v for village");

printf("\nEnter sex - m for male / f for female");

printf("\nEnter the health, age, location and sex of the person:");

scanf ("%c %d %c %c", &health, &age, &location, &sex);



if ((health=='g') && ((age>=25)&&(age<=35)) && (location=='c') && (sex=='m'))

{
premium=4;
max_amount=2;
printf("This person is insured.\nThe payable premium is Rs. %d per thousand\n and the max policy amount is Rs. %d Lakhs", premium, max_amount);

}

else if ((health=='g') && ((age>=25)&&(age<=35)) && (location=='c') && (sex=='f'))

{
premium=3;
max_amount=1;
printf("This person is insured.\nThe payable premium is Rs. %d per thousand\nand the max policy amount is Rs. %d Lakhs", premium, max_amount);

}

else if ((health=='b') && ((age>=25)&&(age<=35)) && (location=='v') && (sex=='m'))

{
premium=6;
max_amount=10000;
printf("This person is insured.\nThe payable premium is Rs. %d per thousand \nand the max policy amount is Rs. %d ", premium, max_amount);

}

else
{
printf("This person is not insured.");
}

}









The file can be found at:
Download File

[2/2] Detection of Character using ASCII (Conditional Operator)

The only difference between the previous program and this one is the use of Conditional Operator (:) instead of the Logical Operators.


Using conditional operators determine:

1) Whether the character entered through the keyboard is a lower case alphabet or not.
2) Whether a character entered through the keyboard is a special symbol or not.




#include<stdio.h>
main()
{
char charac;


printf("Enter the character to be detected:");
scanf("%c", &charac);

((charac>=97) && (charac<=122))?(printf("\nLower Case")):(printf("\nNot a Lower Case character"));

/*For detection of Special Symbols*/

(((charac>=0) && (charac<=47)) || ((charac>=58) && (charac<=64))|| ((charac>=91) && (charac<=96))|| ((charac>=123) && (charac<=127)))? (printf("\nSpecial Symbol")):(printf("\nNot a Special Symbol"));

}






The file can be downloaded at:
Download File

Monday, July 10, 2006

[1/2] Detection of Character using ASCII (Logical Operators)

This program detects whether the character entered is a Small letter, capital letter, a number or a special Symbol. This can be done by comparing each of the character with its corresponding ASCII Value.

Any character is entered through the keyboard, write a program to determine whether the
character entered is a capital letter, a small case letter, a digit or a special symbol.



#include<stdio.h>
main()
{
char charac;

printf ("Enter the character to be checked for: ");
scanf("%c", &charac);

if ((charac>=65) && (charac<=95))

{
printf("The entered character is a Capital(Upper Case) Letter");
}
else if ((charac>=97) && (charac<=122))

{
printf("The entered character is a Small(Lower Case) Letter.");
}
else if ((charac>=48) && (charac<=57))

{
printf("The entered character is a Number");
}
else if ((charac>=0) && (charac<=47) ||(charac>=58) && (charac<=64) ||(charac>=91) && (charac<=96) ||(charac>=123) && (charac<=127))

{
printf("The entered character is a special symbol. ");
}
}




The file can be found at:
Download File

[3/3] Checking for Leap Years using Logical Operators

Any year is entered through the keyboard, write a program to
determine whether the year is leap or not.
(Use the logical operators && and ||)


#include<stdio.h>
main()
{


int year;

printf("Enter the year to check for a leap year:");
scanf ("%d", &year);



if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))

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

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

}


The file can be found at:
Download File


[2/3] Checking for Leap Year using Conditional Operators

Write a program using conditional operators to determine whether a year
entered through the keyboard is a leap year or not.



#include<stdio.h>
main()
{

int year;

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

(year%4==0)?(printf("Leap Year")):(printf("Not a Leap Year"));

}




The file can be found at:
Download File

[1/3] Checking for Leap Year using if-else

This program is the first of the three programs that check whether the entered year is a leap year. This program doesn't check whether the number entered is a four digit number.
Just checks whether the number is divisible by four...
Yet another program to demonstrate the use of the Modulus Operator.

Any year is input through the keyboard.
Write a program to determine whether the year is leap year or not.
(Hint: Use the % (modulus) operator



#include<stdio.h>
main()
{

int year;

printf ("Enter the year to be checked for validity as a Leap Year:");
scanf ("%d", &year);

if (year%4==0)
{
printf ("The year is a Leap Year.\n");

}
else
{
printf("The year is not a Leap Year.\n");

}
}




The file can be found at:
Download File