0% found this document useful (0 votes)
40 views13 pages

Practical 5 Without Watermark PDF

The document contains examples of programs to demonstrate looping statements and other programming concepts. It includes programs to calculate digit sum and reverse of a number, print series, check prime numbers, Armstrong numbers, Fibonacci series and other mathematical series. Programs for simple calculator, palindrome, factorial, patterns and tables are also given.

Uploaded by

vibhuim1507
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views13 pages

Practical 5 Without Watermark PDF

The document contains examples of programs to demonstrate looping statements and other programming concepts. It includes programs to calculate digit sum and reverse of a number, print series, check prime numbers, Armstrong numbers, Fibonacci series and other mathematical series. Programs for simple calculator, palindrome, factorial, patterns and tables are also given.

Uploaded by

vibhuim1507
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

2ES104: Programming for Problem Solving

Practical-5
AIM : To study and practice the program for understanding looping statements

5.1 Write a program to calculate sum of all digits of a number like this (Hint:
if(no/10==0) inside while loop): Enter any number: 456 6+5+4=11
#include<stdio.h>
Output:
int main(){

int n;

printf(“enter any number \n”);


scanf("%d", &n);
int sum, reminder;
sum = 0;
for (int i = 0; n != 0; n = n / 10){
reminder = n % 10;
sum = sum + reminder;}
printf("%d", sum);
return 0;
}

5.2 Write a program to reverse the order of digits of entered number using:
(a) printf() function inside of the loop (b) printf() function outside of the loop.

#include <stdio.h>

Output:
int main(){

int n;

printf(“enter any number \n”);


scanf("%d", &n);
int reminder, sum;
sum = 0;
for (int i = 0; n != 0; n = n / 10){
reminder = n % 10;
sum = sum * 10 + reminder;
printf("%d \n", sum);
}

printf("%d \n", sum);

5.3 Write a program for following Series: 1 + 3 + 5 + … + n.


#include<stdio.h>
Output:
int main(){

int n;

printf("enter the number \n");


scanf("%d",&n);
for(int i=1;i<=n;i+=2){
printf("%d \n",i);
}

5.4 Write a program to check that whether the entered number is prime or
not. #include<stdio.h>
Output:
int main(){

int n;

printf("enter the number \n");


scanf("%d",&n);
int a=0;
for(int i=2;i<=n/2;i++){
if(n%i==0){
a=1;
}

if(a==0){

printf("number is prime");
}else{

printf("number is not prime");

}}

5.5 Write a program to print first N prime numbers.


#include <stdio.h>

int main(){
Output:
int n;

printf("enter the number \n");


scanf("%d", &n);
for (int i = 2; i <= n; i++){
int a = 0;
for (int j = 2; j <= i / 2; j++){
if (i % j == 0){
a = 1;
}

}
if (a == 0){
printf("%d \n",i);
}}}

5.6 Write a program to all print Armstrong between 0 to 999.


#include<stdio.h> Output:

int main(){

int z,n;
int b,t;
int reminder,sum,a;
int length;
printf("enter the number \n");
scanf("%d",&z);
for(int y=0;y<=z;y++){
n=y;
b=n;
length=0;
t=n;
for(int i=0;t!=0;t=t/10){

length=length+1;

sum=0;
for(int j=0;n!=0;n=n/10){
reminder=n%10;
a=1;

for(int i=0;i<length;i++){
a=a*reminder;
}

sum=sum+a;

if(sum==b &&sum!=0){
printf("%d is armsrong number \n",sum);
} } }

5.7 Write a Program to print Fibonacci series


#include<stdio.h> Output:

int main(){

int n;

printf("enter the number \n");


scanf("%d ",&n);
int a=0;
int b=1;
int c;
for(int i=2;i<=n;i++){
if(i==2){
printf("%d \n",a);
}

c=a+b;
a=b;

b=c;

printf("%d \n",c);
}}

5.8 Write a program to print 1+1/2+1/3+1/4+………+1/N series.


#include<stdio.h>
int main(){

Output:
int n;

printf("enter the number \n");


scanf("%d",&n);
for(int i=1;i<=n;i++){
if(i==1){
printf("1 + ");
}else if(i>1&&i<n){
printf("1/%d + ",i);
}else{

printf("1/%d",n);

} } }

5.9 Write a simple calculator program performing all five basic function
shown below. Make sure that after performing each operation your program
should prompt to keep continue after each operation (do not use GOTO
statement) like shown below:
#include<stdio.h>
int main(){
char x;
int a,b;
int c;
do{
printf("type 1 for sum \ntype 2 for substraction \ntype 3 for multiplication \ntype 4 for divison \n");
scanf("%d",&c);
printf("enter two numbers \n");

scanf("%d %d",&a,&b);

if(c==1){
Output:
printf("sum is %d \n",a+b);

}else if(c==2){

printf("substraction is %d \n",a-b);

}else if(c==3){

printf("multiplication is %d\n",a*b);

}else if(c==4){

printf("division is %d \n",a/b);

printf("if you want to do it again press y \n");


scanf(" %c",&x);
M}while (x=='y');
return 0; }

5.10 Write a program to check that whether entered number is palindrome


or not?

#include<stdio.h> Output:

int main(){

int n,b;

printf("enter any number \n");


scanf("%d",&n);
b=n;
int sum,reminder;
sum=0;
for(n;n!=0;n=n/10){
reminder=n%10;
sum=sum*10+reminder;
}
if(sum==b){

printf("entered number is palindrome");

}else{

printf("entered number is not palindrome");

return 0;

5.11 Write a program to find out factorial (e.g. 4! = 4*3*2*1 = 24 ) of any


integer number using:
(1) for loop

(2) while loop

(3) do…while loop

1. #include<stdio.h> int main(){


Output:
int n,sum,b;
sum=1;
printf("enter the number\n");
scanf("%d",&n);
b=n;
for(int i=1;i<=n;i++){
sum=sum*i;
}
printf("%d!=%d",b,sum);
}

2. #include<stdio.h>
int main(){ Output:
int n,sum,b;
sum=1;
printf("enter the number\n");
scanf("%d",&n);
b=n;
int i=1;
while(i<=n){
sum=sum*i;
i++;

printf("%d!=%d",b,sum);

3. #include<stdio.h> Output:
int main(){
int n,sum,b;
sum=1;
printf("enter the number\n");
scanf("%d",&n);
b=n;
int i=1;
do{
sum=sum*i;
i++;

}while(i<=n);
printf("%d!=%d",b,sum);
}

5.12

1. #include<stdio.h>
int main(){ Output:

int n;

printf("enter the number \n");


scanf("%d",&n);
for(int i=0;i<n;i++){
for(int j=0;j<=i;j++){
printf("*");
}

printf("\n");

2. #include<stdio.h> Output:
int main(){

int n;

printf("enter the number \n");


scanf("%d",&n);
for(int i=0;i<n;i++){
for(int j=0;j<=i;j++){
printf("%d",i+1);
}
printf("\n");
}
}

3. #include<stdio.h> Output:
int main(){

int n;

printf("enter the number \n");


scanf("%d",&n);
int a=1;
for(int i=0;i<n;i++){
if((i+2)%2==0){
a=1;
}else{a=0;}

for(int j=0;j<=i;j++){

printf("%d",a);

if(a==1){

a=0;

}else{
a=1;
}

printf("\n");

}}
3. #include<stdio.h> Output
int main(){ :
int n;

printf("enter the number \n");


scanf("%d",&n);
for(int i=n;i>0;i--){
for(int j=i;j>0;j--){
printf("%d",j);
}
printf("\n");
}
}
5. #include<stdio.h> Output:
int main(){

int n,g;
g=1;
printf("enter the number \n");
scanf("%d",&n);
for(int i=0;i<n;i++){
for(int j=0;j<=i;j++){
printf("%d ",g);
g++;
}

printf("\n");

Output:

6. #include<stdio.h>

int n,g;

g=1;

printf("enter the number \n");


scanf("%d",&n);
for(int i=0;i<n;i++){
for(int k=n;i<k;k--){
printf(" ");
}
for(int j=0;j<=i;j++){
printf("%d",g);
g++;
}
for(int m=0;m<i;m++){
--g;
printf("%d",g-1);
}
printf("\n");
}
}

7. #include<stdio.h>
int main(){

int n;
int m;
printf("enter the number \n"); Output:
scanf("%d",&n);

m=n;

for(int i=n;i>0;i--){

for(int k=i;k<n;k++){

printf(" ");

for(int j=i;j>0;j--){
printf("*");
}

for(int p=m;p>1;p--){
printf("*");
}
m--;
printf("\n");
}

8. #include<stdio.h>
Output:
int main(){

int n;

printf("enter the number \n");


scanf("%d",&n);
for(int i=0;i<n;i++){
int a=1;
for(int j=n;j>i;j--){
printf("%d",a);
a++;
}

printf("\n");
}}

5.13 Write a program to print multiplicative table like follow: (Hint: use
“continue;” statement in your loop to skip particular iteration. Use “break;”
to limit only those multiplicative table up to which you want)
#include<stdio.h>
void main(){
int i,j,k,x,y,z;
printf("Enter the skip point number: ");
scanf("%d", &z);
printf("How many total no. of tables you want? ");
scanf("%d", &k);

for(i=1;i<=10;i++){
for(j=1;j<=10;j++){
if(j==z){
continue;
}
y = i * j;
printf("%d * %d = %d \n", i, j, y);
}
printf("\n");
if(i==k){
break;
}
}
}

You might also like