KEMBAR78
Assignment | PDF | Multiplication | Integer (Computer Science)
0% found this document useful (0 votes)
6 views11 pages

Assignment

The document contains a series of programming questions and their corresponding C language code solutions. Topics covered include displaying messages, performing arithmetic operations, using conditional statements, and implementing loops and switch cases. Each question is followed by a complete code snippet that demonstrates the solution.

Uploaded by

Akshay Gupta
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)
6 views11 pages

Assignment

The document contains a series of programming questions and their corresponding C language code solutions. Topics covered include displaying messages, performing arithmetic operations, using conditional statements, and implementing loops and switch cases. Each question is followed by a complete code snippet that demonstrates the solution.

Uploaded by

Akshay Gupta
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/ 11

Q1. Write a program to display a message ?

Ans> #include <stdio.h>

int main() {

printf("Assignment");
return 0;

Q2. Write a program to display message using \n \t ?

Ans> #include <stdio.h>

int main() {

printf("\n Assignment \t Test");


return 0;

Q3. Write a program to add two integer numbers ?

Ans> #include <stdio.h>

int main() {

int number1, number2, sum;

printf("Enter two integers: ");


scanf("%d %d", &number1, &number2);

sum = number1 + number2;

printf("%d + %d = %d", number1, number2, sum);


return 0;

Q4. Write a program for addition, subtraction, multiplication, division of two


numbers ?

Ans> #include <stdio.h>


int main()
{
int first, second, add, subtract, multiply;
float divide;

printf("Enter two integers\n");


scanf("%d%d", &first, &second);

add = first + second;


subtract = first - second;
multiply = first * second;
divide = first / (float)second;

printf("Sum = %d\n",add);
printf("Difference = %d\n",subtract);
printf("Multiplication = %d\n",multiply);
printf("Division = %.2f\n",divide);

return 0;

Q5. Write a program to calculate simple interest ?

Ans> #include<stdio.h>

int main()
{
float Principal_Amount, ROI, Time_Period, S_I;

printf(" Enter the Principal Amount : \n");


scanf("%f", &Principal_Amount);

printf(" Enter Rate Of Interest : \n");


scanf("%f", &ROI);

printf(" Enter the Time Period in Years : \n");


scanf("%f", &Time_Period);

S_I = (Principal_Amount * ROI * Time_Period) / 100;


printf("\nSimple Interest for Principal Amount %f is = %f",
Principal_Amount, S_I);

return 0;

}
Q6. Write a program to calculate area of triangle ?

Ans> #include<stdio.h>

int main()
{
float base,height,area;

printf("enter base of triangle: ");


scanf("%f",&base);

printf("enter height of the triangle: ");


scanf("%f",&height);

area=(base*height)/2;

printf("Area Of Triangle: %f\n",area);

return 0;

Q7. Write a program to calculate area of circle ?

Ans> #include <stdio.h>

int main() {
float radius, area_circle;

printf("Enter the radius of circle : ");


scanf("%f", &radius);

area_circle = 3.14 * radius * radius;


printf("Area of circle : %f", area_circle);

return 0;

Q8. Write a program to implement pre and post decrement unary operator ?

Ans> #include <stdio.h>

int main ()
{
int x, y, a, b;
a = 10;
x = --a;

printf (" Pre Decrement Operator");

printf (" \n The value of x is %d.", x);

printf (" \n The value of a is %d.", a);

b = 10;
y = b--;
printf (" \n\n Post Decrement Operator");
printf (" \n The value of y is %d.", y);

printf (" \n The value of b is %d.", b);

return 0;

Q9. Write a program to implement size of operator ?

Ans> #include <stdio.h>


int main() {
int a = 16;

printf("Size of variable a : %d\n",sizeof(a));


printf("Size of int data type : %d\n",sizeof(int));
printf("Size of char data type : %d\n",sizeof(char));
printf("Size of float data type : %d\n",sizeof(float));

return 0;
}

Q10. Write a program to implement relational binary operator ?

Ans> #include <stdio.h>

int main() {

int a = 21;
int b = 10;

if( a == b ) {
printf(" a is equal to b\n" );
}
else {
printf(" a is not equal to b\n" );
}

if ( a < b ) {
printf(" a is less than b\n" );
}
else {
printf(" a is not less than b\n" );
}

if ( a > b ) {
printf(" a is greater than b\n" );
}
else {
printf(" a is not greater than b\n" );
}

Q11. Write a program to implement logical operator ?

Ans> #include <stdio.h>

int main()
{
int age;

printf("Enter age: ");


scanf("%d", &age);

if(age>=13 && age<=19)


printf("%d is a teenage value\n", age);
else
printf("%d is not a teenage value\n", age);

return 0;
}

Q12. Write a program to find greater no b/w two numbers using if-else ?

Ans> #include <stdio.h>

int main() {
int a, b;
printf("Please Enter Two different values\n");
scanf("%d %d", &a, &b);
if(a > b)
{
printf("%d is Largest\n", a);
}
else if (b > a)
{
printf("%d is Largest\n", b);
}
else
{
printf("Both are Equal\n");
}

return 0;

Q13. Write a program to whether given no. is even or odd using if-else ?

Ans> #include <stdio.h>

int main()
{
int a;
printf("Enter number: ");
scanf("%d", &a);

if (a % 2 == 0) {
printf("The given number is EVEN");
}
else {
printf("The given number is ODD");
}

return 0;

Q14. Write a program to find greatest no. among three numbers if-else-if ?

Ans> #include <stdio.h>

int main()
{
int A, B, C;

printf("Enter the numbers A, B and C: ");


scanf("%d %d %d", &A, &B, &C);

if (A >= B && A >= C)


printf("%d is the largest number.", A);

else if (B >= A && B >= C)


printf("%d is the largest number.", B);

else if (C >= A && C >= B)


printf("%d is the largest number.", C);

return 0;

Q15. Write a program to calculate gross salary using nested if-else ?

Ans> #include <stdio.h>

int main()
{
float basic, gross, da, hra;

printf("Enter basic salary of an employee: ");


scanf("%f", &basic);

if(basic <= 10000)


{
da = basic * 0.8;
hra = basic * 0.2;
}
else if(basic <= 20000)
{
da = basic * 0.9;
hra = basic * 0.25;
}
else
{
da = basic * 0.95;
hra = basic * 0.3;
}

gross = basic + hra + da;

printf("GROSS SALARY OF EMPLOYEE = %.2f", gross);


return 0;

Q16. Write a program to generate mark sheet using nested if-else ?

Ans> #include <stdio.h>

int main() {
float subject_1, subject_2,
subject_3, subject_4, subject_5;

float total, average, percentage;


char grade;

printf("Enter the marks of five subjects::\n");


scanf("%f%f%f%f%f", &subject_1, &subject_2, &subject_3,
&subject_4, &subject_5);

total = subject_1 + subject_2 + subject_3 + subject_4 + subject_5;


average = total / 5.0;
percentage = (total / 500.0) * 100;

if (average >= 90)


grade = 'A';
else if (average >= 80 && average < 90)
grade = 'B';
else if (average >= 70 && average < 80)
grade = 'C';
else if (average >= 60 && average < 70)
grade = 'D';
else
grade = 'E';

printf("\nThe Total marks is: %f / 500.00", total);


printf("\nThe Average marks is: %f", average);
printf("\nThe Percentage is: %f", percentage);
printf("\nThe Grade is: '%c'", grade);

return 0;

Q17. Write a program to enter day number and display name of week day using switch
case ?
Ans> #include <stdio.h>

int main()
{
int week;

printf("Enter week number(1-7): ");


scanf("%d", &week);

switch(week)
{
case 1:
printf("Monday");
break;
case 2:
printf("Tuesday");
break;
case 3:
printf("Wednesday");
break;
case 4:
printf("Thursday");
break;
case 5:
printf("Friday");
break;
case 6:
printf("Saturday");
break;
case 7:
printf("Sunday");
break;
default:
printf("Invalid input! Please enter week number between 1-7.");
}

return 0;
}

Q18. Write a program to enter month number and display name of season of month
using switch case ?

Ans> #include <stdio.h>


int main()
{
int month;
printf("Input Month No : ");
scanf("%d",&month);
switch(month)
{
case 1:
printf("January\n");
break;
case 2:
printf("February\n");
break;
case 3:
printf("March\n");
break;
case 4:
printf("April\n");
break;
case 5:
printf("May\n");
break;
case 6:
printf("June\n");
break;
case 7:
printf("July\n");
break;
case 8:
printf("August\n");
break;
case 9:
printf("September\n");
break;
case 10:
printf("October\n");
break;
case 11:
printf("November\n");
break;
case 12:
printf("December\n");
break;
default:
printf("invalid Month number. \nPlease try again ....\n");
break;
}

Q19. Write a program to enter alphabets and display whether it is vowel or


consonant using switch case ?

Ans> #include <stdio.h>


int main() {
char c;
int lowercase_vowel, uppercase_vowel;
printf("Enter an alphabet: ");
scanf("%c", &c);

lowercase_vowel = (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c ==


'u');

uppercase_vowel = (c == 'A' || c == 'E' || c == 'I' || c == 'O' || c ==


'U');

if (lowercase_vowel || uppercase_vowel)
printf("%c is a vowel.", c);
else
printf("%c is a consonant.", c);
return 0;
}

Q20. Write a program to print table of any number using for loop ?

Ans> #include <stdio.h>


int main(){
int i, num;

printf("Enter number to print table: ");


scanf("%d", &num);
for(i=1; i<=10; i++)
{
printf("%d * %d = %d\n", num, i, (num*i));

return 0;

You might also like