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

Code For LAB Exam

This document provides 5 C programs with solutions to common problems: 1. A program to calculate the area of a triangle given the base and height. 2. A program to check if a number is positive, negative, or zero. 3. A program to calculate the factorial of a given number. 4. Programs using if/else statements to find the greatest of three numbers, check if a number is greater than another, and check if a number is even or odd. 5. A program to calculate the sum of natural numbers using a while loop.
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)
23 views12 pages

Code For LAB Exam

This document provides 5 C programs with solutions to common problems: 1. A program to calculate the area of a triangle given the base and height. 2. A program to check if a number is positive, negative, or zero. 3. A program to calculate the factorial of a given number. 4. Programs using if/else statements to find the greatest of three numbers, check if a number is greater than another, and check if a number is even or odd. 5. A program to calculate the sum of natural numbers using a while loop.
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

Part 1: .

Basic Simple C Programs for Practice


 Que-3:C Program to Find Area of Triangle Given Base And
Height
Solution :-
#include<stdio.h>
int main()
{
/*Program By Ghanendra Yadav
Visit http://www.programmingwithbasics.com/
*/
float b,h,area;

printf("Enter Height and Base Of Triangle : ");


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

area = (h*b)/2;
printf("Area of Triangle is: %f\n",area);
return 0;
}
 Output:

 Que-10: C Program To Check Number Is Positive Or Negative


Solution :-
 #include<stdio.h>
int main()
{
int a;
printf("Enter The Number You Want To Check :\n");
scanf("%d",&a);
if(a<0)
{
printf("Number Is Negative:\n");
}
else if(a>0)
{
printf("Number Is Possitive:\n");
} else {
printf("You Enter Zero :\n");
}
return 0;
}
 Output:
 Que-12:Write A C Program To Calculate Factorial Of A Given
Number .
Solution :-

#include<stdio.h>
int main() {
int fact = 1;
intnum, i;
printf("Enter the number to calculate factorial: ");
scanf("%d", &num);
for (i = 1; i <= num; ++i) {
fact *= i;
}
printf("Factorial of %d is %d\n", num, fact);
return 0;
}

 Output:

Part 2: If/Else C Programs to Practice


 Que 2:C Program To Find Greatest Among Three Numbers

Solution :-

#include<stdio.h>
int main() {
int num1, num2, num3;
printf("Enter three numbers separated by spaces: ");
scanf("%d %d %d", &num1, &num2, &num3);
if (num1 > num2 ) {
printf("The greatest number is: %d\n", num1);
} else if (num2 >num3 ) {
printf("The greatest number is: %d\n", num2);
} else {
printf("The greatest number is: %d\n", num3);
}

return 0;
}

Output:
2. C Program to Print Day of Week Name Using
Switch Case.
Solution :
#include <stdio.h>

int main()
{
/*c program to print days of week using switch */
int choice;

printf("Monday Will be First Days and So On\n\n");


printf("Enter Any Number Between (1 to 7):");

scanf("%d", &choice);
printf("\n");

switch (choice)
{
case 1:
printf("Today is Monday");
break;
case 2:
printf("Today is Tuesday");
break;
case 3:
printf("Today is Wednesday");
break;
case 4:
printf("Today is Thursday");
break;
case 5:
printf("Today is Friday");
break;
case 6:
printf("Today is Saturday");
break;
case 7:
printf("Today is Sunday");
break;
default:
printf("Don't Be Smart....Wrong Choice Try Again!!!");
}

getch();
}

Output:

3.C Program to Find Sum of Natural Numbers


Using While Loop
Solution :
#include<stdio.h>
int main()
{
//Ghanendra Yadav
int n,sum=0;
while(1)
{
printf("\nEnter The Natural Number :\n");
scanf("%d",&n);
sum=(n*(n+1))/2;
printf("\nSum Of Natural Number Is = %d\n",sum);
}
return 0;
}

Output:
4.C Program For Check Greater Among Two Number

#include<stdio.h>
int main()
{
float val1, val2;
printf("Enter The First Number :\n");
scanf("%f", &val1);

printf("Enter The Second Number :\n");


scanf("%f",&val2);

if(val1 >val2)
{
printf("First Number is Greater Than Second\n");
}
else if(val2 > val1)
{
printf("Second Number is Greater Than First\n");
}
else
{
printf("Both Number are Equal");
}
return 0;
}
Output:
5.C Program For Check Number Is Even Or Odd

#include<stdio.h>

int main()
{
int num;

printf("\nEnter Any Number To Check Even Or Odd :\n");


scanf("%d", &num);

if (num%2 == 0)
{
printf("%d is EVEN\n", num);
}
else
{
printf("%d is ODD\n", num);
}
return 0;
}

Output

You might also like