Writing "W" letter with c - c

Receiving the height information from the user and printing the letter W with *** (3 stars).This is my homework but i need to improve this code.
I did something like that with google but it always showing me same height. I want someone to enter a number and change the height. (i do not know why the site not accepting #include <stdio.h> as a code. Sorry for that.)
#include <stdio.h>
int main()
{
int a,b,c,d,e;
for(a=0;a<=11;a++)
{
for(b=0;b<=2;b++){
printf("*");
}
for(c=10;c>=a;c--)
{
printf(" ");
}
for(b=0;b<=2;b++){
printf("*");
}
for(d=1;d<=a;d++)
{
printf(" ");
}
for(b=0;b<=2;b++){
printf("*");
}
for(e=10;e>=a;e--)
{
printf(" ");
}
for(b=0;b<=2;b++){
printf("*");
}
printf("\n");
}
}

#include <stdio.h>
int main()
{
int a,b,c,d,e,height;
printf("Enter a height : "); // print "Enter a height :"
scanf("%d",&height); // ask to user how much height
height--;
for(a=0;a<=height;a++) //a<=11
{
for(b=0;b<=2;b++){
printf("*");
}
for(c=0;c<height-a;c++)
{
printf(" ");
}
for(b=0;b<=2;b++){
printf("*");
}
for(d=1;d<=a;d++)
{
printf(" ");
}
for(b=0;b<=2;b++){
printf("*");
}
for(e=0;e<height-a;e++)
{
printf(" ");
}
for(b=0;b<=2;b++){
printf("*");
}
printf("\n");
}
}

Related

Problem in obtaining output, if statement maybe?

#include <stdio.h>
int main()
{
int rev_num=0,x=0,n,a;
printf("Enter a number");
scanf("%d",&n);
for(int i=n+1;i<i+1;i++)
{
a=i;
while(a>0)
{
rev_num=rev_num*10+a%10;
a/=10;
}
for(int j=2;j<i;j++)
{
x=0;
if(i%j==0)
{
x=1;
break;
}
}
if(x==0 && i==rev_num)
{
printf("%d is the next prime palindrome",i);
break;
}
}
return 0;
}
We have to find next prime palindrome of a number, but the code is not giving any o/p, nor any error. It just keeps asking for input indefintely.Why????

Star Pattern in C- 2nd half is not printing

I've been trying to print star pattern but it's not working after
"if(i>4)" as you can see the second image there's a gap in between which implies that the spaces are getting printing or the new line but the star pattern is not getting printed
enter code here
//the first half
int main() {
int i,j,k,m;
for (i=1;i<=4;i++) {
for (j=1;j<=4-i;j++) {
printf(" ");
}
for(k=1;k<=i;k++)
{
printf("*");
}
printf("\n"); // to print the first half
}
//the second half
if(i>4){
for(j=1;j<=i-2;j++)
{
for (k=1;k<=j;k++) {
printf(" ");
}
for(m=1;m<=4-i;m++)
{
printf("*");
}
printf("\n");
}
}
return 0;
}
Try this
#include<stdio.h>
int main()
{
int n;
scanf("%d",&n);
n = n/2+1;
for(int i=1;i<=n;i++)
{
for(int j=n-i;j>=1;j--)
{
printf(" ");
}
for(int k=1;k<=i;k++)
{
printf("*");
}
printf("\n");
}
for(int i=1;i<=n;i++)
{
for(int k=1;k<=i;k++)
{
printf(" ");
}
for(int j=n-i;j>=1;j--)
{
printf("*");
}
printf("\n");
}
return 0;
}
Output

drawing a rectangle for user-defined dimensions using for lops, using extended ASCII characters

I did some part of it but can't seem to get the whole rectangle. Could someone point out what I'm doing wrong?
Here is my code:
printf("Enter the length and width of the rectangle : ");
scanf("%d%d",&length,&width);
printf("\n%c", 218);
for(i=1;i<=length;i++)
{
printf("%c",196);
}
printf("%c",191);
for(j=1;j<=width;j++)
{
printf("\n");
printf("%c",179);
}
printf("\n");
printf("%c", 192);
for(i=1;i<=length;i++)
{
printf("%c",196);
}
printf("%c", 217);
return 0;
My output
In your middle loop, you only print a single vertical bar before going to the next line.
You need to print the bar once, then add another loop to print the spaces, then print one more bar:
for(j=1;j<=width;j++)
{
printf("\n");
printf("%c",179);
for (i=1;i<=length;i++) {
printf(" ");
}
printf("%c",179);
}
You are only printing the first column and not the second one.
An approach would be to loop over the whole rectangle and check if you are at an edge or not.
#include <stdio.h>
int main()
{
int length, width, i, j;
printf("Enter the length and width of the rectangle : ");
scanf("%d%d",&length, &width);
for(i=0;i<width;i++)
{
for(j=0;j<length;j++)
{
if( j==0 || j==length-1 || i==0 || i==width-1 )
{
printf("%c", '*');
}
else
{
printf(" ");
}
}
printf("\n");
}
return 0;
}
#include <stdio.h>
#include <stdlib.h>
int main()
{
int length, width,i,j;
printf("Enter the length and width of the rectangle : ");
scanf("%d%d",&length,&width);
printf("\n%c", 218);
for(i=1;i<=length;i++)
{
printf("%c",196);
}
printf("%c",191);
for(j=1;j<=width;j++)
{
printf("\n");
printf("%c",179);
for (i=1;i<=length;i++) {
printf(" ");
}
printf("%c",179);
}
printf("\n");
printf("%c", 192);
for(i=1;i<=length;i++)
{
printf("%c",196);
}
printf("%c", 217);
return 0;
}

calling a function on another fuction in the same file

i have an error massage but i don't undestand what is the problem. i'm deperately searching for the problem.
i tried to create a libary function, and include one of the function on another function in the same file. Is this posibble ? if so please expalain.
#include "head.h"
void fungsiCetak()
{
int i,n;
system("cls");
system("color 1f");
FILE*nip,*nama,*golongan,*anak,*kk,*birth;
nip=fopen("nip.txt","a");
nama=fopen("nama.txt","a");
anak=fopen("jum_anak.txt","a");
kk=fopen("jum_kk.txt","a");
birth=fopen("birth.txt","a");
golongan=fopen("golongan.txt","a");
//////////////////////////////////////////////////////////////////
fflush(stdin);
printf("Berapa data yang ingin di inputkan ? ");
scanf("%d",&n);
i=1;
do{
fflush(stdin);
printf(" Masukan NIP : ");
gets(pegawai.nip);
fprintf(nip,"%s\n",pegawai.nip);
printf("Masukan nama Pegawai : ");
gets(pegawai.nama);
fprintf(nama,"%s\n",pegawai.nama);
printf("Masukan golongan : ");
scanf("%d",&pegawai.gol);
fprintf(golongan,"%d\n",pegawai.gol);
printf("Jumlah anak : ");
scanf("%d",&pegawai.jum_anak);
fprintf(anak,"%d\n",pegawai.jum_anak);
printf("Jumlah anggota keluarga : ");
scanf("%d",&pegawai.jum_kk);
fprintf(kk,"%d\n",pegawai.jum_kk);
printf("Tanggal lahir (dd-mm-yyyy) : ");
scanf("%d-%d-%d",&pegawai.birth.hari,
&pegawai.birth.bulan,
&pegawai.birth.tahun);
fprintf(birth,"%d-%d-%d\n",pegawai.birth.hari,
pegawai.birth.bulan,
pegawai.birth.tahun);
printf("\n");
i++;
} while(i<=n);
fclose(nip);
fclose(nama);
fclose(golongan);
fclose(anak);
fclose(kk);
fclose(birth);
}
void fungsiBaca()
{
system("cls");
int count=0;
int nip2,nip1[20],anak1[20],kk1[20];
int gol[20];
char birth1[30][30];
char bufer[100],buffer1[100];
char nama1[30][30];
struct pegawai;
FILE*nip,*nama,*golongan,*anak,*kk,*birth;
nama=fopen("nama.txt","r");
i=0;
while(fgets(bufer,sizeof bufer,nama)!=NULL)
{
strcpy(nama1[i],bufer);
i++;
}
a=0;
golongan=fopen("golongan.txt","r");
while(a<=i)
{
fscanf(golongan,"%d",&gol[a]);
a++;
}
nip=fopen("nip.txt","r");
a=0;
while(a<i)
{
fscanf(nip,"%d",&nip1[a]);
a++;
}
anak=fopen("jum_anak.txt","r");
a=0;
while(a<i)
{
fscanf(anak,"%d",&anak1[a]);
a++;
}
kk=fopen("jum_kk.txt","r");
a=0;
while(a<i)
{
fscanf(kk,"%d",&kk1[a]);
a++;
}
birth=fopen("birth.txt","r");
a=0;
while(fgets(buffer1,sizeof buffer1,birth)!=NULL)
{
strcpy(birth1[a],buffer1);
a++;
}
search :
fflush(stdin);
printf("----------------------------------------\n");
printf("masukan NIP : ");
scanf("%d",&nip2);
printf("----------------------------------------\n");
for(a=0; a<=i; a++)
{
if(nip1[a]==nip2){
printf("Nama pegawai : %s",nama1[a]);
printf("Golongan : %d\n",gol[a]);
printf("Jumlah anak : %d\n",anak1[a]);
printf("Jumlah KK : %d\n",kk1[a]);
printf("Tanggal Lahir : %s\n",birth1[a]);
count=1;
}
}
if (count==0)
{
printf("Maaf data tidak ditemukan!\n");
}
fflush(stdin);
printf("Ingin mencari data kembali ? [y/n]");
scanf("%c",&kembali);
if (kembali=='y')
{
system("cls");
goto search;
}
fclose(nip);
fclose(nama);
fclose(golongan);
fclose(kk);
fclose(anak);
fclose(birth);
}
void fungsiGaji ()
{
int gol,absen;
float tunj_makan,tunj_anak,tunj_kes,total,gajih_pokok;
fungsiBaca();
//////////////////////////////////////////////////Kondisi gajih pokok
if (gol[a]==1)
{
gajih_pokok=GOl1;
} else if (gol[a]==2)
{
gajih_pokok=GOL2;
} else if (gol[a]==3)
{
gajih_pokok=GOl3;
} else
{
gajih_pokok=GOL4;
}
printf("Gajih pokok : %.2f",gajih_pokok);
/////////////////////////////////////////////// kondisi tunjangan
}
the error message is
I use CodeBlocks to create the program
The problem is here:
int gol,absen; <--- gol is integer
...
if (gol[a]==1) <---- gol[a] ?

How can I count the average separately for all rows

It's a basic program, but I need to find out how to calculate the average for each row separately. Should I use more counts or should I use another loop?
int main()
{
int r;
int Count=0;
double sum=0;
double Average=0;
double c,value;
for (r=1; r<11; r++)
{
for(c=1; c<5; c++)
{
value=(rand()%10000)/100.0;
if (value>=67.0)
{
Count++;
printf("%5.2f ",value);
sumTotal=sumRow/sumCol;
sum += value;
}
else
{
printf(" ");
}
}
Average=sum/Count;
if(Average == 0){
printf("| ");
}
else{
printf("| %6.2f ",Average);
}
printf("\n");
}
return 0;
}
make the counters in outer loop
int main()
{
int r;
double c,value;
for (r=1; r<11; r++)
{
int Count=0;
double sum=0;
double Average=0;
for(c=1; c<5; c++)
{
value=(rand()%10000)/100.0;
if (value>=67.0)
{
Count++;
printf("%5.2f ",value);
// sumTotal=sumRow/sumCol;
sum += value;
}
else
{
printf(" ");
}
}
Average=sum/Count;
if(Average == 0){
printf("| ");
}
else{
printf("| %6.2f ",Average);
}
printf("\n");
}
return 0;
}
I commented out broken code.

Resources