0% found this document useful (0 votes)
38 views26 pages

Lab Activity3 Vinay Kapoor

Uploaded by

vinaykapoorin
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)
38 views26 pages

Lab Activity3 Vinay Kapoor

Uploaded by

vinaykapoorin
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/ 26

Questions for Lab 3

1. Write a C program to declare variables of different data types (int, float, char) and print their
sizes using the sizeof() operator.

Solution:

#include<stdio.h>

int main(){

int num=3;

float fnum=89.7;

char chr='A';

double dnum=56789.90;

printf("\nsize of integer value: %d",sizeof(num));

printf("\nsize of float value is: %d",sizeof(fnum));

printf("\nsize of character is: %d",sizeof(chr));

printf("\nsize of double value is: %d\n",sizeof(dnum));

return 0;

Output:
2. Create a program that accepts an integer input and prints its binary, octal, and hexadecimal
equivalents.

Solution:

#include<stdio.h>

int main(){

int a,num,rem,bin=0,pow=1;

printf("enter a number:");

scanf("%d",&num);

a=num;

while(num!=0){

rem=num%2;

bin+=(rem*pow);

num=num/2;

pow=pow*10;

printf("binary equivalent of %d is %d",a,bin);

return 0;

Output:
3. Write a program that declares two integer variables and swaps their values without using a third
variable.

Solution:

#include<stdio.h>

int main()

int a,b,c;

printf("enter two numbers:");

scanf("%d%d",&a,&b);

printf("\n\nbefore swapping:\n");

printf("a=%d\nb=%d\n",a,b);

c=a;

a=b;

b=c;

printf("\n\nafter swapping:\n");

printf("a=%d\nb=%d\n",a,b);

return 0;

Output:
4. Design a program that accepts user input for two float numbers and outputs their sum,
difference, product, and quotient.

Solution:

#include<stdio.h>

int main()

float a,b;

printf("enter two numbers:");

scanf("%f%f",&a,&b);

printf("\n a=%f \nb=%f",a,b);

printf("\nsum of a and b is:%f",a+b);

printf("\ndiff of a and b is:%f",a-b);

printf("\nproduct of a and b is:%f",a*b);

printf("\nquotioent is",a/b);

return 0;

Output:
5. Write a C program to calculate the area of a circle using float data type and print the result up to
two decimal places.

Solution:

#include<stdio.h>

int main()

float r,area;

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

scanf("%f",&r);

area=3.14*r*r;

printf("\nArea of circle with radius %.2f is : %.2f\n",r,area);

return 0;

Output:
6. Write a program to convert temperature from Fahrenheit to Celsius and vice versa, using float
variables.

Solution:

#include<stdio.h>

int main()

float tempc,tempf;

int c;

printf("\ncase1:celsius to fehrenhite");

printf("\ncase2:fehrenhite to celsius");

printf("\n enter case:");

scanf("%d",&c);

switch(c){

case 1:

printf("enter temp in celsius:");

scanf("%f",&tempc);

tempf=(tempc*9/5)+32;

printf("temprature in fahrenite is: %f",tempf);

break;

case 2:

printf("enter temp in fahrenite:");

scanf("%f",&tempf);

tempc=(tempf-32)*5/9;

printf("temprature in celsius is: %f",tempc);

break;

return 0;

Output:
7. Create a program that declares a char variable and determines whether the character entered is a
vowel or consonant.

Solution:

#include<stdio.h>

int main()

char c;

printf("\n enter a character:");

scanf("%c",&c);

if(c=='A'||c=='E'||c=='I'||c=='O'||c=='U'||c=='a'||c=='e'||c=='i'||c=='o'||c=='u'){

printf("\nentered char is a vovel");

else{

printf("\n entered char is a consonant");

return 0;

Output:
8. Write a C program that inputs a char and checks if it's a digit, uppercase letter, or lowercase letter
using if-else statements.

Solution:

#include<stdio.h>

int main()

char c;

printf("\n enter a character:");

scanf("%c",&c);

if(c=='A'||c=='B'||c=='C'||c=='D'||c=='E'||c=='F'||c=='G'||c=='H'||c=='I'||c=='J'||c=='K'||
c=='L'||c=='M'||c=='N'||c=='O'||c=='P'||c=='Q'||c=='R'||c=='S'||c=='T'||c=='U'||c=='V'||c=='W'||
c=='X'||c=='Y'||c=='Z'){

printf("\n entered char is a Uppercase letter");

else if(c=='a'||c=='b'||c=='c'||c=='d'||c=='e'||c=='f'||c=='g'||c=='h'||c=='i'||c=='j'||
c=='k'||c=='l'||c=='m'||c=='n'||c=='o'||c=='p'||c=='q'||c=='r'||c=='s'||c=='t'||c=='u'||c=='v'||
c=='w'||c=='x'||c=='y'||c=='z'){

printf("\n entered char is a Lowercase letter");

else{

printf("\n it's a digit");

return 0;

Output:
9. Develop a program to print the ASCII value of any char inputted by the user.

Solution:

#include<stdio.h>

int main()

char c;

printf("\n enter a character:");

scanf("%c",&c);

printf("ASCII value of %c is %d",c,c);

return 0;

Output:
10.Write a C program to find the sum of digits of an integer using int data type.

Solution:

#include<stdio.h>

int main()

int a,num,rem,sum=0;

printf("\n enter a number:");

scanf("%d",&num);

a=num;

while(num!=0){

rem=num%10;

sum+=rem;

num=num/10;

printf("sum of digits of %d is %d",a,sum);

return 0;

Output:
11. Write a program to perform addition, subtraction, multiplication, division, and modulus
operations on two integers provided by the user.

Solution:

#include<stdio.h>

int main()

int a,b;

printf("enter two numbers:");

scanf("%d%d",&a,&b);

printf("\n a=%d \nb=%d",a,b);

printf("\nsum of a and b is:%d",a+b);

printf("\ndiff of a and b is:%d",a-b);

printf("\nproduct of a and b is:%d",a*b);

printf("\nquotioent is:%d",a/b);

printf("\nremainder is:%d",a%b);

return 0;

Output:
12.Create a C program that accepts two integers and prints the result of their increment (++) and
decrement (--) operations.

Solution:

#include<stdio.h>

int main()

int num1,num2;

printf("enter two numbers:");

scanf("%d%d",&num1,&num2);

printf("\nnum1=%d",num1);

printf("\nnum2=%d",num2);

printf("\nnum1++=%d",++num1);

printf("\nnum1--=%d",----num1);

printf("\nnum2++=%d",++num2);

printf("\nnum2--=%d",----num2);

return 0;

Output:
13.Write a C program to check if two numbers are equal using the == operator.

Solution:

#include<stdio.h>

int main()

int num1,num2;

printf("enter two numbers:");

scanf("%d%d",&num1,&num2);

printf("\nnum1=%d",num1);

printf("\nnum2=%d",num2);

if(num1==num2){

printf("\nnumbers are equal");

else{

printf("\nnumbers are not equal");

return 0;

Output:
14.Design a program that checks if a given number is even or odd using the modulus operator (%).

Solution:

#include<stdio.h>

int main()

int num;

printf("enter a numbers:");

scanf("%d",&num);

if(num%2==0){

printf("\n%d is even",num);

else{

printf("\n%d is odd",num);

return 0;

Output:
15.Write a program to check if a number is divisible by both 3 and 5 using logical operators (&&).

Solution:

#include<stdio.h>

int main()

int num;

printf("enter a numbers:");

scanf("%d",&num);

if((num%3==0)&&(num%5==0)){

printf("\n%d is divisible by both 3 and 5",num);

else if(num%3==0){

printf("\n%d is not divisible by 5 but divisible by 3",num);

else if(num%5==0){

printf("\n%d is not divisible by 3 but divisible by 5",num);

else{

printf("\n%d is neither divisible by 5 nor by 3",num);

return 0;

Output:
16.Create a program that finds the largest of three numbers using conditional operators (?:).

Solution:

#include<stdio.h>

int main()

int num1,num2,num3,big;

printf("enter three numbers:");

scanf("%d%d%d",&num1,&num2,&num3);

printf("\nnum1=%d\nnum2=%d\nnum3=%d",num1,num2,num3);

num1=num1>num2?num1:num2;

big=num1>num3?num1:num3;

printf("\nbiggest number is:%d",big);

return 0;

Output:
17.Write a C program to demonstrate the use of the bitwise AND (&), OR (|), and XOR (^) operators
on two integer inputs.

Solution:

#include<stdio.h>

int main()

int a,b;

printf("enter two numbers:");

scanf("%d%d",&a,&b);

printf("\nbitwse AND:%d",a&b);

printf("\nbitwse OR:%d",a|b);

printf("\nbitwse XOR:%d",a^b);

return 0;

Output:
18.Develop a program that uses the shift operators (<< and >>) to multiply and divide a number by 2,
respectively.

Solution:

#include<stdio.h>

int main()

int num;

printf("enter a numbers:");

scanf("%d",&num);

printf("\n%d/2 is:%d",num,num>>1);//divide using right shift

printf("\n%d*2 is:%d",num,num<<1);//multiply using left shift

return 0;

Output:
19.Create a program that demonstrates the difference between logical AND (&&) and bitwise AND
(&) using two integer inputs.

Solution:

#include <stdio.h>

int main() {

int a, b;

printf("Enter two integers: ");

scanf("%d %d", &a, &b);

if (a && b) {

printf("Logical AND (&&): Both a and b are non-zero.\n");

} else {

printf("Logical AND (&&): At least one of a or b is zero.\n");

int result = a & b;

printf("Bitwise AND (&): %d & %d = %d\n", a, b, result);

return 0;

Output:
20.Write a C program to check if a number is positive, negative, or zero using relational and
conditional operators.

Solution:

#include<stdio.h>

int main()

int num;

printf("enter a number:");

scanf("%d",&num);

if(num>0){

printf("\n %d is positive number",num);

else if(num<0){

printf("\n %d is negative number",num);

else{

printf("\n number is zero");

return 0;

Output:
21.Write a C program that accepts an integer input and checks if it is a leap year or not using if-else
statements.

Solution:

#include<stdio.h>

int main()

int year;

printf("enter the year:");

scanf("%d",&year);

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

printf("\n %d is Leap year",year);

else{

printf("\n %d is not a Leap year",year);

return 0;

Output:
22.Design a program that inputs a student's marks and prints "Pass" or "Fail" based on whether the
marks are greater than or equal to 40.

Solution:

#include<stdio.h>

int main()

int marks;

printf("enter the marks of student:");

scanf("%d",&marks);

if(marks>=40){

printf("\nPass");

else{

printf("Fail");

return 0;

Output:
23.Write a C program that checks whether a number is prime or not using if-else.

Solution:

#include <stdio.h>

int main() {

int num, i, isPrime = 1;

printf("Enter a positive integer: ");

scanf("%d", &num);

if (num <= 1) {

isPrime = 0;

} else {

for (i = 2; i <= num/2; i++) {

if (num % i == 0) {

isPrime = 0;

break;

if (isPrime) {

printf("%d is a prime number.\n", num);

} else {

printf("%d is not a prime number.\n", num);

return 0;

Output:
24.Create a program that accepts three sides of a triangle and checks if it forms a valid triangle using
if-else conditional statements.

Solution:

#include<stdio.h>

int main()

int s1,s2,s3;

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

scanf("%d%d%d",&s1,&s2,&s3);

if((s1+s2)>s3){

printf("\n Triangle is valid");

else if((s2+s3)>s1){

printf("\n Triangle is valid");

else if((s1+s3)>s2){

printf("\n Triangle is valid");

else{

printf("triangle is not valid");

return 0;

Output:
25.Write a program that inputs a year and checks whether it is a leap year using nested if-else.

Solution:

#include <stdio.h>

int main() {

int year;

printf("Enter a year: ");

scanf("%d", &year);

if (year % 4 == 0) {

if (year % 100 == 0) {

if (year % 400 == 0) {

printf("%d is a leap year.\n", year);

} else {

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

} else {

printf("%d is a leap year.\n", year);

} else {

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

return 0;

Output:

You might also like