0% found this document useful (0 votes)
4 views6 pages

C Programs for Basic Operations and Logic

Uploaded by

s64021360
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)
4 views6 pages

C Programs for Basic Operations and Logic

Uploaded by

s64021360
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

Expression Evaluation:

[Link] a C program, which takes two integer operands and one operator from the user,
performs the operation and then prints the result. (Consider the operators +,-,*, /, % and
use Switch Statement).

Program:

#include <stdio.h>

#include <conio.h>

void main()

int a, b, c;

char ch;

clrscr();

printf("Enter your operator(+,-, /, *, %)\n");

scanf("%c", &ch);

printf("Enter the values of a and b\n");

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

switch (ch)

case '+':

c = a + b;

printf("addition of two numbers is %d", c);

break;

case '-':

case '*':
case '/':

case '%':

c = a - b;

printf("substraction of two numbers is %d", c);

break;

c = a * b;

printf("multiplication of two numbers is %d", c);

break;

c = a / b;

printf("remainder of two numbers is %d", c);

break;

c = a % b;

printf("quotient of two numbers is %d", c);

break;

default:

printf("Invalid operator");

break;

getch();

Output:

Enter your operator(+,-, /, *, %)

Enter the values of a and b


45

addition of two numbers is 9

2. Write a program that finds if a given number is a prime number

Program:

#include<stdio.h>

int main()

int n, i, flag = 0;

printf("\nEnter a number:”);

scanf("%d",&n);

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

if (n % i == 0)

flag = 1;

break;

if(flag==0){

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

else{

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

}
return(0);

Output:

Enter a number:43

43 is a prime number

3. Write a C program to find the sum of individual digits of a positive integer and test
given number is palindrome.

Program:

#include<stdio.h>

#include<conio.h>

void main ()

int number = 0, digit = 0, sum= 0 , rev = 0, temp;

clrscr();

printf("Enter any number\n ");

scanf("%d", &number);

temp = number;

while (number != 0)

digit = number % 10;

sum = sum+digit;

rev = rev * 10 + digit;

number = number/ 10;

}
printf ("Sum of individual digits of a given number is %d \n", sum);

if ( temp == rev)

printf ("%d is a Palindrome",temp);

else

printf ("%d is not a Palindrome",temp);

getch();

Output:

Enter any number

656

Sum of individual digits of a given number is 17

656 is a palindrome

4. A Fibonacci sequence is defined as follows: the first and second terms in the
sequenceare 0 and 1. Subsequent terms are found by adding the preceding two terms in
the sequence. Write a C program to generate the first n terms of the sequence.

Program:

#include <stdio.h>

int main()

int i, n, t1 = 0, t2 = 1, nextTerm;
printf("Enter the number of terms: ");

scanf("%d", &n);

printf("Fibonacci Series: ");

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

printf("%d, ", t1);

nextTerm = t1 + t2;

t1 = t2;

t2 = nextTerm;

return 0;

Output:

Enter the number of terms: 8

Fibonacci Series: 0, 1, 1, 2, 3, 5, 8, 13,

You might also like