0% found this document useful (0 votes)
57 views19 pages

C Prohramming Practical

The document contains 18 questions asking to write C programs to perform various calculations and operations. These include programs to calculate addition, subtraction, multiplication, division and modulus of two numbers; simple interest; area of a circle; determining if a number is positive or negative; even or odd; finding the greatest of three numbers; swapping two numbers; a basic calculator using switch case; finding the sum of natural numbers; generating a table for a given number; calculating factorial of a number; determining if a number is prime; printing the Fibonacci series; printing pyramid patterns; printing the weekday for a given week number; reversing a given digit; checking if a number is Armstrong; and checking if a number is a palindrome.

Uploaded by

manishkharwar.hh
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)
57 views19 pages

C Prohramming Practical

The document contains 18 questions asking to write C programs to perform various calculations and operations. These include programs to calculate addition, subtraction, multiplication, division and modulus of two numbers; simple interest; area of a circle; determining if a number is positive or negative; even or odd; finding the greatest of three numbers; swapping two numbers; a basic calculator using switch case; finding the sum of natural numbers; generating a table for a given number; calculating factorial of a number; determining if a number is prime; printing the Fibonacci series; printing pyramid patterns; printing the weekday for a given week number; reversing a given digit; checking if a number is Armstrong; and checking if a number is a palindrome.

Uploaded by

manishkharwar.hh
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/ 19

Q1.

Write a C program to find addition, subtraction, division,


multiplication and modulus of two numbers.

#include <stdio.h>

int main() {

int num1, num2;


int sum, sub, mult, mod;
float div;

printf("Enter first number: ");


scanf("%d", &num1);

printf("Enter second number: ");


scanf("%d", &num2);

sum = num1 + num2;


sub = num1 - num2;
mult = num1 * num2;
div = (float)num1 / num2;
mod = num1 % num2;

printf("The SUM of the given numbers is %d\n", sum);


printf("The DIFFERENCE of the given numbers is %d\n", sub);
printf("The PRODUCT of the given numbers is %d\n", mult);
printf("The QUOTIENT of the given numbers is %f\n", div);
printf("The MODULUS of given numbers is %d\n", mod);

return 0;
}

Output:

1
Q.2 Write a C program to find simple interest.
#include <stdio.h>

int main()
{
float P, R, T;

printf("Enter the value of Principal amount: ");


scanf("%f", &P);
printf("Enter the value of Interest rate: ");
scanf("%f", &R);
printf("Enter the value of time: ");
scanf("%f", &T);

float SI = (P * T * R) / 100;

printf("Simple Interest = %f\n", SI);

return 0;
}

Output:

2
Q.3 Write a C program to find the area of a circle.

#include <stdio.h>
#include<conio.h>
int main() {
float pie = 3.14;
int radius;

printf("Enter The Radius of Circle:");


scanf("%d",&radius);

float area = (float)(pie* radius * radius);

printf("The area of the given circle is %f", area);


return 0;
}

Output:

3
Q.4 Write a C program to find if a given number is positive or
negative.

#include <stdio.h>

int main()
{
int num;

printf("Input a number : ");


scanf("%d", &num);
if (num >= 0)
printf("%d is a positive number \n", num);
else
printf("%d is a negative number \n", num);
}

Output:

4
Q.5 Write a C program to find if a given number is even or
odd.

#include <stdio.h>
int main() {
int num;
printf("Enter an integer: ");
scanf("%d", &num);

if(num % 2 == 0)
printf("%d is even.", num);
else
printf("%d is odd.", num);

return 0;
}

Output:

5
Q.6 Write a C program to find the greatest number among
three numbers.

#include <stdio.h>
int main() {
int num1, num2, num3;

printf("Enter three numbers:");


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

if (num1 >= num2 && num1 >= num3)


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

else if (num2 >= num1 && num2 >= num3)


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

else
printf("%d is the largest number.", num3);
return 0;
}

Output:

6
Q.7 Write a C program of swapping of two numbers.

#include<stdio.h>
int main()
{
int a, b;
printf("Enter the value of number a:");
scanf("%d", &a);

printf("Enter the value of number b:");


scanf("%d", &b);

printf("Before swap a=%d b=%d",a,b);


a=a+b;
b=a-b;
a=a-b;
printf("\nAfter swap a=%d b=%d",a,b);
return 0;
}

Output:

7
Q.8 Write a C program to make a simple calculator using
switch case.

#include <stdio.h>

int main() {

char operator;
int num1, num2;

printf("Enter an operator (+, -, *, /): ");


scanf(" %c", &operator);
printf("Enter two operands: ");
scanf("%d %d", &num1, &num2);

switch (operator) {
case '+':
printf("%d + %d = %d", num1, num2, num1 + num2);
break;
case '-':
printf("%d - %d = %d", num1, num2, num1 - num2);
break;
case '*':
printf("%d * %d = %d", num1, num2, num1 * num2);
break;
case '/':
if (num2 != 0) {
printf("%d / %d = %.2lf", num1, num2, (double)num1 / num2);
} else {
printf("Error! Division by zero is not allowed");
}
break;
default:
printf("Error! Operator is not correct");
}

return 0;
}

Output:
(+) addition:

8
(-) subtraction:

(*) multiplication:

(/) division:

9
Q.9 Write a C program to find the sum of natural numbers.

#include <stdio.h>
int main() {
int n, i, sum = 0;

printf("Enter a positive integer: ");


scanf("%d", &n);

for (i = 1; i <= n; ++i) {


sum += i;
}

printf("Sum = %d", sum);


return 0;
}

Output:

10
Q.10 Write a C program to generate table of any number.

#include <stdio.h>
int main() {
int n;
printf("Enter an integer: ");
scanf("%d", &n);

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


printf("%d * %d = %d \n", n, i, n * i);
}
return 0;
}

Output:

11
Q.11 Write a C program to find factorial of a number

#include<stdio.h>

int main() {

int x,fact=1,n;

printf("Enter a number to find factorial: ");

scanf("%d",&n);

for(x=1;x<=n;x++)

fact=fact*x;

printf("Factorial of %d is: %d",n,fact);

return 0;
}

Output:

12
Q.12 Write a C program to find if a given number is Prime or
not.

#include <stdio.h>

int main()
{
int i, num, n = 0;

printf("Enter any number to Check for Prime: ");


scanf("%d", &num);

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


{
if (num % i == 0)
{
n++;
break;
}
}
if (n == 0 && num != 1)
{
printf("%d is a Prime number", num);
}
else
{
printf("%d is not a Prime number", num);
}

return 0;

Output:

13
Q.13 Write a C program to print Fibonacci series.

#include<stdio.h>
int main()
{
int n1=0,n2=1,n3,i,number;
printf("Enter the number of elements:");
scanf("%d",&number);
printf("\n%d %d",n1,n2);
for(i=2;i<number;++i)
{
n3=n1+n2;
printf(" %d",n3);
n1=n2;
n2=n3;
}
return 0;
}

Output:

14
Q.14 Write a C program to print simple pyramid patterns.

#include <stdio.h>
int main() {
int i, j, rows;
printf("Enter the number of rows: ");
scanf("%d", &rows);
for (i = 1; i <= rows; ++i) {
for (j = 1; j <= i; ++j) {
printf("* ");
}
printf("\n");
}
return 0;
}

Output:

15
Q.15 Write a C program to print weekdays.

#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;
}

Output:

16
Q.16 Write a C program to find the reverse of a given digit.

#include <stdio.h>

int main() {

int n, reverse = 0, remainder;

printf("Enter an integer: ");


scanf("%d", &n);

while (n != 0) {
remainder = n % 10;
reverse = reverse * 10 + remainder;
n /= 10;
}

printf("Reversed number = %d", reverse);

return 0;
}

Output:

17
Q.17 Write a C program to check given number is Armstrong
or not.
#include <stdio.h>
int main() {
int num, originalNum, remainder, result = 0;
printf("Enter a three-digit integer: ");
scanf("%d", &num);
originalNum = num;

while (originalNum != 0) {
remainder = originalNum % 10;
result += remainder * remainder * remainder;
originalNum /= 10;
}

if (result == num)
printf("%d is an Armstrong number.", num);
else
printf("%d is not an Armstrong number.", num);

return 0;
}

Output:

18
Q.18 Write a C program to check given number is palindrome
or not.
● A palindrome number is a number that is the same after the reverse.
For example 121, is 121 after reverse.

#include<stdio.h>
int main()
{
int n,r,sum=0,temp;
printf("enter the number=");
scanf("%d",&n);
temp=n;
while(n>0)
{
r=n%10;
sum=(sum*10)+r;
n=n/10;
}
if(temp==sum)
printf("The given number is palindrome number");
else
printf("The given number is not palindrome");
return 0;
}

Output:

19

You might also like