Practical - 4 for C
looping statement
#include<stdio.h>
#include<conio.h>
void main(){
int sum = 0,num;
clrscr();
for(int i = 1;i<=5;i++){
printf("Enter a number");
scanf("%d",&num);
sum += num;
}
printf("Avg is %d",sum/5);
getch();
}
#include<stdio.h>
#include<conio.h>
void main(){
int sum = 0,num = 0;
printf("Enter a number");
scanf("%d",&num);
while(num!=0){
sum += num%10;
num /= 10;
}
printf("sum of digits is %d",sum);
getch();
}
3.
#include<stdio.h>
#include<conio.h>
void main(){
int num=0,rnum=0,cnum,i=1;
clrscr();
printf("Enter a number");
scanf("%d",&num);
cnum = num;
while(cnum!=0){
cnum /= 10;
i *= 10;
}
i /= 10;
while(num!=0){
rnum += (num%10)*i;
num /= 10;
i /= 10;
}
printf("reversed number is %d",rnum);
getch();
}
4.
#include<stdio.h>
#include<conio.h>
void main(){
int num=0,arnum=0,m1,cpnum,cnum,i=0,mint=0;
clrscr();
printf("Enter a number");
scanf("%d",&num);
cnum = cpnum = num;
while(cnum!=0){
cnum /= 10;
i++;
}
i--;
while(num!=0){
mint =m1 = num%10;
for(int c=1;c<=i;c++){
mint *= m1;
}
arnum += mint;
num /= 10;
}
arnum == cpnum?printf("It is armstrong number"):printf("It is not an armstrongnumber");
getch();
}
5.
#include<stdio.h>
#include<conio.h>
void main(){
int c=1;
int o=5;
int m =5;
clrscr();
//design 1
for(int i=1;i<=5;i++){
for(int v =1;v<=i;v++){
printf("*");
}
printf("\n");
}
printf("\n");
printf("\n");
//desingn 2
while(c<=5){
for(int i = 1;i<=o;i++){
printf(" ");
}
for(int v = 1;v<=c;v++){
printf("%d",o);
}
c++;
o--;
printf("\n");
}
printf("\n");
printf("\n");
// design 3
for(int s = 1;s<=5;s++)
{
for(int f=1;f<s;f++){
printf(" ");
}
for(int g=1;g<=m;g++){
printf("$");
}
printf("\n");
m--;
}
getch();
}
#include<stdio.h>
#include<conio.h>
void main(){
int num,a=1;
clrscr();
printf("Enter a number");
scanf("%d",&num);
do{
printf("%d * %d = %d",num,a,num*a);
a++;
printf("\n");
}while(a<=10);
getch();
}
#include<stdio.h>
#include<conio.h>
void main(){
int num=0,count=0,avg=0;
clrscr();
do{
printf("Enter a number");
scanf("%d",&num);
if(num<0){
break;
}
avg += num ;
count++;
}while(num>0);
printf("average is %d",avg/count);
getch();
}
#include<stdio.h>
#include<conio.h>
void main(){
int n=0,count=0,sum=0;
clrscr();
printf("Enter a max-number");
scanf("%d",&n);
printf("sum of ");
for(int i =1;i<=n;i++){
printf("%d",i);
printf("+");
sum += i;
}
printf("\b = %d",sum);
getch();
}