Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
2 views

c_programming_chap_5 (1)

The document is a tutorial on C programming focused on simple arithmetic problems, covering various topics such as addition, multiplication, and determining if a number is positive or negative. It includes example code for each problem, demonstrating how to implement these operations in C. Additional topics include finding maximum values, calculating sums, performing integer division, and generating mathematical series.

Uploaded by

Genius Shivam
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

c_programming_chap_5 (1)

The document is a tutorial on C programming focused on simple arithmetic problems, covering various topics such as addition, multiplication, and determining if a number is positive or negative. It includes example code for each problem, demonstrating how to implement these operations in C. Additional topics include finding maximum values, calculating sums, performing integer division, and generating mathematical series.

Uploaded by

Genius Shivam
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

C Programming

Simple Arithmetic Problems


Chapter - 5

t
en
1. Addition of Integers
1
2
3

4
5
6
// C program to add two integers
# include < stdio .h >

int main () {
int a , b , sum ;
t ud
printf ( " Enter ␣ two ␣ integers : ␣ " ) ;
As
7 scanf ( " % d ␣ % d " , &a , & b ) ;
8 sum = a + b ;
9 printf ( " Sum : ␣ % d \ n " , sum ) ;
10 return 0;
C

11 }
eB

2. Multiplication of Integers
1 // C program to multiply two integers
th

2 # include < stdio .h >


3

4 int main () {
5 int a , b , product ;
6 printf ( " Enter ␣ two ␣ integers : ␣ " ) ;
7 scanf ( " % d ␣ % d " , &a , & b ) ;
8 product = a * b ;
9 printf ( " Product : ␣ % d \ n " , product ) ;
10 return 0;
11 }

1
3. Determining if a Number is Positive or Neg-
ative
1 // C program to check if a number is positive or negative
2 # include < stdio .h >
3
4 int main () {
5 int num ;
6 printf ( " Enter ␣ an ␣ integer : ␣ " ) ;
7 scanf ( " % d " , & num ) ;
8 if ( num > 0)

t
9 printf ( " Positive \ n " ) ;

en
10 else if ( num < 0)
11 printf ( " Negative \ n " ) ;
12 else
13 printf ( " Zero \ n " ) ;

d
14 return 0;
}
15
tu
As
4. Determining if a Number is Even or Odd
1 // C program to check if a number is even or odd
BC

2 # include < stdio .h >


3
4 int main () {
5 int num ;
e

6 printf ( " Enter ␣ an ␣ integer : ␣ " ) ;


7 scanf ( " % d " , & num ) ;
th

8 if ( num % 2 == 0)
9 printf ( " Even \ n " ) ;
10 else
11 printf ( " Odd \ n " ) ;
12 return 0;
13 }

5. Maximum of Two Numbers


1 // C program to find the maximum of two numbers
2 # include < stdio .h >

youtube.com/@thebcastudent 2
3

4 int main () {
5 int a , b ;
6 printf ( " Enter ␣ two ␣ integers : ␣ " ) ;
7 scanf ( " % d ␣ % d " , &a , & b ) ;
8 if ( a > b )
9 printf ( " Maximum : ␣ % d \ n " , a ) ;
10 else
11 printf ( " Maximum : ␣ % d \ n " , b ) ;
12 return 0;
13 }

t
en
6. Maximum of Three Numbers
// C program to find the maximum of three numbers

d
1
2 # include < stdio .h >
3
tu
4 int main () {
5 int a , b , c ;
As
6 printf ( " Enter ␣ three ␣ integers : ␣ " ) ;
7 scanf ( " % d ␣ % d ␣ % d " , &a , &b , & c ) ;
8 if ( a >= b && a >= c )
9 printf ( " Maximum : ␣ % d \ n " , a ) ;
BC

10 else if ( b >= a && b >= c )


11 printf ( " Maximum : ␣ % d \ n " , b ) ;
12 else
13 printf ( " Maximum : ␣ % d \ n " , c ) ;
14 return 0;
e

15 }
th

7. Sum of the First n Numbers


1 // C program to calculate the sum of the first n numbers
2 # include < stdio .h >
3
4 int main () {
5 int n , sum = 0;
6 printf ( " Enter ␣ a ␣ positive ␣ integer : ␣ " ) ;
7 scanf ( " % d " , & n ) ;
8 for ( int i = 1; i <= n ; ++ i ) {

youtube.com/@thebcastudent 3
9 sum += i ;
10 }
11 printf ( " Sum : ␣ % d \ n " , sum ) ;
12 return 0;
13 }

8. Sum of Given n Numbers

t
1 // C program to find the sum of given n numbers
2 # include < stdio .h >

en
3
4 int main () {
5 int n , sum = 0;
6 printf ( " Enter ␣ the ␣ number ␣ of ␣ elements : ␣ " ) ;
7

8
9
10
11
12
scanf ( " % d " , & n ) ;
int numbers [ n ];
printf ( " Enter ␣ % d ␣ numbers : ␣ " , n ) ;
for ( int i = 0; i < n ; i ++) {
t
scanf ( " % d " , & numbers [ i ]) ;
sum += numbers [ i ];
ud
As
13 }
14 printf ( " Sum : ␣ % d \ n " , sum ) ;
15 return 0;
16 }
C
eB

9. Integer Division
1 // C program to perform integer division
2 # include < stdio .h >
th

3
4 int main () {
5 int dividend , divisor , quotient , remainder ;
6 printf ( " Enter ␣ dividend ␣ and ␣ divisor : ␣ " ) ;
7 scanf ( " % d ␣ % d " , & dividend , & divisor ) ;
8 quotient = dividend / divisor ;
9 remainder = dividend % divisor ;
10 printf ( " Quotient : ␣ %d , ␣ Remainder : ␣ % d \ n " , quotient ,
remainder ) ;
11 return 0;
12 }

youtube.com/@thebcastudent 4
10. Digit Reversing
1 // C program to reverse the digits of a number
2 # include < stdio .h >
3
4 int main () {
5 int num , reversed = 0;
6 printf ( " Enter ␣ an ␣ integer : ␣ " ) ;
7 scanf ( " % d " , & num ) ;
8 while ( num != 0) {
9 reversed = reversed * 10 + num % 10;
10 num /= 10;

t
11 }

en
12 printf ( " Reversed ␣ Number : ␣ % d \ n " , reversed ) ;
13 return 0;
14 }

d
tu
11. Table Generation for n
As
1 // C program to generate multiplication table for n
2 # include < stdio .h >
3

4 int main () {
BC

5 int n ;
6 printf ( " Enter ␣ an ␣ integer : ␣ " ) ;
7 scanf ( " % d " , & n ) ;
8 for ( int i = 1; i <= 10; ++ i ) {
e

9 printf ( " % d ␣ x ␣ % d ␣ = ␣ % d \ n " , n , i , n * i ) ;


10 }
th

11 return 0;
12 }

12. Exponentiation (ab)


1 // C program to calculate a ^ b
2 # include < stdio .h >
3
4 int main () {
5 int a , b , result = 1;
6 printf ( " Enter ␣ base ␣ and ␣ exponent : ␣ " ) ;

youtube.com/@thebcastudent 5
7 scanf ( " % d ␣ % d " , &a , & b ) ;
8 for ( int i = 1; i <= b ; ++ i ) {
9 result *= a ;
10 }
11 printf ( " % d ^% d ␣ = ␣ % d \ n " , a , b , result ) ;
12 return 0;
13 }

13. Factorial of a Number

t
1 // C program to calculate factorial of a number

en
2 # include < stdio .h >
3
4 int main () {
int n , factorial = 1;

d
5
6 printf ( " Enter ␣ a ␣ positive ␣ integer : ␣ " ) ;
7 scanf ( " % d " , & n ) ;
tu
8 for ( int i = 1; i <= n ; ++ i ) {
9 factorial *= i ;
As
10 }
11 printf ( " Factorial : ␣ % d \ n " , factorial ) ;
12 return 0;
13 }
BC

14. Sine Series Calculation


e

// C program to calculate sine series for an angle


th

1
2 # include < stdio .h >
3 # include < math .h >
4
5 int main () {
6 int n ;
7 float x , sum = 0;
8 printf ( " Enter ␣ value ␣ of ␣ x ␣ ( in ␣ radians ) ␣ and ␣ number ␣ of ␣ terms
:␣");
9 scanf ( " % f ␣ % d " , &x , & n ) ;
10 for ( int i = 0; i < n ; ++ i ) {
11 int sign = ( i % 2 == 0) ? 1 : -1;
12 sum += sign * pow (x , 2 * i + 1) / tgamma (2 * i + 2) ;
13 }

youtube.com/@thebcastudent 6
14 printf ( " Sine ␣ value : ␣ % f \ n " , sum ) ;
15 return 0;
16 }

15. Cosine Series Calculation


1 // C program to calculate cosine series for an angle
2 # include < stdio .h >
3 # include < math .h >
4

t
5 int main () {

en
6 int n ;
7 float x , sum = 0;
8 printf ( " Enter ␣ value ␣ of ␣ x ␣ ( in ␣ radians ) ␣ and ␣ number ␣ of ␣ terms
:␣");
9

d
scanf ( " % f ␣ % d " , &x , & n ) ;
10
tu
for ( int i = 0; i < n ; ++ i ) {
11 int sign = ( i % 2 == 0) ? 1 : -1;
12 sum += sign * pow (x , 2 * i ) / tgamma (2 * i + 1) ;
As
13 }
14 printf ( " Cosine ␣ value : ␣ % f \ n " , sum ) ;
15 return 0;
16 }
BC

16. Combinations (nCr)


e

// C program to calculate nCr


th

1
2 # include < stdio .h >
3
4 int factorial ( int n ) {
5 if ( n == 0 || n == 1)
6 return 1;
7 else
8 return n * factorial ( n - 1) ;
9 }
10
11 int main () {
12 int n , r ;
13 printf ( " Enter ␣ n ␣ and ␣ r : ␣ " ) ;
14 scanf ( " % d ␣ % d " , &n , & r ) ;

youtube.com/@thebcastudent 7
15 int nCr = factorial ( n ) / ( factorial ( r ) * factorial ( n - r )
);
16 printf ( " nCr : ␣ % d \ n " , nCr ) ;
17 return 0;
18 }

17. Pascal’s Triangle


1 // C program to generate Pascal ’s triangle
2 # include < stdio .h >

t
3

en
4 int factorial ( int n ) {
5 if ( n == 0 || n == 1)
6 return 1;
else

d
7
8 return n * factorial ( n - 1) ;
9 }
tu
10
11 int main () {
As
12 int n ;
13 printf ( " Enter ␣ the ␣ number ␣ of ␣ rows : ␣ " ) ;
14 scanf ( " % d " , & n ) ;
15 for ( int i = 0; i < n ; i ++) {
BC

16 for ( int j = 0; j <= i ; j ++) {


17 printf ( " % d ␣ " , factorial ( i ) / ( factorial ( j ) *
factorial ( i - j ) ) ) ;
18 }
19 printf ( " \ n " ) ;
e

20 }
th

21 return 0;
22 }

18. Prime Number Checking


1 // C program to check if a number is prime
2 # include < stdio .h >
3
4 int main () {
5 int num , isPrime = 1;
6 printf ( " Enter ␣ an ␣ integer : ␣ " ) ;

youtube.com/@thebcastudent 8
7 scanf ( " % d " , & num ) ;
8 if ( num <= 1) isPrime = 0;
9 for ( int i = 2; i <= num / 2; ++ i ) {
10 if ( num % i == 0) {
11 isPrime = 0;
12 break ;
13 }
14 }
15 if ( isPrime )
16 printf ( " % d ␣ is ␣ a ␣ prime ␣ number .\ n " , num ) ;
17 else
18 printf ( " % d ␣ is ␣ not ␣ a ␣ prime ␣ number .\ n " , num ) ;
return 0;

t
19

20 }

en
19. Factors of a Number

d
tu
1 // C program to find all factors of a number
2 # include < stdio .h >
As
3

4 int main () {
5 int num ;
6 printf ( " Enter ␣ an ␣ integer : ␣ " ) ;
BC

7 scanf ( " % d " , & num ) ;


8 printf ( " Factors ␣ of ␣ % d ␣ are : ␣ " , num ) ;
9 for ( int i = 1; i <= num ; ++ i ) {
10 if ( num % i == 0)
11 printf ( " % d ␣ " , i ) ;
e

12 }
th

13 printf ( " \ n " ) ;


14 return 0;
15 }

20. Perfect Number Checking


1 // C program to check if a number is a perfect number
2 # include < stdio .h >
3
4 int main () {
5 int num , sum = 0;

youtube.com/@thebcastudent 9
6 printf ( " Enter ␣ an ␣ integer : ␣ " ) ;
7 scanf ( " % d " , & num ) ;
8 for ( int i = 1; i <= num / 2; ++ i ) {
9 if ( num % i == 0)
10 sum += i ;
11 }
12 if ( sum == num )
13 printf ( " % d ␣ is ␣ a ␣ perfect ␣ number .\ n " , num ) ;
14 else
15 printf ( " % d ␣ is ␣ not ␣ a ␣ perfect ␣ number .\ n " , num ) ;
16 return 0;
17 }

t
en
21. Greatest Common Divisor (GCD) of Two
Numbers

d
tu
1 // C program to find the GCD of two numbers
2 # include < stdio .h >
As
3
4 int gcd ( int a , int b ) {
5 if ( b == 0)
6 return a ;
BC

7 return gcd (b , a % b ) ;
8 }
9
10 int main () {
11 int a , b ;
e

12 printf ( " Enter ␣ two ␣ integers : ␣ " ) ;


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

13
14 printf ( " GCD ␣ of ␣ % d ␣ and ␣ % d ␣ is ␣ % d \ n " , a , b , gcd (a , b ) ) ;
15 return 0;
16 }

22. Swapping Two Numbers


1 // C program to swap two numbers
2 # include < stdio .h >
3
4 int main () {

youtube.com/@thebcastudent 10
5 int a , b , temp ;
6 printf ( " Enter ␣ two ␣ integers : ␣ " ) ;
7 scanf ( " % d ␣ % d " , &a , & b ) ;
8 // Swapping logic
9 temp = a ;
10 a = b;
11 b = temp ;
12 printf ( " After ␣ swapping : ␣ a ␣ = ␣ %d , ␣ b ␣ = ␣ % d \ n " , a , b ) ;
13 return 0;
14 }

t
en
t ud
C As
eB
th

youtube.com/@thebcastudent 11

You might also like