0% found this document useful (0 votes)
2 views15 pages

C Program

The document contains multiple C programs demonstrating various patterns and algorithms, including printing half pyramids with stars, numbers, and alphabets, as well as inverted pyramids. It also includes programs for finding the largest element in an array, generating Fibonacci series, checking for palindromes, and performing basic arithmetic operations using functions. Additionally, it explains call by value and call by reference methods in function calls.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
2 views15 pages

C Program

The document contains multiple C programs demonstrating various patterns and algorithms, including printing half pyramids with stars, numbers, and alphabets, as well as inverted pyramids. It also includes programs for finding the largest element in an array, generating Fibonacci series, checking for palindromes, and performing basic arithmetic operations using functions. Additionally, it explains call by value and call by reference methods in function calls.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 15

Program to print half pyramid using *

*
* *
* * *
* * * *
* * * * *

1. #include<stdio.h>
2. int main() {
3. int i, j, rows;
4. printf("Enter number of rows: ");
5. scanf("%d", &rows);
6. for (i=1; i<=rows; ++i) {
7. for (j=1; j<=i; ++j)
8. { printf("* "); }
9. printf("\n");
10. }
11. return0;
12. }

Program to print half pyramid a using numbers


1
1 2
1 2 3
1 2 3 4
1 2 3 4 5

1. #include<stdio.h>
2. int main() {
3. int i,j,rows;
4. printf("Enter number of rows: ");
5. scanf("%d", &rows);
6. for (i=1; i<=rows; ++i) {
7. for (j=1; j<=i; ++j)
8. { printf("%d ",j); }
9. printf("\n");
10. }
11. return0;
12. }
Program to print half pyramid using alphabets
A
B B
C C C
D D D D
E E E E E

1. #include<stdio.h>
2. int main() {
3. int i, j;
4. char input, alphabet='A';
5. printf("Enter the uppercase character you want to print in last row: ");
6. scanf("%c", &input);
7. for (i=1; i<=(input-'A'+1); ++i) {
8. for (j=1; j<=i; ++j)
9. { printf("%c", alphabet); }
10. ++alphabet;
11. printf("\n");
12. }
13. return0;
14. }

Inverted half pyramid using *

* * * * *
* * * *
* * *
* *
*

1. #include<stdio.h>
2. int main() {
3. int i, j, rows;
4. printf("Enter number of rows: ");
5. scanf("%d", &rows);
6. for (i=rows; i>=1; --i) {
7. for (j=1; j<=i; ++j)
8. { printf("* "); }
9. printf("\n");
10. }
11. return0;
12.

Inverted half pyramid using numbers


1 2 3 4 5
1 2 3 4
1 2 3
1 2
1

1. #include<stdio.h>
2. int main() {
3. int i ,j, rows;
4. printf("Enter number of rows: ");
5. scanf("%d", &rows);
6. for (i=rows; i>=1; --i) {
7. for (j=1; j<=i; ++j)
8. { printf("%d ",j); }
9. printf("\n");
10. }
11. return0;
12. }

Program to print full pyramid using *

*
* * *
* * * * *
* * * * * * *
* * * * * * * * *

1. #include<stdio.h>
2. int main() {
3. int i, space, rows, k=0;
4. printf("Enter number of rows: ");
5. scanf("%d", &rows);
6. for (i=1; i<=rows; ++i,k=0) {
7. for (space=1; space<=rows-i; ++space)
8. { printf(" "); }
9. while (k!=2*i-1) {
10. printf("* ");
11. ++k;
12. }
13. printf("\n");
14. }
15. return0;
16. }
17.

Program to print pyramid using numbers


1
2 3 2
3 4 5 4 3
4 5 6 7 6 5 4
5 6 7 8 9 8 7 6 5

1. #include<stdio.h>
2. int main() {
3. int i, space, rows, k=0, count=0, count1=0;
4. printf("Enter number of rows: ");
5. scanf("%d", &rows);
6. for (i=1; i<=rows; ++i) {
7. for (space=1; space<=rows-i; ++space) {
8. printf(" ");
9. ++count;
10. }
11. while (k!=2*i-1) {
12. if (count <= rows-1)
13. { printf("%d ", i+k);
14. ++count;
15. }
16. else {
17. ++count1;
18. printf("%d ", (i+k-2*count1));
19. }
20. ++k;
21. }
22. count1=count=k=0;
23. printf("\n");
24. }
25. return0;
26. }

Inverted full pyramid using *


* * * * * * * * *
* * * * * * *
* * * * *
* * *
*

1. #include<stdio.h>
2. int main() {
3. int rows, i, j, space;
4. printf("Enter number of rows: ");
5. scanf("%d", &rows);
6. for (i=rows; i>=1; --i) {
7. for (space=0; space<rows-i; ++space)
8. printf(" ");
9. for (j=i; j<=2*i-1; ++j)
10. printf("* ");
11. for (j=0; j<i-1; ++j)
12. printf("* ");
13. printf("\n");
14. }
15. return0;
16. }

Output Matrix:
24 29
6 25

C Program to Find Largest Element in an Array


1. #include<stdio.h>
2. int main(){
3. int i, n;
4. float arr[100];
5. printf("Enter the number of elements (1 to 100): ");
6. scanf("%d",&n);
7.
8. for(i =0; i < n;++i){
9. printf("Enter number%d: ", i +1);
10. scanf("%f",&arr[i]);
11. }
12.
13. // storing the largest number to arr[0]
14. for(i =1; i < n;++i){
15. if(arr[0]< arr[i])
16. arr[0]= arr[i];
17. }
18.
19. printf("Largest element = %.2f", arr[0]);
20.
21. return0;
22. }
Output
Enter the number of elements (1 to 100): 5
Enter number1: 34.5
Enter number2: 2.4
Enter number3: -35.5
Enter number4: 38.7
Enter number5: 24.5
Largest element = 38.70

Fibonacci Series in C using loop


#include<stdio.h>
int main()
{
int count, first_term =0, second_term =1, next_term, i;

//Ask user to input number of terms


printf("Enter the number of terms:\n");
scanf("%d",&count);

printf("First %d terms of Fibonacci series:\n",count);


for( i =0; i < count ; i++)
{
if( i <=1)
next_term = i;
else
{
next_term = first_term + second_term;
first_term = second_term;
second_term = next_term;
}
printf("%d\n",next_term);
}

return0;
}
Output:

Enter the number of terms:8


First8 terms of Fibonacci series:
0
1
1
2
3
5
8
13

Program to display Fibonacci series using recursion


#include<stdio.h>
int fibonacci_series(int);
int main()
{
int count, c =0, i;
printf("Enter number of terms:");
scanf("%d",&count);

printf("\nFibonacci series:\n");
for( i =1; i <= count ; i++)
{
printf("%d\n", fibonacci_series(c));
c++;
}

return0;
}
int fibonacci_series(int num)
{
if( num ==0)
return0;
elseif( num ==1)
return1;
else
return( fibonacci_series(num-1)+ fibonacci_series(num-2));
}
Output:

Enter number of terms:6


Fibonacci series:
0
1
1
2
3
5

Program 1: check palindrome using while loop

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

/* Here we are generating a new number (reverse_num)


* by reversing the digits of original input number
*/
temp=num;
while(temp!=0)
{
remainder=temp%10;
reverse_num=reverse_num*10+remainder;
temp/=10;
}

/* If the original input number (num) is equal to


* to its reverse (reverse_num) then its palindrome
* else it is not.
*/
if(reverse_num==num)
printf("%d is a palindrome number",num);
else
printf("%d is not a palindrome number",num);
return0;
}
Output:

Program 2: check palindrome using recursion


#include<stdio.h>

int check_palindrome(int num){

staticint reverse_num=0,rem;

if(num!=0){
rem=num%10;
reverse_num=reverse_num*10+rem;
check_palindrome(num/10);
}

return reverse_num;
}
int main(){
int num, reverse_num;

printf("Enter a number: ");


scanf("%d",&num);
reverse_num = check_palindrome(num);

if(num==reverse_num)
printf("%d is a palindrome number",num);
else
printf("%d is not a palindrome number",num);

return0;
}
Output:

01. Sum using function.


#include <stdio.h>

int sum (int, int);//function declaration


int main (void)
{
int total;
printf("\n\n Function : a simple structure of function
:\n");
printf("------------------------------------------------
\n");
total = sum (5, 6);//function call
printf ("The total is : %d\n", total);
return 0;
}
int sum (int a, int b) //function definition
{
int s;
s=a+b;
return s; //function returning a value
}

02. Square root using function.


#include <stdio.h>

double square(double num)


{
return (num * num);
}
int main()
{
int num;
double n;
printf("\n\n Function : find square of any number :\n");
printf("------------------------------------------------\n");

printf("Input any number for square : ");


scanf("%d", &num);
n = square(num);
printf("The square of %d is : %.2f\n", num, n);
return 0;
}

03. SWAP two numbers using function


#include<stdio.h>

void swap(int *,int *);


int main()
{

int n1,n2;
printf("\n\n Function : swap two numbers using function :\n");
printf("------------------------------------------------\n");
printf("Input 1st number : ");
scanf("%d",&n1);
printf("Input 2nd number : ");
scanf("%d",&n2);

printf("Before swapping: n1 = %d, n2 = %d ",n1,n2);


//pass the address of both variables to the function.
swap(&n1,&n2);

printf("\nAfter swapping: n1 = %d, n2 = %d \n\n",n1,n2);


return 0;
}

void swap(int *p,int *q)


{
//p=&n1 so p store the address of n1, so *p store the value of n1
//q=&n2 so q store the address of n2, so *q store the value of n2

int tmp;
tmp = *p; // tmp store the value of n1
*p=*q; // *p store the value of *q that is value of n2
*q=tmp; // *q store the value of tmp that is the value of n1
}

04. Decimal to binary using function

#include<stdio.h>

long toBin(int);

int main()

long bno;

int dno;

printf("\n\n Function : convert decimal to binary :\n");

printf("-------------------------------------------\n");

printf(" Input any decimal number : ");

scanf("%d",&dno);

bno = toBin(dno);

printf("\n The Binary value is : %ld\n\n",bno);


return 0;

long toBin(int dno)

long bno=0,remainder,f=1;

while(dno != 0)

remainder = dno % 2;

bno = bno + remainder * f;

f = f * 10;

dno = dno / 2;

return bno;

05. Calling functions

1. Call by value
2. Call by reference
1. CALL BY VALUE:
● In call by value method, the value of the variable is passed to the function as parameter.
● The value of the actual parameter can not be modified by formal parameter.
● Different Memory is allocated for both actual and formal parameters. Because, value of
actual parameter is copied to formal parameter.
Note:

● Actual parameter – This is the argument which is used in function call.


● Formal parameter – This is the argument which is used in function definition
EXAMPLE PROGRAM FOR C FUNCTION (USING CALL BY VALUE):
● In this program, the values of the variables “m” and “n” are passed to the function
“swap”.
● These values are copied to formal parameters “a” and “b” in swap function and used.
C
1 #include<stdio.h>

2 // function prototype, also called function declaration

3 void swap(int a, int b);

5 int main()

6 {

7 int m = 22, n = 44;

8 // calling swap function by value

9 printf(" values before swap m = %d \nand n = %d", m, n);

10 swap(m, n);

11 }

12

13 void swap(int a, int b)

14 {

15 int tmp;

16 tmp = a;

17 a = b;

18 b = tmp;

19 printf(" \nvalues after swap m = %d\n and n = %d", a, b);

20 }

OUTPUT:

values before swap m = 22


and n = 44
values after swap m = 44
and n = 22

2. CALL BY REFERENCE:
● In call by reference method, the address of the variable is passed to the function as
parameter.
● The value of the actual parameter can be modified by formal parameter.
● Same memory is used for both actual and formal parameters since only address is used by
both parameters.
EXAMPLE PROGRAM FOR C FUNCTION (USING CALL BY REFERENCE):
● In this program, the address of the variables “m” and “n” are passed to the function
“swap”.
● These values are not copied to formal parameters “a” and “b” in swap function.
● Because, they are just holding the address of those variables.
● This address is used to access and change the values of the variables.
C

1 #include<stdio.h>

2 // function prototype, also called function declaration

3 void swap(int *a, int *b);

5 int main()

6 {

7 int m = 22, n = 44;

8 // calling swap function by reference

9 printf("values before swap m = %d \n and n = %d",m,n);

10 swap(&m, &n);

11 }

12

13 void swap(int *a, int *b)

14 {

15 int tmp;

16 tmp = *a;

17 *a = *b;

18 *b = tmp;

19 printf("\n values after swap a = %d \nand b = %d", *a, *b);

20 }

OUTPUT:

values before swap m = 22


and n = 44
values after swap a = 44
and b = 22
Pdf pages: 19-24, 26-40, 43-46.

chapter 02: examples: 1, 2, 4, 5, 7, 10, 12, 13, 16.

You might also like