C Program
C Program
*
* *
* * *
* * * *
* * * * *
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. }
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. }
* * * * *
* * * *
* * *
* *
*
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.
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. }
*
* * *
* * * * *
* * * * * * *
* * * * * * * * *
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.
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. }
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
return0;
}
Output:
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:
#include<stdio.h>
int main()
{
int num, reverse_num=0, remainder,temp;
printf("Enter an integer: ");
scanf("%d",&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;
if(num==reverse_num)
printf("%d is a palindrome number",num);
else
printf("%d is not a palindrome number",num);
return0;
}
Output:
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);
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
}
#include<stdio.h>
long toBin(int);
int main()
long bno;
int dno;
printf("-------------------------------------------\n");
scanf("%d",&dno);
bno = toBin(dno);
long bno=0,remainder,f=1;
while(dno != 0)
remainder = dno % 2;
f = f * 10;
dno = dno / 2;
return bno;
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:
5 int main()
6 {
10 swap(m, n);
11 }
12
14 {
15 int tmp;
16 tmp = a;
17 a = b;
18 b = tmp;
20 }
OUTPUT:
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>
5 int main()
6 {
10 swap(&m, &n);
11 }
12
14 {
15 int tmp;
16 tmp = *a;
17 *a = *b;
18 *b = tmp;
20 }
OUTPUT: