Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 12

/* 1) C program to print the following pattern - pattern1.

c */

#include<stdio.h>

#include<conio.h>

main()

int i,j,k,n;

printf("Enter no. of rows: ");

scanf("%d",&n);

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

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

printf("%d",i);

printf("\n");

getch();

}
/* 2) C program to print the following pattern - pattern2.c */

#include<stdio.h>

#include<conio.h>

main()

int i,j,k,n;

printf("Enter no. of rows: ");

scanf("%d",&n);

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

for(j=n-i;j>=1;j--)

printf(" ");

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

printf("%d ",i);

printf("\n");

getch();

}
/* 3) C program - pattern3.c */

#include<stdio.h>

#include<conio.h>

main()

int i,j,k,n;

printf("Enter no. of rows: ");

scanf("%d",&n);

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

for(j=n-i;j>=1;j--)

printf(" ");

for(k=1;k<=(2*i-1);k++)

printf("*");

printf("\n");

}
getch();

/* 4) Pattern printing 4 */

#include<stdio.h>

#include<conio.h>

main()

int i,j,k,n;

printf("Enter no of terms: ");

scanf("%d",&n);

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

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

printf(" ");

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

{
printf("*");

printf("\n");

getch();

/* 5) Pattern printing 5 */

#include<stdio.h>

#include<conio.h>

main()

int i,j,k,n;

printf("Enter no of terms: ");

scanf("%d",&n);

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

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

{
printf("*");

printf("\n");

getch();

/* 6) C program to find the value of X^N */

#include<stdio.h>

#include<conio.h>

int power(int,int);

main()

int x,n;

printf("Enter value of x and n: ");

scanf("%d%d",&x,&n);

power(x,n);
printf("%d^%d = %d",x,n,power(x,n));

getch();

int power(int a,int b)

int count=1,sum=1;

while(count<=b)

sum=sum*a;

count++;

return sum;

/* 7) C program to calculate the series: 1+1/3+1/5+1/7+..... */

#include<stdio.h>

#include<conio.h>

main()

{
int n,i;

float sum=0.0, x=1.0;

printf("\n 1 + 1/3 + 1/5 + 1/7 +......");

printf("\n Enter no of terms: ");

scanf("%d",&n);

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

sum=sum+1/x;

x=x+2;

printf("Sum of series: %f",sum);

getch();

/* 8) Calculate the sum of the series using function : 1/1! + 2/2! + 3/3! + ... */

#include<stdio.h>

#include<conio.h>

double series_sum(double);

main()
{

double num,sum;

printf("\n 1/1! + 2/2! + 3/3! +......");

printf("\n Enter No. of terms of the series: ");

scanf("%lf",&num);

sum=series_sum(num);

printf("\n Sum of series= %lf",sum);

getch();

double series_sum(double m)

double sum2=0,fact=1,i;

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

fact=fact*i;

sum2=sum2+(i/fact);

return(sum2);

}
/* 9) Swap two nos. using call by value */

#include<stdio.h>

#include<conio.h>

void swap(int a,int b);

main()

int x,y;

printf("\n Enter two nos. : ");

scanf("%d%d",&x,&y);

printf("\n value before swap: x=%d y=%d",x,y);

swap(x,y);

getch();

void swap(int a,int b)

int temp;

temp=a;

a=b;

b=temp;

printf("\n Value after swap: x=%d y=%d",a,b);


}

/* 10) Swap two nos. using call by reference */

#include<stdio.h>

#include<conio.h>

void swap(int *,int *);

main()

int x,y;

printf("\n Enter two nos. : ");

scanf("%d%d",&x,&y);

printf("\n value before swap: x=%d y=%d",x,y);

swap(&x,&y);

printf("\n value after swap: x=%d y=%d",x,y);

getch();

void swap(int *a,int *b)


{

int temp;

temp=*a;

*a=*b;

*b=temp;

You might also like