SlideShare a Scribd company logo
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

PPTX
C Programming Example
PDF
C Programming Example
DOC
C program to check leap year
DOCX
Practical write a c program to reverse a given number
DOCX
Practical write a c program to reverse a given number
DOC
Shan
DOCX
C programs
DOCX
B.Com 1year Lab programs
C Programming Example
C Programming Example
C program to check leap year
Practical write a c program to reverse a given number
Practical write a c program to reverse a given number
Shan
C programs
B.Com 1year Lab programs

What's hot (20)

PDF
88 c-programs
DOCX
program in c
DOCX
Core programming in c
PDF
Common problems solving using c
PPTX
Loop control structure
DOCX
SaraPIC
PDF
DOC
Basic c programs updated on 31.8.2020
PDF
The solution manual of c by robin
DOCX
C Programming
PPT
All important c programby makhan kumbhkar
PPTX
Najmul
DOCX
C Language Programs
PDF
Bcsl 033 data and file structures lab s1-3
DOCX
Program flowchart
DOCX
Cs291 assignment solution
DOC
C program to add n numbers
88 c-programs
program in c
Core programming in c
Common problems solving using c
Loop control structure
SaraPIC
Basic c programs updated on 31.8.2020
The solution manual of c by robin
C Programming
All important c programby makhan kumbhkar
Najmul
C Language Programs
Bcsl 033 data and file structures lab s1-3
Program flowchart
Cs291 assignment solution
C program to add n numbers
Ad

Similar to Progr3 (20)

PDF
Computer programming subject notes. Quick easy notes for C Programming.Cheat ...
PDF
C faq pdf
PDF
Subject:Programming in C - Lab Programmes
PDF
C programs
PDF
C lab programs
PDF
C lab programs
PDF
Programming Practice Questions_C
DOC
C important questions
PPTX
SIMPLE C PROGRAMS - SARASWATHI RAMALINGAM
DOC
Useful c programs
DOC
C-programs
DOCX
Write a program to check a given number is prime or not
PDF
PCA-2 Programming and Solving 2nd Sem.pdf
DOCX
PCA-2 Programming and Solving 2nd Sem.docx
PDF
Simple C programs
PDF
Programming in C Lab
DOCX
C lab manaual
Computer programming subject notes. Quick easy notes for C Programming.Cheat ...
C faq pdf
Subject:Programming in C - Lab Programmes
C programs
C lab programs
C lab programs
Programming Practice Questions_C
C important questions
SIMPLE C PROGRAMS - SARASWATHI RAMALINGAM
Useful c programs
C-programs
Write a program to check a given number is prime or not
PCA-2 Programming and Solving 2nd Sem.pdf
PCA-2 Programming and Solving 2nd Sem.docx
Simple C programs
Programming in C Lab
C lab manaual
Ad

More from SANTOSH RATH (20)

DOCX
Lesson plan proforma database management system
DOCX
Lesson plan proforma progrmming in c
DOCX
Expected questions tc
PDF
Expected questions tc
PDF
Module wise format oops questions
PDF
2011dbms
PDF
2006dbms
PDF
( Becs 2208 ) database management system
PDF
Rdbms2010
DOCX
Expected Questions TC
PDF
Expected questions tc
DOCX
Expected questions for dbms
PDF
Expected questions for dbms
PDF
Oops model question
PDF
System programming note
DOCX
Operating system notes
PDF
Os notes
PDF
OS ASSIGNMENT 2
PDF
OS ASSIGNMENT-1
PDF
OS ASSIGNMENT 3
Lesson plan proforma database management system
Lesson plan proforma progrmming in c
Expected questions tc
Expected questions tc
Module wise format oops questions
2011dbms
2006dbms
( Becs 2208 ) database management system
Rdbms2010
Expected Questions TC
Expected questions tc
Expected questions for dbms
Expected questions for dbms
Oops model question
System programming note
Operating system notes
Os notes
OS ASSIGNMENT 2
OS ASSIGNMENT-1
OS ASSIGNMENT 3

Recently uploaded (20)

PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PPTX
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
PDF
Sports Quiz easy sports quiz sports quiz
PPTX
Cell Structure & Organelles in detailed.
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PDF
Insiders guide to clinical Medicine.pdf
PDF
Basic Mud Logging Guide for educational purpose
PDF
VCE English Exam - Section C Student Revision Booklet
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PPTX
GDM (1) (1).pptx small presentation for students
PDF
Classroom Observation Tools for Teachers
PPTX
Institutional Correction lecture only . . .
PPTX
Lesson notes of climatology university.
PPTX
master seminar digital applications in india
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
STATICS OF THE RIGID BODIES Hibbelers.pdf
Microbial diseases, their pathogenesis and prophylaxis
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
Sports Quiz easy sports quiz sports quiz
Cell Structure & Organelles in detailed.
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
2.FourierTransform-ShortQuestionswithAnswers.pdf
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
Insiders guide to clinical Medicine.pdf
Basic Mud Logging Guide for educational purpose
VCE English Exam - Section C Student Revision Booklet
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
GDM (1) (1).pptx small presentation for students
Classroom Observation Tools for Teachers
Institutional Correction lecture only . . .
Lesson notes of climatology university.
master seminar digital applications in india
O5-L3 Freight Transport Ops (International) V1.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