Assignment c programing (2)
Assignment c programing (2)
ASSIGNMENT – 2
Output:
2. WAP to find simple interest and compound interest.
Take the amount, rate and time from the user.
Ans:
#include <stdio.h>
#include <math.h> //Math.h used for pow function
int main()
{
float simple, compound, pa, rate, time, amt; //Using float for decimal places
printf("Enter Amount: ");
scanf("%f", &pa);
printf("Enter Time: ");
scanf("%f", &time);
printf("Enter Rate: ");
scanf("%f", &rate);
simple = (pa * rate * time) / 100; //Calculation of simple interest
amt = pa * pow((1 + rate / 100), time); //Calculation of total amount
compound = amt - pa; //Calculation of compound interest
printf("Simple Interest: %.2f\n", simple); //%.2f for not more than 2 decimal places
printf("Compound Interest: %.2f\n", compound); //%.2f for not more than 2 decimal
places
return 0;
}
Output:
Using Switch Function:
#include <stdio.h>
int main()
int swi;
float simple, compound, pa, rate, time, amt; // Using float for decimal places
scanf("%d", &swi);
switch (swi)
case 1:
scanf("%f", &pa);
scanf("%f", &time);
scanf("%f", &rate);
printf("Simple Interest: %.2f\n", simple); //%.2f for not more than 2 decimal places
break;
case 2:
scanf("%f", &pa);
scanf("%f", &time);
scanf("%f", &rate);
printf("Compound Interest: %.2f\n", compound); //%.2f for not more than 2 decimal places
break;
default:
break;
}
return 0;
Output:
Output:
Using Switch Function:
#include <stdio.h>
int main()
{
int swi;
float temperature, fahrenheit, kelvin;
printf("Celsius To Fahrenheit/Kelvin Converter\n");
printf("1. Fahrenheit\n");
printf("2. Kelvin\n");
scanf("%d", &swi);
switch (swi)
{
case 1:
printf("Enter Temperature(In Celsius): ");
scanf("%f", &temperature);
fahrenheit = (temperature * 9 / 5) + 32; // Conversion to fahrenheit
printf("Fahrenheit: %.2f\n", fahrenheit); //%.2f for not more than 2 decimal places
break;
case 2:
printf("Enter Temperature(In Celsius): ");
scanf("%f", &temperature);
kelvin = temperature + 273.15; // Conversion to kelvin
printf("Kelvin: %.2f", kelvin); //%.2f for not more than 2 decimal places
break;
default:
printf("Select a Valid Option");
break;
}
return 0;
}
Output:
Output:
6. WAP to find the sum of n natural numbers and their
average.
Ans:
#include <stdio.h>
int main()
{
int i, sum = 0;
float average, n;
printf("Enter N Number: ");
scanf("%f", &n);
for (i = 0; i <= n; i++)
{
sum = sum + i;
}
printf("Sum of N Natural Numbers: %d\n", sum); //Total sum of the numbers
printf("Average of N Natural Numbers: %.2f\n", average = sum / n); //Average of those
numbers
return 0;
}
Output:
7. WAP to check whether a year is a loop year or not using
Conditional Operator and If Else Condition.
Ans:
#include <stdio.h>
int main()
{
int year;
printf("Enter Year: ");
scanf("%d", &year);
if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) //Formula for checking if a year
is leap or not
{
printf("%d is a Leap Year.\n", year);
}
else
{
printf("%d is not a Leap Year.\n", year);
}
return 0;
}
Output:
Using Conditional Operator:
#include <stdio.h>
int main()
{
int year;
printf("Enter Year: ");
scanf("%d", &year);
(year % 4 == 0 && year % 100 != 0 || year % 400 == 0) ? printf("%d is a Leap Year", year)
: printf("%d is a not Leap Year", year); // Formula for checking if a year is leap or not
return 0;
}
Output:
8. WAP to print the month using switch statement by
entering the first character of the month.
Ans:
#include <stdio.h>
int main()
{
char month;
printf("First Chracter Of The Month: ");
scanf("%s", &month);
switch (month)
{
case 'j':
case 'J':
printf("January or June or July\n");
break;
case 'f':
case 'F':
printf("February\n");
break;
case 'm':
case 'M':
printf("March or May\n");
break;
case 'a':
case 'A':
printf("April or August\n");
break;
case 's':
case 'S':
printf("September\n");
break;
case 'o':
case 'O':
printf("October\n");
break;
case 'n':
case 'N':
printf("November\n");
break;
case 'd':
case 'D':
printf("December\n");
break;
default:
printf("Enter a Valid Character.");
break;
}
return 0;
}
Output:
9. WAP to display square and cube up to an integer with
header file math.h and without math.h .
Ans:
#include <stdio.h>
#include <math.h>
int main()
{
int i, n;
printf("Enter A Number: ");
scanf("%d", &n);
for (i = 1; i <= n; i++)
{
printf("Square of %d: %d\t Cube of %d: %d\n", i, (int)pow(i, 2), i, (int)pow(i, 3));
//Calculation of sqaure and cube
}
return 0;
}
Output:
Without Using Math.h:
#include <stdio.h>
int main()
{
int i, n;
printf("Enter A Number: ");
scanf("%d", &n);
for (i = 1; i <= n; i++)
{
printf("Square of %d: %d\t Cube of %d: %d\n", i, i * i, i, i * i * i); // Calculation of sqaure
and cube
}
return 0;
}
Output:
10. WAP to draw some patterns.
Ans:
#include <stdio.h>
int main()
{
int i, j;
for (i = 1; i <= 5; i++) //Loop for printing rows
{
for (j = 1; j <= i; j++) //Loop for printing columns
{
printf("*");
}
printf("\n");
}
return 0;
}
Output:
2nd Pattern:
#include <stdio.h>
int main()
{
int i, j;
for (i = 1; i <= 5; i++) //Loop for printing rows
{
for (j = 1; j <= i; j++) //Loop for printing columns
{
printf("%d",i);
}
printf("\n");
}
return 0;
}
Output:
3rd Pattern:
#include <stdio.h>
int main()
{
int i, j, n = 1;
for (i = 1; n <= 10; i++) // Loop for printing rows
{
for (j = 1; j <= i; j++) // Loop for printing columns
{
printf("%d ", n++);
}
printf("\n");
}
return 0;
}
Output:
4th Pattern:
#include <stdio.h>
int main()
{
int i, j;
for (i = 5; i >= 1; i--) // Loop for printing rows
{
for (j = 1; j <= i; j++) // Loop for printing columns
{
printf("*");
}
printf("\n");
}
return 0;
}
Output:
5th Pattern:
#include <stdio.h>
int main()
{
int i, j, n;
for (i = 1; i <= 5; i++) // Loop for printing rows
{
for (n = 1; n <= 5 - i; n++) // Loop for printing first loop
{
printf(" ");
}
for (j = 1; j <= 2 * i - 1; j++) // Loop for printing second loop
{
printf("*");
}
printf("\n");
}
return 0;
}
Output: