SlideShare a Scribd company logo
1 of 8
Download to read offline
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

More Related Content

What's hot (20)

88 c-programs
88 c-programs88 c-programs
88 c-programs
 
program in c
program in cprogram in c
program in c
 
Core programming in c
Core programming in cCore programming in c
Core programming in c
 
week-1x
week-1xweek-1x
week-1x
 
Common problems solving using c
Common problems solving using cCommon problems solving using c
Common problems solving using c
 
Loop control structure
Loop control structureLoop control structure
Loop control structure
 
SaraPIC
SaraPICSaraPIC
SaraPIC
 
Prim
PrimPrim
Prim
 
Basic c programs updated on 31.8.2020
Basic c programs updated on 31.8.2020Basic c programs updated on 31.8.2020
Basic c programs updated on 31.8.2020
 
The solution manual of c by robin
The solution manual of c by robinThe solution manual of c by robin
The solution manual of c by robin
 
C Programming
C ProgrammingC Programming
C Programming
 
All important c programby makhan kumbhkar
All important c programby makhan kumbhkarAll important c programby makhan kumbhkar
All important c programby makhan kumbhkar
 
week-10x
week-10xweek-10x
week-10x
 
week-11x
week-11xweek-11x
week-11x
 
Najmul
Najmul  Najmul
Najmul
 
C Language Programs
C Language Programs C Language Programs
C Language Programs
 
Bcsl 033 data and file structures lab s1-3
Bcsl 033 data and file structures lab s1-3Bcsl 033 data and file structures lab s1-3
Bcsl 033 data and file structures lab s1-3
 
Program flowchart
Program flowchartProgram flowchart
Program flowchart
 
Cs291 assignment solution
Cs291 assignment solutionCs291 assignment solution
Cs291 assignment solution
 
C program to add n numbers
C program to add n numbers C program to add n numbers
C program to add n numbers
 

Similar to Progr3

Srinivas Reddy Amedapu, CPDS, CP Lab, JNTU Hyderabad
Srinivas Reddy Amedapu, CPDS, CP Lab, JNTU HyderabadSrinivas Reddy Amedapu, CPDS, CP Lab, JNTU Hyderabad
Srinivas Reddy Amedapu, CPDS, CP Lab, JNTU HyderabadSrinivas Reddy Amedapu
 
Srinivas Reddy Amedapu C and Data Structures JNTUH Hyderabad
Srinivas Reddy Amedapu C and Data Structures JNTUH HyderabadSrinivas Reddy Amedapu C and Data Structures JNTUH Hyderabad
Srinivas Reddy Amedapu C and Data Structures JNTUH HyderabadSrinivas Reddy Amedapu
 
C programming Lab 2
C programming Lab 2C programming Lab 2
C programming Lab 2Zaibi Gondal
 
Computer programming subject notes. Quick easy notes for C Programming.Cheat ...
Computer programming subject notes. Quick easy notes for C Programming.Cheat ...Computer programming subject notes. Quick easy notes for C Programming.Cheat ...
Computer programming subject notes. Quick easy notes for C Programming.Cheat ...DR B.Surendiran .
 
Numerical analysis
Numerical analysisNumerical analysis
Numerical analysisVishal Singh
 
Best C Programming Solution
Best C Programming SolutionBest C Programming Solution
Best C Programming Solutionyogini sharma
 
In C Programming create a program that converts a number from decimal.docx
In C Programming create a program that converts a number from decimal.docxIn C Programming create a program that converts a number from decimal.docx
In C Programming create a program that converts a number from decimal.docxtristans3
 

Similar to Progr3 (20)

C lab
C labC lab
C lab
 
C programs
C programsC programs
C programs
 
C Programming Exam problems & Solution by sazzad hossain
C Programming Exam problems & Solution by sazzad hossainC Programming Exam problems & Solution by sazzad hossain
C Programming Exam problems & Solution by sazzad hossain
 
Srinivas Reddy Amedapu, CPDS, CP Lab, JNTU Hyderabad
Srinivas Reddy Amedapu, CPDS, CP Lab, JNTU HyderabadSrinivas Reddy Amedapu, CPDS, CP Lab, JNTU Hyderabad
Srinivas Reddy Amedapu, CPDS, CP Lab, JNTU Hyderabad
 
Srinivas Reddy Amedapu C and Data Structures JNTUH Hyderabad
Srinivas Reddy Amedapu C and Data Structures JNTUH HyderabadSrinivas Reddy Amedapu C and Data Structures JNTUH Hyderabad
Srinivas Reddy Amedapu C and Data Structures JNTUH Hyderabad
 
C file
C fileC file
C file
 
C programming Lab 2
C programming Lab 2C programming Lab 2
C programming Lab 2
 
C faq pdf
C faq pdfC faq pdf
C faq pdf
 
Progr2
Progr2Progr2
Progr2
 
Computer programming subject notes. Quick easy notes for C Programming.Cheat ...
Computer programming subject notes. Quick easy notes for C Programming.Cheat ...Computer programming subject notes. Quick easy notes for C Programming.Cheat ...
Computer programming subject notes. Quick easy notes for C Programming.Cheat ...
 
C-programs
C-programsC-programs
C-programs
 
Numerical analysis
Numerical analysisNumerical analysis
Numerical analysis
 
C important questions
C important questionsC important questions
C important questions
 
C lab programs
C lab programsC lab programs
C lab programs
 
C lab programs
C lab programsC lab programs
C lab programs
 
C lab excellent
C lab excellentC lab excellent
C lab excellent
 
C and Data Structures Lab Solutions
C and Data Structures Lab SolutionsC and Data Structures Lab Solutions
C and Data Structures Lab Solutions
 
C and Data Structures
C and Data Structures C and Data Structures
C and Data Structures
 
Best C Programming Solution
Best C Programming SolutionBest C Programming Solution
Best C Programming Solution
 
In C Programming create a program that converts a number from decimal.docx
In C Programming create a program that converts a number from decimal.docxIn C Programming create a program that converts a number from decimal.docx
In C Programming create a program that converts a number from decimal.docx
 

More from SANTOSH RATH

Lesson plan proforma database management system
Lesson plan proforma database management systemLesson plan proforma database management system
Lesson plan proforma database management systemSANTOSH RATH
 
Lesson plan proforma progrmming in c
Lesson plan proforma progrmming in cLesson plan proforma progrmming in c
Lesson plan proforma progrmming in cSANTOSH RATH
 
Expected questions tc
Expected questions tcExpected questions tc
Expected questions tcSANTOSH RATH
 
Expected questions tc
Expected questions tcExpected questions tc
Expected questions tcSANTOSH RATH
 
Module wise format oops questions
Module wise format oops questionsModule wise format oops questions
Module wise format oops questionsSANTOSH RATH
 
( Becs 2208 ) database management system
( Becs 2208 ) database management system( Becs 2208 ) database management system
( Becs 2208 ) database management systemSANTOSH RATH
 
Expected Questions TC
Expected Questions TCExpected Questions TC
Expected Questions TCSANTOSH RATH
 
Expected questions tc
Expected questions tcExpected questions tc
Expected questions tcSANTOSH RATH
 
Expected questions for dbms
Expected questions for dbmsExpected questions for dbms
Expected questions for dbmsSANTOSH RATH
 
Expected questions for dbms
Expected questions for dbmsExpected questions for dbms
Expected questions for dbmsSANTOSH RATH
 
Oops model question
Oops model questionOops model question
Oops model questionSANTOSH RATH
 
System programming note
System programming noteSystem programming note
System programming noteSANTOSH RATH
 
Operating system notes
Operating system notesOperating system notes
Operating system notesSANTOSH RATH
 

More from SANTOSH RATH (20)

Lesson plan proforma database management system
Lesson plan proforma database management systemLesson plan proforma database management system
Lesson plan proforma database management system
 
Lesson plan proforma progrmming in c
Lesson plan proforma progrmming in cLesson plan proforma progrmming in c
Lesson plan proforma progrmming in c
 
Expected questions tc
Expected questions tcExpected questions tc
Expected questions tc
 
Expected questions tc
Expected questions tcExpected questions tc
Expected questions tc
 
Module wise format oops questions
Module wise format oops questionsModule wise format oops questions
Module wise format oops questions
 
2011dbms
2011dbms2011dbms
2011dbms
 
2006dbms
2006dbms2006dbms
2006dbms
 
( Becs 2208 ) database management system
( Becs 2208 ) database management system( Becs 2208 ) database management system
( Becs 2208 ) database management system
 
Rdbms2010
Rdbms2010Rdbms2010
Rdbms2010
 
Expected Questions TC
Expected Questions TCExpected Questions TC
Expected Questions TC
 
Expected questions tc
Expected questions tcExpected questions tc
Expected questions tc
 
Expected questions for dbms
Expected questions for dbmsExpected questions for dbms
Expected questions for dbms
 
Expected questions for dbms
Expected questions for dbmsExpected questions for dbms
Expected questions for dbms
 
Oops model question
Oops model questionOops model question
Oops model question
 
System programming note
System programming noteSystem programming note
System programming note
 
Operating system notes
Operating system notesOperating system notes
Operating system notes
 
Os notes
Os notesOs notes
Os notes
 
OS ASSIGNMENT 2
OS ASSIGNMENT 2OS ASSIGNMENT 2
OS ASSIGNMENT 2
 
OS ASSIGNMENT-1
OS ASSIGNMENT-1OS ASSIGNMENT-1
OS ASSIGNMENT-1
 
OS ASSIGNMENT 3
OS ASSIGNMENT 3OS ASSIGNMENT 3
OS ASSIGNMENT 3
 

Recently uploaded

Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Celine George
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphThiyagu K
 
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...fonyou31
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfchloefrazer622
 
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...Sapna Thakur
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDThiyagu K
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Disha Kariya
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactPECB
 
General AI for Medical Educators April 2024
General AI for Medical Educators April 2024General AI for Medical Educators April 2024
General AI for Medical Educators April 2024Janet Corral
 
social pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajansocial pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajanpragatimahajan3
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfagholdier
 

Recently uploaded (20)

Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdf
 
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
General AI for Medical Educators April 2024
General AI for Medical Educators April 2024General AI for Medical Educators April 2024
General AI for Medical Educators April 2024
 
social pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajansocial pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajan
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 

Progr3

  • 1. 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. 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 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. 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. 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. 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. 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. 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