I am trying to print out a sequence of numbers and * that form a rectangle depending on the number provided by the user. I managed to print a rectangle with * but I do not know how to incorporate numbers to print out something like:
1*****
12****
123***
1234**
12345*
123456
This is what I have:
#include <stdio.h>
int main{
int i, j;
int num;
printf("Enter a number from 1-9: ");
scanf("%d", &num);
for(i=1; i<=num; i++)
{
for(j=1; j<=num; j++)
{
printf("*");
}
printf("\n");
}
return 0;
}
Hope this helps!
#include <stdio.h>
int main(){
int i, j;
int num;
printf("Enter a number from 1-9: ");
scanf("%d", &num);
for(i=1; i<=num; i++)
{
for(j=1; j<=i; j++)
{
printf("%d",j);
}
for(;j<=num;j++)
{
printf("*");
}
printf("\n");
}
return 0;
}
Also,Instead for using 2 loops inside of Master loop,You can Use conditional operators to manipulate output!
Cheers! :)
Related
I have been practising with arrays for a bit and I have encountered a problem I can't seem to find the answer for. I am trying to display the numbers the user enters, however they are not turning out as I expected. It should be in a form of a column.
#include <stdio.h>
int main (void)
{
double A[5], B[5];
int i;
for (i=0; i<=4; i++)
{
printf("Enter 5 numbers for column A: ");
scanf("%lf", &A[i]);
}
for (i=0; i<=4; i++)
{
printf("Enter 5 numbers for column B: ");
scanf("%lf", &B[i]);
}
printf("A = (%f) B = (%f) \n", A[i], B[i]);
return 0;
}
The printf statement seems to be correct however numbers are not showing in the output.
You should ask yourself, what is the value of i, when printing the final output.
You should also ask yourself, what is in array A and B at index i.
Given these are understood, we can display the content of an array in the same fashion as it is filled.
#include <stdio.h>
int main (void)
{
double A[5], B[5];
int i;
for (i=0; i<=4; i++)
{
printf("Enter 5 numbers for column A: ");
scanf("%lf", &A[i]);
}
for (i=0; i<=4; i++)
{
printf("Enter 5 numbers for column B: ");
scanf("%lf", &B[i]);
}
for (i=0; i<=4; i++)
{
printf("A = (%f) B = (%f) \n", A[i], B[i]);
}
return 0;
}
As said by #Tsakiroglou Fotis, you forgot to add bracket after the main function and also you are not looping the final print statement to print all the elements. Try using editors that does take care of such mistakes. here is your corrected code
#include <stdio.h>
int main (void){
double A[5], B[5];
int i;
for(i=0; i<=4; i++)
{
printf("Enter 5 numbers for column A: ");
scanf("%lf", &A[i]);
}
for(i=0; i<=4; i++)
{
printf("Enter 5 numbers for column B: ");
scanf("%lf", &B[i]);
}
for(i=0; i<5; i++){
printf("A = (%f) B = (%f) \n", A[i], B[i]);
}
return 0;
}
I had an interview in which I was asked to write an hourglass star pattern program. For example, if the user specifies 5 then the pattern must be like:
*****
***
*
***
*****
If 7 then the pattern must be like:
*******
*****
***
*
***
*****
*******
My coding for the pattern is:
for(i=n; i>=1; i--)
{
for(j=i; j<n; j++)
{
printf(" ");
}
for(j=1; j<=(2*i-1); j++)
{
printf("*");
}
printf("\n");
}
for(i=1; i<=n; i++)
{
for(j=i; j<n; j++)
{
printf(" ");
}
for(j=1; j<=(2*i-1); j++)
{
printf("*");
}
printf("\n");
}
Please help me to solve this problem.
You can use only 1 loop for printing:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char* argv[])
{
int a=0;
scanf("%d",&a);
int i=0;
char* asterisk=malloc(sizeof(char)*a);
memset(asterisk,'*',a*sizeof(char));
while(i<a)
{
if(i<=a/2)
printf("%*.*s\n",a-i,a-2*i,asterisk);
else
printf("%*.*s\n",i+1,2*(i+1)-a,asterisk);
i++;
}
free(asterisk);
return 0;
}
This could help. It's not the best that there is and it could be made more efficient, but it should give you an idea.
#include<stdio.h>
void print(int space, int star)
{
while (space--) {
printf(" ");
}
while (star--) {
printf("*");
}
printf("\n");
}
void main()
{
int star,space,i,j;
printf("Enter number:");
scanf("%d",&star);
j=star/2;
i=star;space=0;
while (space<j || star>0) {
print(space,star);
space=space+1;star=star-2;
}
space=j-1;star=3;
while(space>0 || star<=i) {
print(space,star);
space=space-1;star=star+2;
}
}
Please comment if you have any doubts.
Note:The above code is just a sample. There are many (and far more efficient) ways to implement the pattern and it is encouraged to find a way to do so.
Edit:The code now prints only 1 star in the middle. Did not notice that. Apologies.
Guys I found the mistake, What i did in that code.
#include <stdio.h>
void main()
{
int i, j, n;
printf("Enter Odd value for n : ");
scanf("%d", &n);
for(i=n/2+1; i>1; i--)
{
for(j=i; j<n; j++)
{
printf(" ");
}
for(j=1; j<=(2*i-1); j++)
{
printf("*");
}
printf("\n");
}
for(i=1; i<=n/2+1; i++)
{
for(j=i; j<n; j++)
{
printf(" ");
}
for(j=1;j<=(2*i-1); j++)
{
printf("*");
}
printf("\n");
}
}
Thank You!!!!!!! Guys for your help.
I made a program for making a pascal triangle and for the input of numbers ( rows ) > 5 , there is an alignment problem i.e for ncr > 10. Help me out please.
I have included the images for output of the program.
Output Image
#include<stdio.h>
int factorial(int number)
{
int fact=1;
for(int i=1; i<=number; ++i )
{
fact*=i;
}
return fact;
}
int ncr(int n, int r)
{
int ncr;
int fact1=factorial(n);
int fact2=factorial(n-r);
int fact3=factorial(r);
ncr = fact1 /(fact2 * fact3);
return ncr;
}
int main()
{
int rows;
printf("enter the number of rows :\n");
scanf("%d",&rows);
for(int n=0; n<rows; n++)
{
for(int i=1; i<=rows-n; i++)
{
printf(" ");
}
for(int r=0; r<=n; r++)
{
printf("%d ",ncr(n,r));
}
printf("\n");
}
return 0;
}
You can change the inner loop like this
for(int i=1; i<=rows-n; i++)
{
printf(" "); // Note the extra space
}
for(int r=0; r<=n; r++)
{
printf("%3d ",ncr(n,r)); // Changed to %3d
}
This will work upto 9 rows. If you want it to work for more rows, you can add another space in the first printf and change the second printf to %5d
printf can take a precision before the formatter. Change printf("%d ",ncr(n,r)); to printf("%3d ",ncr(n,r)); to make the numbers 3 characters wide. Also change printf(" "); to printf(" ");.
If you use
printf ("Width trick: %*d \n", 5, 10);
this will add 5 more spaces before the digit value.
This code works only if the system has unique solutions. When there is no solution or infinitely many solutions, it should print "Has no unique solution." but the code below prints "nan" or "inf". How can I do that?
#include<stdio.h>
int main()
{
int i,j,k,n;
double A[20][20],c,x[10];
printf("\nEnter the size of matrix: ");
scanf("%d",&n);
printf("\nEnter the elements of augmented matrix row-wise:\n");
for(i=1; i<=n; i++)
{
for(j=1; j<=(n+1); j++)
{
printf(" A[%d][%d]:", i,j);
scanf("%lf",&A[i][j]);
}
}
for(j=1; j<=n; j++)
{
for(i=1; i<=n; i++)
{
if(i!=j)
{
c=A[i][j]/A[j][j];
for(k=1; k<=n+1; k++)
{
A[i][k]=A[i][k]-c*A[j][k];
}
}
}
}
printf("\nThe solution is:\n");
for(i=1; i<=n; i++)
{
x[i]=A[i][n+1]/A[i][i];
printf("\n x%d=%0.3f\n",i,x[i]);
}
return(0);
}
Perform a check for the whether the number is valid or not before printing.If not valid, print your desired message.You can modify the last for loop in your code as follows:
for(i=1; i<=n; i++)
{
x[i]=A[i][n+1]/A[i][i];
//Nan and inf check
if((A[i][i]!=A[i][i]) || (A[i][i] ==0))
break;
else
printf("\n x%d=%0.3f\n",i,x[i]);
}
printf("Has no unique solution");
return(0);
}
Handling Infinity and Nan
If n=3,the output is
1*2*3
7*8*9
4*5*6
If n=5,the output is
1*2*3*4*5
11*12*13*14*15
21*22*23*24*25
16*17*18*19*20
6*7*8*9*10
CODE:
int i, j, a[50][50], k = 1, m = 0;
for (i = 0; i < n; i += 2) {
for (j = 0; j < n; j++) {
a[i][j] = k;
k++;
}
printf("\n");
}
m = k;
for (i = 1; i <= n; i += 2) {
for (j = 0; j < n; j++) {
a[i][j] = m;
m++;
}
printf("\n");
}
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
printf("%d", a[i][j]);
}
printf("\n");
}
I am not so good in c language, but i think it will help you.
please have a look, and you can do making some change if any syntax error occurs but logic is clear.
#include <stdio.h>
#include <math.h>
void printOutput(int n){
int k = ceil(n/2);
int m =1;
int j =1;
int l =k;
int i;
int b;
for(i=1;i<=n;i++){
for(b=m;b<=m+n;b++){
printf(b);
}
printf("\n");
if(i<k){
j= (2*j);
m =n*j+1;
} else{
int z = n-i-1;
m= n+1 +n*(2)*z;
l =z-2;
}
}
}
void main(){
int input;
printf("Enter a Value : ");
scanf(" %d",&input);
printOutput(input);
}
#include<stdio.h>
#include<conio.h>
int k=1;
int main(){
int n;
scanf("%d",&n);
for(int i=1;i<=n/2+1;i++){
for(int j=1;j<=n;j++){
if(j!=1&&j!=n+1){
printf("*");
}
printf("%d",k);
k++;
}
printf("\n \n");
k=k+n;
}
k=k-3*n;
for(int i=1;i<=n/2;i++){
for(int j=1;j<=n;j++){
if(j!=1&&j!=n+1){
printf("*");
}
printf("%d",k);
k++;
}
printf("\n \n");
k=k-(n/2+1)*n;
}
}
This is a rough sketch of what you should do, there are some minor flaws with it... However, the functionality is there. Next time, if you can't understand what algorithm the code calls for, I suggest you write this array out on a sheet of paper and follow each row. Notice where each row gets placed, you should start to come up with a way to do this (there are more ways than this one...) It may seem hard at first, but if you want to be in this field, you have to have the mindset for it. I agree with the others, this is NOT a homework site, rather a site to help build off the knowledge you know, so begin to actually try to write the program, and then submit it here if you're having trouble with it.
#include <stdio.h>
void loadNprint(int size, int a[][size]){
int i,j,count=1,down=size-1, up =0;
for(i=0; i<size; i++){
for(j=0;j<size; j++){
if((i%2) == 0)a[up][j] = count;
if((i%2) == 1)a[down][j]= count;
count++;
}
if((i%2) == 0)up++;//keeping track of the rows in ascending order
if((i%2) == 1)down--;//keeping track of rows in descending order
}
for(i=0; i<size; i++){
for(j=0; j<size; j++){
printf("%4d",a[i][j]);
}
printf("\n");
}
}
void main(){
int input;
printf("Enter a Value : ");
scanf(" %d",&input);
int myarray[input][input];
loadNprint(input,myarray);
}
This program works perfect. But if you want make some changes..do it yourself.
Next time please try some coding yourself before asking. This will print a different pattern for even numbers.
#include<stdio.h>
#include<conio.h>
int n,beginnew,cpy;
int main()
{
printf("Please enter a value : ");
scanf("%d",&n);
//process
beginnew=n-n/2+1;//beginning of new pattern
cpy=n-1;
for(int i=1;i<n+1;i++)
{
if(i<beginnew)
{
for(int h=n-1;h>=0;h--)
printf("%d * ", (n*(2*i-1)-h) );
}
else
{
for(int h=n-1;h>=0;h--)
printf("%d * ",(n*(cpy)-h) );
cpy=cpy-2;
}
printf("\n");
}
getch();
return 0;
}
//this code print Diagonal Pattern if matrix is
1 2 3
4 5 6
7 8 9
output is :
1
4 2
7 5 3
8 6
9
import java.util.*;
class DiagonalPattern
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int x[][];
int i,j,row,col,p,temp=1,last=0;
System.out.println("how many array wants to create and size of array");
row=sc.nextInt();
col=sc.nextInt();
x=new int[row][col];
System.out.println("Enter " +row*col+ " elements of array of array");
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
x[i][j]=sc.nextInt();
last=j;
}
}
for(i=0;i<row;i++)
{
System.out.println("");
int k=i;
for(j=0;j<=i;j++,k--)
{
if(j==col)
{
break;
}
else
{
System.out.print(x[k][j]);
System.out.print(" ");
}
}
}
for(p=x.length;p>0;p--,temp++)
{
System.out.println("");
i=x.length-1;
int k=i;
for(j=temp;j<=last;j++,k--)
{
System.out.print(x[k][j]);
System.out.print(" ");
}
}
}
}