You are on page 1of 13

/*Print the numbers from 1 to 10 with their squares*/ PROGRAM: #include<stdio.h> #include <stdio.h> #include<conio.

h> void main() { int i; clrscr(); printf(\n The numbers with their squares is: \n); for(i = 1; i <= 10; i = i + 1) printf("%d = %d \n", i, i * i); getch();
}

OUTPUT The numbers with their squares is: 1=1 2=4 3=9 4 = 16 5 = 25 6 = 36 7 = 49 8 = 64 9=8

/*Sum of n numbers using for, do-while*/ PROGRAM: #include <stdio.h> #include <conio.h> void main() { int n, count, sum=0,op; clrscr(); printf("Enter an integer: "); scanf("%d",&n); printf("\n Enter your choice\n1.do while loop\n2.for loop: "); scanf("%d",&op); switch(op) { case 1: count=1; do { sum+=count; ++count; } while(count<=n); printf("\n\nSum = %d",sum);

break; case 2: for(count=1;count<=n;++count) { sum+=count; } printf("\n\nSum = %d",sum); break; default: printf("\n\n Invalid Option"); } getch(); }

OUTPUT Enter an integer: 15 Enter your choice 1. do while loop 2. for loop 1 Sum = 120

/*Factorial of a given number using functions*/


PROGRAM: #include<stdio.h> #include <conio.h> void main() { int i,fact,num; int factorial(int); clrscr(); printf("Enter a number: "); scanf("%d",&num); fact = factorial(num); printf("\n\nFactorial of %d is: %d",num,fact); getch(); }

int factorial(int n) { int i,f=1; for(i=1; i<=n; i++) f=f*i; return f; }

OUTPUT Enter a number: 5 Factorial of 5 is: 120

/*swap two numbers using call by value and call by reference*/


PROGRAM: include<stdio.h> #include<conio.h> void main() { inta,b; void swap1(int, int); void swap2(int *x,int *y); clrscr(); printf("Enter the value of a and b"); scanf("%d%d",&a,&b); printf("\nBefore swapping result is: \n a=%d \t b=%d\n",a,b); swap1(a,b); printf("\n In call by value, after swapping result is:\n a=%d \t b=%d \n",a,b); swap2(&a,&b); printf("\n In call by reference, after swapping result is:\n a=%d \t b=%d ",a,b); getch(); } void swap1(int x, int y) { int t;

t=x;

x=y; y=t; } void swap2(int *x,int *y) { int t; t=*x; *x=*y; *y=t; }

OUTPUT Enter the value of a and b 40 70 Before swapping result is: a=40 b=70

In call by value, after swapping result is: a=40 b=70

In call by reference, after swapping result is: a=70 b=40

/*smallest and largest element in an array*/


PROGRAM: #include<stdio.h> #include<conio.h> void main() { int a[50],i,n,large,small; clrscr(); printf("How many elements:"); scanf("%d",&n); printf("Enter the Array:"); for(i=0;i<n;++i) scanf("%d",&a[i]); large=small=a[0]; for(i=1;i<n;++i) { if(a[i]>large) large=a[i]; if(a[i]<small) small=a[i]; } printf("The largest element is %d",large);

printf("\nThe smallest element is %d",small); getch(); }

OUTPUT How many elements: 6 Enter the Array: 98 65 23 87 54 21 The largest element is 98 The smallest element is 21

You might also like