Practical 5 Without Watermark PDF
Practical 5 Without Watermark PDF
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;
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;
int n;
5.4 Write a program to check that whether the entered number is prime or
not. #include<stdio.h>
Output:
int main(){
int n;
if(a==0){
printf("number is prime");
}else{
}}
int main(){
Output:
int n;
}
if (a == 0){
printf("%d \n",i);
}}}
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);
} } }
int main(){
int n;
c=a+b;
a=b;
b=c;
printf("%d \n",c);
}}
Output:
int n;
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);
#include<stdio.h> Output:
int main(){
int n,b;
}else{
return 0;
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("\n");
2. #include<stdio.h> Output:
int main(){
int n;
3. #include<stdio.h> Output:
int main(){
int n;
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;
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;
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("\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;
}
}
}