Program to Sum the Inputs Until User Enters a Positive Integer

Sum the  Inputs Until User Enters a Positive Integer.png

#include<stdio.h>

// Program to Sum the Inputs Until User Enters a Positive Integer

int main()
{
int n=0, sum=0;

// for
printf(“Using For loop\n”);
printf(“Enter any positive number “);
for(;n>=0;)
{
sum += n;
scanf(“%d”,&n);
}
printf(“Sum = %d”, sum);
//while
printf(“\n\nUsing While loop\n”);
printf(“Enter any positive number “);
n=0;
sum=0;

while(n>=0)
{
sum += n;
scanf(“%d”,&n);
}
printf(“Sum = %d”, sum);
}

return 0;}

Sum of numbers entered by a user(using for and while)

Sum of nums entered by a user.png

#include<stdio.h>
// Sum of nums entered by a user
int main()
{
int n, num, sum=0,i;
printf(“Enter how many numbers you want to enter “);
scanf(“%d”,&n);

printf(“Using for loop\n”);
for(i=1;i<=n;i++)
{
printf(“Enter the number “);
scanf(“%d”,&num);
sum= sum + num;
}

printf(“\nSum of %d numbers is %d\n\n”,n,sum);

printf(“Using while loop\n”);
i= 1;
while(i<=n)
{
printf(“Enter the number “);
scanf(“%d”,&num);
sum= sum + num;
i++; // n+=1
}

printf(“Sum of % numbers is %d”,n,sum);

return 0;
}

Pyramid and Pattern

1)

*
**
***
****
*****

#include<stdio.h>
int main(){
int i, j,rows;
printf(“Enter number of rows: “);
scanf(“%d”,&rows);
for(i=1;i<=4;i++)
{
for(j=1;j<=i;j++)
{
printf(“*”);
}
printf(“\n”);
}
return 0;
}

program1

2)

1
1 2
1 2 3
1 2 3 4
1 2 3 4 5

#include<stdio.h>
int main(){
int i, j,rows;
printf(“Enter number of rows: “);
scanf(“%d”,&rows);
for(i=1;i<=rows;i++)
{
for(j=1;j<=i;j++)
{
printf(“%d”,j);
}
printf(“\n”);
}
return 0;
}

program2

3)

A
A B
A B C
A B C D
A B C D E

#include<stdio.h>
int main(){
int i, j,rows;
printf(“Enter number of rows: “);
scanf(“%d”,&rows);
for(i=0;i<rows;i++)
{
for(j=0;j<=i;j++)
{
printf(“%c”,(‘A’+j));
}
printf(“\n”);
}
return 0;
}
Program3.png

4)

* * * * *
* * * *
* * *
* *
*

#include<stdio.h>
int main(){
int i, j,rows;
printf(“Enter number of rows: “);
scanf(“%d”,&rows);
for(i=1;i<=rows;i++)
{
for(j=i;j<=rows;j++)
{
printf(“*”);
}
printf(“\n”);
}
return 0;
}

Program4.png

5.

1 2 3 4 5
1 2 3 4
1 2 3
1 2
1

#include<stdio.h>
int main(){
int i, j,rows;

printf(“Enter number of rows: “);
scanf(“%d”,&rows);
for(i=1;i<=rows;i++)
{
for(j=1;j<=rows-i+1;j++)
{
printf(“%d”,j);
}
printf(“\n”);
}
return 0;
}

Program5.png

 

int and char

The char keyword declares a character variable. Variables of type char are 1 byte in length. They can be signed or unsigned, which means they have a range of -128 to 127 and 0 to 255, respectively.  For example:

char alphabet;

Variables of type int are one machine-type word in length. They can be signed (default) or unsigned, which means that in this configuration of the compiler they have by default a range of -32768 to 32767 and 0 to 65535 respectively

 

int a, b, c;                       // ‘a’, ‘b’, ‘c’ are integer variables