0% found this document useful (0 votes)
26 views12 pages

CJ3 1

The document contains 5 programming questions and their solutions in C language. It includes programs to find sum of digits of a 3-digit number, calculate student grade based on average score, identify if a character is vowel or consonant, calculate internet usage charges based on slabs, and convert a number to its roman numeral equivalent.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views12 pages

CJ3 1

The document contains 5 programming questions and their solutions in C language. It includes programs to find sum of digits of a 3-digit number, calculate student grade based on average score, identify if a character is vowel or consonant, calculate internet usage charges based on slabs, and convert a number to its roman numeral equivalent.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

ASSIGNMENT 2

Ansh Shivhare
0801IT221152
AB22G090

QUESTION 1.Write a program using if-else that takes a 3-digit integer as input and find:

Sum of digit of the number.

Sum of 1st and 3rd digit.

Source code

#include <stdio.h>

int main()

int num;

printf("Enter a 3 digit integer: ");

scanf("%d", &num);

if (num >= 100 && num <= 999)

int sum = 0;

int temp = num;

while (temp > 0)

sum += temp % 10;

temp /= 10;

printf("The sum of the digits is %d\n", sum);

}
ASSIGNMENT 2
Ansh Shivhare
0801IT221152
AB22G090

else

printf("Invalid input. Please enter a 3 digit integer.\n");

return 0;

Output

Source code

#include <stdio.h>

int main()

int num;

printf("Enter a 3 digit integer: ");

scanf("%d", &num);

if (num >= 100 && num <= 999)

int sum = 0;

sum += num / 100;

sum += num % 10;


ASSIGNMENT 2
Ansh Shivhare
0801IT221152
AB22G090

printf("The sum of the first and third digit is %d\n", sum);

else

printf("Invalid input. Please enter a 3 digit integer.\n");

return 0;

Output

QUESTION 2 Write a program that determines a student’s grade. The program will read three scores and

determine the grade based on the following rules:

-if the average score =90% =&gt;grade=A

-if the average score &gt;= 70% and grade=B

-if the average score&gt;=50% and grade=C

-if the average scoregrade=F

SOURCE CODE

#include <stdio.h>
ASSIGNMENT 2
Ansh Shivhare
0801IT221152
AB22G090

int main()

double score1, score2, score3;

double average;

char grade;

printf("Enter the first score: ");

scanf("%lf", &score1);

printf("Enter the second score: ");

scanf("%lf", &score2);

printf("Enter the third score: ");

scanf("%lf", &score3);

average = (score1 + score2 + score3) / 3;

if (average >= 90)

grade = 'A';

else if (average >= 80)

grade = 'B';

else if (average >= 70)

grade = 'C';

}
ASSIGNMENT 2
Ansh Shivhare
0801IT221152
AB22G090

else if (average >= 60)

grade = 'D';

else

grade = 'F';

printf("The average score is %.2f and the grade is %c\n", average, grade);

return 0;

Output

QUESTION 3 . Write a C program using if-else and switch to accept a character from the keyboard and
print ‘A Vowel’ if it is a vowel otherwise prints ‘A Consonant’.

Source code
ASSIGNMENT 2
Ansh Shivhare
0801IT221152
AB22G090

#include <stdio.h>

int main()

char ch;

printf("Enter a character: ");

scanf("%c", &ch);

if ((ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z'))

switch (ch)

case 'A':

case 'E':

case 'I':

case 'O':

case 'U':

printf("vowel\n");

break;

case 'a':

case 'e':

case 'i':

case 'o':

case 'u':

printf("vowel\n");
ASSIGNMENT 2
Ansh Shivhare
0801IT221152
AB22G090

break;

default:

printf("consonant\n");

else

printf("Invalid input. Please enter a letter.\n");

return 0;

Output

QUESTION 4 An Internet Service Provider company charges its domestic customers as follows:

Consumption Unit Rate of Charge

0-200 Rs. 1.50 per unit

201 - 400 Rs. 150 plus Rs. 0.15 per unit if exceed 200
ASSIGNMENT 2
Ansh Shivhare
0801IT221152
AB22G090

401 - 600 Rs. 270 plus Rs. 0.60 per unit if exceed 400

and above Rs. 425 plus Rs. 0.95 per unit if exceed 600

The program reads the data usage consumed and prints the amount to be paid by the customer.
Implement using if-else and switch.

Source code

#include <stdio.h>

int main()

int usage;

float charge;

printf("Enter data usage in units: ");

scanf("%d", &usage);

if (usage <= 200)

charge = usage * 1.50;

else if (usage <= 400)

charge = 150 + (usage - 200) * 0.15;

else if (usage <= 600)

{
ASSIGNMENT 2
Ansh Shivhare
0801IT221152
AB22G090

charge = 270 + (usage - 400) * 0.60;

else

charge = 425 + (usage - 600) * 0.95;

printf("The amount to be paid is Rs. %.2f\n", charge);

switch (usage / 200)

case 0:

charge = usage * 1.50;

break;

case 1:

charge = 150 + (usage - 200) * 0.15;

break;

case 2:

charge = 270 + (usage - 400) * 0.60;

break;

default:

charge = 425 + (usage - 600) * 0.95;

break;

printf("The amount to be paid is Rs. %.2f\n", charge);


ASSIGNMENT 2
Ansh Shivhare
0801IT221152
AB22G090

return 0;

Output

QUESTION 5. Write a program to convert number into roman number. Roman letters will be written in
the combinations of seven letters.

I=1, V=5, X=10, L=50, C=100, D=500, M=1000

Source code

#include <stdio.h>

void convertToRoman(int num)

char *symbols[] = {"M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"};

int values[] = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1};

for (int i = 0; i < 13; i++)

{
ASSIGNMENT 2
Ansh Shivhare
0801IT221152
AB22G090

while (num >= values[i])

printf("%s", symbols[i]);

num -= values[i];

printf("\n");

int main()

int num;

printf("Enter a number: ");

scanf("%d", &num);

if (num < 1 || num > 3999)

printf("Invalid number. It must be between 1 and 3999.\n");

return -1;

convertToRoman(num);
ASSIGNMENT 2
Ansh Shivhare
0801IT221152
AB22G090

return 0;

Output

You might also like