Print a right-angled triangle with certain condition in c - c

Enter a certain number, and that number is a condition which determines the number of chars in a single row.Let's say the number is 3
In the first row there is only 1 char.
In second row there is a condition. a+1 Where a is the number that we entered
In third row is 2a+1
Fourth 3a+1
And so on...
Example:
Number that we entered is 3.
a (1)
aaaa (3+1)
aaaaaaa (2*3+1)
Here is what i've came up. I have trouble with implementing that condition.
#include<stdio.h>
main()
{
int i,j,n;
printf("Enter the numbers of rows:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
for(j=1;j<=i;j++)
printf("a");
printf("\n");
}
getch();
}

Just start iterating inner for loop starting from n till i * n as below,
for(i=1;i<=n;i++)
{
for(j=n;j<=i*n;j++)
printf("a");
printf("\n");
}
Here is the demo

Suggestion: get used to start counting at 0
for(i=1;i<=n;i++) // could be for (i = 0; i < n; i++)
{
for(j=1;j<=i;j++) // could be for (j = 0; j < i; j++)
You have to have a multiplication by 3 "somewhere". Try and find the right place and what to multiply by 3.

for(i=0;i<n;i++){
for(j=0;j<n*i+1;j++)
printf("a");
printf("\n");
}

Related

C program to read 'n' numbers and find out the sum of odd valued digits of each number and print them

i am new to programing, i want to know that how we can find the odd digits in a number.
the condition in this program is we should only use concept of arrays.I tried a code for this as follows:
#include <stdio.h>
int main()
{
int A[50],i,x,y,n,sum=0;
scanf("%d",&n);
printf("the value is %d\n",n);
for(i=0;i<n;i++)
scanf("%d",&A[i]);
for(i=0;i<n;i++){
x=A[i]%10;
if(x%2!=0)
sum=sum+x;
A[i]=A[i]/10;
printf("the sum of odd numbers is %d\n",sum);
}
return 0;
}
but in this the code is checking for only one digit of the first number in the loop and then next time it is going to check the digit of second number.
so, i need to write a code to check all digits in the number and then it goes to next number and check all digits and should continue the same process in the loop.So, for this how should i modify my code?
You were missing a loop that would iterate through every digit of A[i] - the inner while loop below,
#include <stdio.h>
int main()
{
int A[50], i, x, y, n, sum=0;
printf("How many numbers will you input?\n");
scanf("%d",&n);
printf("the value is %d\n",n);
for(i=0; i<n; i++) {
scanf("%d",&A[i]);
}
for(i=0; i<n; i++) {
sum = 0;
while (A[i] > 0) {
x = A[i]%10;
if(x%2 != 0) {
sum = sum + x;
}
A[i] = A[i]/10;
}
printf("the sum of odd numbers is %d\n",sum);
}
return 0;
}
The exact algorithm for iterating through each digit in a nice form can be found in this post - although for a different language. Here, apart from the while loop, you also need to reset the sum each time unless you want a cumulative sum over all provided numbers.
Note that I changed the formatting a bit - more space, extra braces, and a message about what you're prompting the user to input.
int temp;
int sum = 0;
temp = number;
do {
lastDigit = temp % 10;
temp = temp / 10;
sum += (lastDigit %2 != 0) ? lastDigit : 0;
} while(temp > 0);

(Lotto 6/49 simulation) Calculate how many draws will take place until a user-entered combination hits the jackpot

I have my code and data for my Lotto 6/49 simulation. I just don't know how to create a simple mathematical expression to calculate how many draws will take place until the user hits the jackpot
Code:
#include <stdio.h>
#include <stdlib.h> //need this library to use srand and rand without warnings
#include <time.h> //need this library to use time function
int main(void) {
int i, j, k , temp, count1=0;
unsigned int count2=1;
int select[6];
int jackpot[6];
printf("Welcome to Lotto 6/49! The 6 numbers you choose must match the 6 randomly chosen numbers in the lottery in order to win the jackpot. Select your 6 numbers!\n\n");
for(i=0; i<6; i++)
{
printf("Enter a number between 1-49: ");
scanf("%d", &select[i]);
}
srand(time(NULL));
for(;;){
for(i=0; i!= 6 ; ) //This loop produces the winning number combination
{
temp= (rand() %49)+1; //produce a random number between 1 and 49
for( k =0 ; k<i && (temp!=jackpot[k]) ; k++); //make sure we do not have duplicate
if (k==i){ //only write to the array if we do not have a duplicate
jackpot[i] = temp;
i++; //only increment the loop if we write a value to the array
}
}
printf("\nThe winning numbers are: "); //displays the winning numbers
for(i=0;i<6;i++)
printf("\t%d\t",jackpot[i]);
printf("\n");
// This loop will check the user array with the jackpot array to determine if it is a winning combination
for(i=0; i<6 && i ==count1; i++){ //if we do not match the first element, we skip the rest
for(j=0; j<6 ; j++)
{
if(select[i]==jackpot[j]){
count1+=1; // only increment if we match
}
}
}
if(count1==6){ //all 6 numbers matched
printf("\n\nYou win the JACKPOT! Congratulations! \nIt took you %d draws to win", count2);
break;
}
else{
count2+=1;
count1=0; //reset match count
}
}
return 0;
}
Data (number range limited to 8 numbers for easier recording, eg temp= (rand() %8)+1 instead of temp= (rand() %49)+1 # line 19.)
Count1 = user input matched
Count2 = # of draws
↑
I have no clue how to calculate this

Removing repetitions of same numbers in an array

Task is to display the array that has no repetitions based on some user generated input.
I'm trying to compare the number with every number before it, if the equality happens, a=1, it should skip it. Code doesn't return anything.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int X[30],Y[30],i,j,k=0,a,N;
printf("Length of the vector: ");
scanf("%d",&N);
printf("Input the numbers: ");
for(i=0;i<N;i++)
scanf("%d",X+i);
Y[0]=X[0];
for(i=1;i<N;i++){
for(j=i-1;j>=0;j--)
if(X[i]=X[j])
a=1;
if(a==0){
k++;
Y[k]=X[i];
}
a=0;
}
for(i=0;i<k;i++)
printf("%d",Y[i]);
}
Three separate issues in your code block:
a is not initialized the first time through your loop. Add a line a = 0; above your loop.
Your if block reads if(X[i]=X[j]); it should be if(X[i] == X[j]) (you're missing one =)
Your final value of k is going to be one less than the total number of elements that you have. Change your final for loop to i = 0; i <= k; i++

Print the last string name in C

I am trying to learn C and here i got a program in which we have to take the input from the user as n number os strings, compare it and arrange it in a alphabetical order. After arranging them in a alphabetical order , i have to only print the last name which was occurring in the order.
Here is the code for the above problem:
#include<stdio.h>
#include<string.h>
int main()
{
int i,j,m,n,len;
char a[50][50],temp[100];
char last ;
printf("Enter the number of elements you wish to order : ");
scanf("%d",&m);
printf("\nEnter the names :\n");
for (i=0;i<m;i++){
scanf("%s",a[i]);
}
for (i=0;i<m;i++){
for (j=i+1;j<m+1;j++) {
if (strcmp(a[i],a[j])>0) {
strcpy(temp,a[i]);
strcpy(a[i],a[j]);
strcpy(a[j],temp);
}
}
}
printf("\n\nSorted strings are : ");
for (i=0;i<m+1;i++){
printf("%s \n",a[i]);
}
return 0;
}
~
The Answer goes this way:
Enter the number of elements you wish to order : 4
Enter the names :
territory
states
hello
like
Sorted strings are : S$???
hello
like
states
territory
My question goes that why am i getting "S$???" and i want only the last word "territory should be printed out not all the names".
Can anyone let me know where am i going wrong? It will be a great help.
Thanks
Tanya
You are sorting m+1 strings with indexes [0..m]. But you input only m strings.
Your indexes should never go above m-1.
\Check this code
In your code you get input as 4
then
a[0]=territory
a[1]=states
a[2]=hello
a[3]=like
But your code runs upper loop at most 3 time then i=3
In next loop j=i+1 then j=4
but a[4]=not exists
Thats why "S$???" occurs
Solution:
#include<stdio.h>
#include<string.h>
int main() {
int i, j, m, n, len;
char a[50][50], temp[100];
char last;
printf("Enter the number of elements you wish to order : ");
scanf("%d", &m);
printf("\nEnter the names :\n");
for (i = 0; i < m; i++) {
scanf("%s", a[i]);
}
for (i = 0; i < m - 1; i++) { // m - 1 enough maximum i = 2;
for (j = i + 1; j < m; j++) { // j maximum j = i + 1 j = 3
if (strcmp(a[i], a[j]) > 0) {
strcpy(temp, a[i]);
strcpy(a[i], a[j]);
strcpy(a[j], temp);
}
}
}
printf("\n\nSorted strings are : "); // print all words in sorted order
for (i = 0; i < m; i++) {
printf("%s \n", a[i]);
}
printf("Last Word:\n");
printf("%s\n", a[m - 1]); // a[3] contains last name
return 0;
}
this was my program it worked for me in turbo c++
#include<conio.h>
#include<iosream.h>
#include<ctype.h>
#include<string.h>
#include<stdio.h>
#include<stdlib.h>
void main()
{
clrscr();
char name[100];
int c=0;
// to take the name including spaces
gets(name);
//this line is to print the first character of the name
cout<<name[0];
//this loop is to print the fist character which is there after every space
for(int l=0;name[l]!='\0';l++)
{
if(name[l]==' ')
{
cout<<"."<<name[l+1];
//l+1 is used because l is the space and l+1 is the character that we need
}
}
//this loop is to find out the last space in the entire sting
for(int i=0;name[i]!='\0';i++)
{
if(name[l+1]==NULL)
{
//here we fond the last space in the string
for(int j=i;name[j]!=' ';j--)
{
c=j+1;
}
// c=j+1 is used bacause we have already taken the first letter of the that word
//no need of taking it again
}
}
//this loop starts from the last space and the position(of space) is stored in k
for(int k=c;name[k]!='\0';k++)
{
cout<<name[k];
}
getch();
}
output:
anentt ranjan shukla
a.r.shukla

C program to print next five prime numbers

I have a program that prints five prime numbers within a user input range(m,n).
My problem is i want to print numbers greater than m. and print only next five numbers. I don't want to use upper limit.
How can I do it so?
#include <stdio.h>
#include <conio.h>
int main()
{
int m,n,i,j,k,flag;
printf("\nEnter The Lower Limit: ");
scanf("%d",&m);
printf("\nEnter The Upper Limit: ");
scanf("%d",&n);
printf("\nPrime Numbers Between %d & %d Are:\n",m,n);
for(i=m ; i<=n ; i++)
{
k=i;
flag=1;
for(j=2 ; (j<=k/2)&&flag ; j++)
{
if(k%j==0)
flag=0;
}
if(flag)
printf("%3d \n",i);
}
}
Why not simply count the number of primes you have printed?
int count = 0;
:
for(i=m ; (i<=n) && (count<5) ; i++)
:
if(flag)
{
printf("%3d \n",i);
count++;
}
PS, using longer names than single characters will help make your program more understandable.
Whether you like it or not, you have a higher limit which is set by the data type you use -> int, in your case. Imagine you want the first 5 prime numbers larger than (MAXINT- 10)...you get the point, but let's assume you are not concerned about corner-cases.
int count=0;
int i=m;
int k;
while (count<5)
{
k=i;
flag=1;
for(j=2 ; (j<=k/2)&&flag ; j++)
{
if(k%j==0)
flag=0;
}
if(flag)
{
printf("%3d \n",i);
count++;
}
i++;
}

Resources