progr3
DESCRIPTION
TRANSCRIPT
������������ ����������������� 1
1. Write a c program to swap two numbers. void main() { int a,b,c; clrscr(); printf("\n Enter two no "); scanf("%d%d",&a,&b); printf("\n Before Swaping value of a and b %d %d",a,b); c=a; a=b; b=c; printf("\n After Swaping value of a and b %d %d",a,b); getch(); }
2. Write a c program to swap two numbers without using third variable. #include<stdio.h> #include<conio.h> int main() { int a=5,b=10; //process one a=b+a; b=a-b; a=a-b; printf("a= %d b= %d",a,b); return 0; getch(); }
3. Write a c program to swap two numbers without using third variable. #include<stdio.h> #include<conio.h> int main() { int a=5,b=10; //process one a=b+a;
������������ ����������������� 2
b=a-b; a=a-b; printf("a= %d b= %d",a,b); return 0; getch(); }
1. How to calculate power of a number in c #include<stdio.h> int main(){ int pow,num,i=1; long int sum=1; printf("\nEnter a number: "); scanf("%d",&num); printf("\nEnter power: "); scanf("%d",&pow); while(i<=pow){ sum=sum*num; i++; } printf("\n%d to the power %d is: %ld",num,pow,sum); return 0; }
2. Code for swapping in c #include<stdio.h> int main(){ int a,b,temp; printf("Enter any two integers: "); scanf("%d%d",&a,&b); printf("Before swapping: a = %d, b=%d",a,b); temp= a; a=b; b=a; printf("\nAfter swapping: a = %d, b=%d",a,b); return 0; }
������������ ����������������� 3
3. Count the number of digits in c
#include<stdio.h> int main(){ int num,count=0; printf("Enter a number: "); scanf("%d",&num); while(num){ num=num/10; count++; } printf("Total digits is: %d",count); return 0; } Sample output: Enter a number: 23 Total digits is: 2
4. C code to count the total number of digit using for loop
#include<stdio.h> int main(){ int num,count=0; printf("Enter a number: "); scanf("%d",&num); for(;num!=0;num=num/10) count++; printf("Total digits is: %d",count); return 0; } Sample output: Enter a number: 456 Total digits is: 3
5. Simple program of c find the largest number #include<stdio.h> int main(){ int n,num,i; int big; printf("Enter the values of n: "); scanf("%d",&n);
������������ ����������������� 4
printf("Number %d",1); scanf("%d",&big); for(i=2;i<=n;i++){ printf("Number %d: ",i); scanf("%d",&num); if(big<num) big=num; } printf("Largest number is: %d",big); return 0; } Sample Output: Enter the values of n: Number 1: 12 Number 2: 32 Number 3: 35 Largest number is: 35
6. C program to calculate sum of digits #include<stdio.h> int main(){ int num,sum=0,r; printf("Enter a number: "); scanf("%d",&num); while(num){ r=num%10; num=num/10; sum=sum+r; } printf("Sum of digits of number: %d",sum); return 0; } Sample output: Enter a number: 123 Sum of digits of number: 6
������������ ����������������� 5
7. Sum of digits of a number in c using for loop
#include<stdio.h> int main(){ int num,sum=0,r; printf("Enter a number: "); scanf("%d",&num); for(;num!=0;num=num/10){ r=num%10; sum=sum+r; } printf("Sum of digits of number: %d",sum); return 0; }
8. Sum of digits in c using recursion #include<stdio.h> int getSum(int); int main(){ int num,sum; printf("Enter a number: "); scanf("%d",&num); sum = getSum(num); printf("Sum of digits of number: %d",sum); return 0; } int getSum(int num){ static int sum =0,r; if(num!=0){ r=num%10; sum=sum+r; getSum(num/10); }
������������ ����������������� 6
return sum; } Sample output: Enter a number: 45 Sum of digits of number: 9
9. Write a c program to find out L.C.M. of two numbers.
Definition of LCM (Least common multiple): LCM of two integers is a smallest positive integer which is multiple of both integers that it is divisible by the both of the numbers. For example: LCM of two integers 2 and 5 is 10 since 10 is the smallest positive numbers which is divisible by both 2 and 5. #include<stdio.h> int main(){ int n1,n2,x,y; printf("\nEnter two numbers:"); scanf("%d %d",&n1,&n2); x=n1,y=n2; while(n1!=n2){ if(n1>n2) n1=n1-n2; else n2=n2-n1; } printf("L.C.M=%d",x*y/n1); return 0; }
������������ ����������������� 7
10. Write a c program to find out the sum of
infinite G.P. series Definition of geometric progression (G.P.): A series of numbers in which ratio of any two consecutive numbers is always a same number that is constant. This constant is called as common ratio. Example of G.P. series: 2 4 8 16 32 64 Here common difference is 2 since ratio any two consecutive numbers for example 32 / 16 or 64/32 is 2. Sum of G.P. series:Sn =a(1–rn+1)/(1-r) Tn term of G.P. series:Tn = arn-1 Sum of infinite G.P. series: Sn = a/(1-r) if 1 > r = a/(r-1) if r > 1
#include<stdio.h> int main(){ float a,r; float sum=0; printf("Enter the first number of the G.P. series: "); scanf("%f",&a); printf("Enter the common ratio of G.P. series: "); scanf("%f",&r); if(1 > r) sum = a/(1-r); else sum = a/(r-1); printf("\nSum of the infinite G.P. series: %f",sum); return 0; }
������������ ����������������� 8
Sample output: Enter the first number of the G.P. series: 1 Enter the common ratio of G.P. series: .5 Sum of the infinite G.P. series: 2.000000 Enter the first number of the G.P. series: 5 Enter the common ratio of G.P. series: 2 Sum of the infinite G.P. series: 5.000000