0% found this document useful (0 votes)
4 views

Assignment c programing (2)

The document contains a programming assignment with various tasks implemented in C, including generating multiplication tables, calculating simple and compound interest, finding factorials, converting temperatures, and checking leap years. Each task includes code snippets and explanations for user input and output. Additionally, there are examples of using switch statements and loops for pattern generation.

Uploaded by

abhaysingh19034
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Assignment c programing (2)

The document contains a programming assignment with various tasks implemented in C, including generating multiplication tables, calculating simple and compound interest, finding factorials, converting temperatures, and checking leap years. Each task includes code snippets and explanations for user input and output. Additionally, there are examples of using switch statements and loops for pattern generation.

Uploaded by

abhaysingh19034
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

USCS

ASSIGNMENT – 2

Submitted To: Submitted By:


Mrs. Upasana Rana Ankit Singh Ghota
Assistant Professor BCA 1st Semester
USCS Roll No: 1
1. Implement a program for writing a table for any
number.
Ans:
#include <stdio.h>
int main()
{
int i = 1, n;
printf("Multiplication Table For : ");
scanf("%d", &n);
while (i < 11) //Multiplication of Table
{
printf("%d x %d = %d\n", n, i, n * i);
i++;
}
return 0;
}

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>

#include <math.h> //Math.h used for pow function

int main()

int swi;

float simple, compound, pa, rate, time, amt; // Using float for decimal places

printf("Simple and Compound Interest Calculator\n");

printf("1. Simple Interest\n");

printf("2. Compound Interest\n");

scanf("%d", &swi);

switch (swi)

case 1:

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

printf("Simple Interest: %.2f\n", simple); //%.2f for not more than 2 decimal places

break;

case 2:

printf("Enter Amount: ");

scanf("%f", &pa);

printf("Enter Time: ");

scanf("%f", &time);

printf("Enter Rate: ");

scanf("%f", &rate);

amt = pa * pow((1 + rate / 100), time); // Calculation of total amount

compound = amt - pa; // Calculation of compound interest

printf("Compound Interest: %.2f\n", compound); //%.2f for not more than 2 decimal places

break;

default:

printf("Select a Valid Option");

break;

}
return 0;

Output:

3. WAP to find the factorial of any number.


Ans:
#include<stdio.h>
int main()
{
int n,i,f=1;
printf("Enter A Number: ");
scanf("%d",&n);
for(i=1;i<=n;i++) //Calculation for the factorial of any number
{
f=f*i;
}
printf("Factorial of %d: %d",n,f);
return 0;
}
Output:

4. WAP to convert any Temperature into Fahrenheit and


Kelvin.
Ans:
#include <stdio.h>
int main()
{
float temperature, fahrenheit, kelvin;
printf("Enter Temperature(In Celsius): ");
scanf("%f", &temperature);
fahrenheit = (temperature * 9 / 5) + 32; // Conversion to fahrenheit
kelvin = temperature + 273.15; // Conversion to kelvin
printf("Fahrenheit: %.2f\n", fahrenheit); //%.2f for not more than 2 decimal places
printf("Kelvin: %.2f", kelvin); //%.2f for not more than 2 decimal places
return 0;
}

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:

5. WAP to print a given statement a number of times


which the user defines.
Ans:
#include <stdio.h>
int main()
{
int i, n;
char statement[50];
printf("Enter a Statement: ");
scanf("%s", statement);
printf("Enter a Number: ");
scanf("%d", &n);
for (i = 0; i < n; i++)
{
printf("%s\n", statement);
}
return 0;
}
Output:

Already Defined Statement:


#include <stdio.h>
int main()
{
int i, n;
char statement[50] = "Krishnansh";
printf("Enter a Number: ");
scanf("%d", &n);
for (i = 0; i < n; i++)
{
printf("%s\n", statement);
}
return 0;
}

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:

You might also like