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

Assignment C Programming

The document contains 10 C programming questions and answers. Each question asks the student to write a C program to solve a problem such as checking if a license is valid, calculating volume of a frustum, finding the greatest common divisor of two numbers, etc. For each question, the student provides the C code to solve the problem.

Uploaded by

lolpro788
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)
17 views

Assignment C Programming

The document contains 10 C programming questions and answers. Each question asks the student to write a C program to solve a problem such as checking if a license is valid, calculating volume of a frustum, finding the greatest common divisor of two numbers, etc. For each question, the student provides the C code to solve the problem.

Uploaded by

lolpro788
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/ 10

ASSIGNMENT C PROGRAMMING

SET-G
Name- Vivek Kumar
Sec-AC
Roll-69
University roll- 2315002505
Q-1
Ans-
#include <stdio.h>
#include <time.h>
int main() {
time_t expirationDate, currentTime;
char licenseType[20];
printf("Enter expiration date: ");
scanf("%ld", &expirationDate);
printf("Enter license type (Trial(T) or Subscription(S)): ");
scanf("%s", licenseType);
// Get the current time
time(&currentTime);
int isTrial = 0;
int isSubscription = 0;
if (licenseType[0] == 'T' || licenseType[0] == 't') {
isTrial = 1;
} else if (licenseType[0] == 'S' || licenseType[0] == 's') {
isSubscription = 1;
}
if (isTrial) {
// Trial license is valid for 30 days
if (expirationDate - currentTime >= 0 && expirationDate - currentTime <= 30 * 24 * 60 * 60) {
printf("License is valid.\n");
} else {
printf("License is invalid.\n");
}
} else if (isSubscription) {
// Subscription license is valid for 365 days
if (expirationDate - currentTime >= 0 && expirationDate - currentTime <= 365 * 24 * 60 * 60) {
printf("License is valid.\n");
} else {
printf("License is invalid.\n");
}
} else {
printf("Invalid license type.\n");
} return 0; }
Q-2
Ans-

#include <stdio.h>
#include <math.h>

int main() {
double r1, r2, h, volume;
const double pi = 3.14159265359; // Approximate value of pi

// Input the values of r1, r2, and h


printf("Enter the value of r1: ");
scanf("%lf", &r1);

printf("Enter the value of r2: ");


scanf("%lf", &r2);

printf("Enter the height (h): ");


scanf("%lf", &h);

// Calculate the volume of the frustum


volume = (1.0 / 3.0) * pi * (pow(r2, 2) - pow(r1, 2)) * h;

// Output the result


printf("The volume of the frustum is: %.2lf\n", volume);

return 0;
}
Q-3
Ans-

#include <stdio.h>

int main() {
double initialVelocity, acceleration, time, finalVelocity;

// Input initial velocity, acceleration, and time


printf("Enter initial velocity (m/s): ");
scanf("%lf", &initialVelocity);

printf("Enter acceleration (m/s^2): ");


scanf("%lf", &acceleration);

printf("Enter time (s): ");


scanf("%lf", &time);

// Calculate final velocity


finalVelocity = initialVelocity + (acceleration * time);

// Output the final velocity


printf("Final velocity: %.2lf m/s\n", finalVelocity);

return 0;
}
Q-4
Ans-

#include <stdio.h>

int main() {
int num1, num2;

// Input two positive integers


printf("Enter the first positive integer: ");
scanf("%d", &num1);

printf("Enter the second positive integer: ");


scanf("%d", &num2);

// Find the LCM without using functions


int max = (num1 > num2) ? num1 : num2;
int lcm = max;

while (1) {
if (lcm % num1 == 0 && lcm % num2 == 0) {
printf("The least common multiple of %d and %d is %d.\n", num1,
num2, lcm);
break;
}
lcm += max;
}

return 0;
}
Q-5
Ans-

#include <stdio.h>

int main() {
int n, i;
unsigned long long int term1 = 0, term2 = 1, nextTerm;

// Input the number of terms you want in the sequence


printf("Enter the number of terms in the Fibonacci sequence: ");
scanf("%d", &n);

// Display the first two terms


printf("Fibonacci Sequence up to %d terms:\n", n);
if (n >= 1) {
printf("%llu", term1);
}
if (n >= 2) {
printf(", %llu", term2);
}

// Generate and print the remaining terms


for (i = 3; i <= n; i++) {
nextTerm = term1 + term2;
printf(", %llu", nextTerm);
term1 = term2;
term2 = nextTerm;
}

printf("\n");
return 0;
}
Q-6
Ans-

#include <stdio.h>

int main() {
int numStudents;
printf("Enter the number of students: ");
scanf("%d", &numStudents);

for (int i = 1; i <= numStudents; i++) {


int score;
printf("Enter the score for student %d: ", i);
scanf("%d", &score);

char grade;
if (score >= 90 && score <= 100) {
grade = 'A';
} else if (score >= 80) {
grade = 'B';
} else if (score >= 60 && score <=70) {
grade = 'C';
} else if (score >= 50) {
grade = 'D';
} else {
grade = 'F';
}

printf("Student %d - Score: %d, Grade: %c\n", i, score, grade);


}

return 0;
}
Q-7
Ans-

#include <stdio.h>
#include <math.h>

int main() {
int numTerms;
double firstTerm, commonRatio;

// Input the first term and common ratio


printf("Enter the first term: ");
scanf("%lf", &firstTerm);

printf("Enter the common ratio: ");


scanf("%lf", &commonRatio);

// Input the number of terms in the series


printf("Enter the number of terms in the G.P. series: ");
scanf("%d", &numTerms);

double gpSeries[numTerms];
double sum = 0.0;

// Calculate and print the G.P. series and its sum


for (int i = 0; i < numTerms; i++) {
gpSeries[i] = firstTerm * pow(commonRatio, i);
sum += gpSeries[i];
}

printf("G.P. Series: ");


for (int i = 0; i < numTerms; i++) {
printf("%.2lf", gpSeries[i]);
if (i < numTerms - 1) {
printf(", ");
}
}
printf("\n");

printf("Sum of G.P. Series: %.2lf\n", sum);

return 0;
}
Q-8
Ans-

#include <stdio.h>

int main() {
int num1, num2;

// Input two positive integers


printf("Enter the first positive integer: ");
scanf("%d", &num1);

printf("Enter the second positive integer: ");


scanf("%d", &num2);

int gcd;

// Find the GCD using the Euclidean algorithm


while (num2 != 0) {
int temp = num2;
num2 = num1 % num2;
num1 = temp;
}

gcd = num1;

// Display the GCD


printf("The greatest common divisor of %d and %d is %d.\n", num1, num2,
gcd);

return 0;
}
Q-9
Ans-

#include <stdio.h>

int main() {
int num, original, sum, fact, digit;

printf("Enter a number: ");


scanf("%d", &num);

original = num;
sum = 0;

while (num > 0) {


digit = num % 10;
fact = 1;

// Calculate the factorial of the digit


for (int i = 1; i <= digit; i++) {
fact *= i;
}

sum += fact;
num /= 10;
}

if (sum == original) {
printf("%d is a Strong number.\n", original);
} else {
printf("%d is not a Strong number.\n", original);
}

return 0;
}
Q-10
Ans-

#include <stdio.h>

int main() {
int year;
char continueCheck;

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

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


printf("%d is a leap year.\n", year);
} else {
printf("%d is not a leap year.\n", year);
}

printf("Do you want to check another year? (Y/N): ");


scanf(" %c", &continueCheck);

} while (continueCheck == 'Y' || continueCheck == 'y');

return 0;
}

You might also like