Professional Documents
Culture Documents
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
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
OUTPUT How many elements: 6 Enter the Array: 98 65 23 87 54 21 The largest element is 98 The smallest element is 21