C Program To Count Positive, Negative and Zero using Ternary Operator and without using Array

Lets write a C program to enter number till the user wants. At the end it should display the count of positive number, negative number and zeros entered, using Ternary Operator or Conditional Operator and without using arrays.

Related Read:
while loop in C programming
Positive or Negative or Zero Using Ternary Operator: C Program

Note:
Any number greater than 0 is positive.
Any number less than 0 is negative.

scale

Expected Output for the Input

User Input:
Enter the limit
6
Enter 6 numbers
-1
0
2
-3
5
6

Output:
Positive Numbers: 3
Negative Numbers: 2
Number of zero: 1

Logic To Count Positive, Negative and Zero using Ternary Operator and without using Array

Complete logic to this program is present at C Program To Count Positive, Negative and Zero without using Array

Video Tutorial: C Program To Count Positive, Negative and Zero using Ternary Operator and without using Array


[youtube https://www.youtube.com/watch?v=OMGSxVCav24]

YouTube Link: https://www.youtube.com/watch?v=OMGSxVCav24 [Watch the Video In Full Screen.]

Source Code: C Program to Count Positive, Negative and Zero using Ternary Operator and without using Array

#include < stdio.h >

int main()
{
    int limit, num, positive = 0, negative = 0, zero = 0;

    printf("Enter the limit\n");
    scanf("%d", &limit);

    printf("Enter %d numbers\n", limit);

    while(limit)
    {
        scanf("%d", &num);
        (num > 0) ? positive++ : ((num < 0) ? negative++ : zero ++);
        limit--;
    }

    printf("\nPositive Numbers: %d\n", positive);
    printf("Negative Numbers: %d\n", negative);
    printf("Number of zero: %d\n", zero);

    return 0;
}

Output:
Enter the limit
5
Enter 5 numbers
2
-1
5
6
0

Positive Numbers: 3
Negative Numbers: 1
Number of zero: 1

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

C Program To Count Positive, Negative and Zero without using Array

Lets write a C program to enter number till the user wants. At the end it should display the count of positive number, negative number and zeros entered, without using arrays.

Related Read:
while loop in C programming
Number is Positive or Negative or Zero: C Program

Note:
Any number greater than 0 is positive.
Any number less than 0 is negative.

scale

Expected Output for the Input

User Input:
Enter the limit
5
Enter 5 numbers
0
5
3
2
-1

Output:
Positive Numbers: 3
Negative Numbers: 1
Number of zero: 1

Video Tutorial: C Program to Count Positive, Negative and Zero without using Array


[youtube https://www.youtube.com/watch?v=SuvrPe4Bg2c]

YouTube Link: https://www.youtube.com/watch?v=SuvrPe4Bg2c [Watch the Video In Full Screen.]

Source Code: C Program to Count Positive, Negative and Zero without using Array

#include < stdio.h >

int main()
{
    int limit, num, positive = 0, negative = 0, zero = 0;

    printf("Enter the limit\n");
    scanf("%d", &limit);

    printf("Enter %d numbers\n", limit);

    while(limit)
    {
        scanf("%d", &num);

        if(num > 0)
        {
            positive++;
        }
        else if(num < 0)
        {
            negative++;
        }
        else
        {
            zero++;
        }

        limit--;
    }

    printf("\nPositive Numbers: %d\n", positive);
    printf("Negative Numbers: %d\n", negative);
    printf("Number of zero: %d\n", zero);

    return 0;
}

Output 1:
Enter the limit
8
Enter 8 numbers
1
-5
0
6
-2
0
5
4

Positive Numbers: 4
Negative Numbers: 2
Number of zero: 2

Output 2:
Enter the limit
10
Enter 10 numbers
2
6
9
3
-5
-7
0
9
-10
-50

Positive Numbers: 5
Negative Numbers: 4
Number of zero: 1

Logic To Count Positive, Negative and Zero without using Array

We ask the user to enter the maximum numbers he or she wants to enter, and store it inside variable limit. Now we iterate the while loop limit number of times. Inside the while loop, for each iteration, until limit is equal to zero, we keep asking the user to enter a number. Once the user enters the number we check if its greater than 0, if its true we’ll increment the value of variable positive by one. If the user entered number is less than 0, then we increment the value of variable negative by one. If the user entered number is neither greater than 0 nor less than 0, then its a zero – so we increment the value of variable zero by one.

For each iteration of the while loop, we decrement the value of variable limit by one. Once the value of variable limit is 0, control exits the while loop and the result will be printed. That is, the number of positives, negatives and the zeros entered by the user.

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

C Program To Draw Pyramid of Alphabets, using While Loop

Lets write a C program to draw / print / display a pyramid / triangle formed from uppercase / capital letter English Alphabets, using while loop.

Related Read:
Nested While Loop: C Program
C Program To Draw Pyramid of Stars, using While Loop

Expected Output for the Input

User Input:
Enter the number of rows for Pyramid
5

Output:

    
     A
    BCD
   EFGHI
  JKLMNOP
 QRSTUVWXY

Pyramid With 20 Rows
pyramid of alphabets

Video Tutorial: C Program To Draw Pyramid of Alphabets, using While Loop


[youtube https://www.youtube.com/watch?v=eDPYA_c73I4]

YouTube Link: https://www.youtube.com/watch?v=eDPYA_c73I4 [Watch the Video In Full Screen.]

Logic To Draw Pyramid of Alphabets, using While Loop

We ask the user to enter the maximum number of rows for the pyramid and store it inside the address of variable num. We declare and initialize a variable count to 1(indicating first row of the pyramid).

In the outer while loop we check if count is less than or equal to num. For each iteration of the while loop we increment the value of count by 1. So the count value will have the row number i.e., for each iteration of the outer while loop we select row one by one (to print the alphabets).

Inside first inner while loop we print the adequate number of space for each row. Inside the second inner while loop we actually print the alphabets needed for each row.

At the end the result will be a pyramid with the number of rows as input by the user.

ASCII Value of Capital Letter English Alphabets

ASCII value of A is 65

ASCII value of B is 66

ASCII value of C is 67

ASCII value of D is 68

ASCII value of E is 69

ASCII value of F is 70

ASCII value of G is 71

ASCII value of H is 72

ASCII value of I is 73

ASCII value of J is 74

ASCII value of K is 75

ASCII value of L is 76

ASCII value of M is 77

ASCII value of N is 78

ASCII value of O is 79

ASCII value of P is 80

ASCII value of Q is 81

ASCII value of R is 82

ASCII value of S is 83

ASCII value of T is 84

ASCII value of U is 85

ASCII value of V is 86

ASCII value of W is 87

ASCII value of X is 88

ASCII value of Y is 89

ASCII value of Z is 90

Reference: C Program To Print All ASCII Characters and Code

Source Code: C Program To Draw Pyramid of Alphabets, using While Loop

#include < stdio.h >

int main()
{
    int num, count = 1, i, alphabet = 65;

    printf("Enter the number of rows for Pyramid\n");
    scanf("%d", &num);

    while(count <= num)
    {
        i = 0;
        while(i <= (num-count))
        {
            printf(" ");
            i++;
        }

        i = 0;
        while(i < (2*count-1))
        {
            printf("%c", alphabet);

            if(alphabet == 90)
            {
                alphabet = 64;
            }

            alphabet++;
            i++;
        }
        printf("\n");
        count++;
    }

    return 0;
}

Output
Enter the number of rows for Pyramid
14

              A
             BCD
            EFGHI
           JKLMNOP
          QRSTUVWXY
         ZABCDEFGHIJ
        KLMNOPQRSTUVW
       XYZABCDEFGHIJKL
      MNOPQRSTUVWXYZABC
     DEFGHIJKLMNOPQRSTUV
    WXYZABCDEFGHIJKLMNOPQ
   RSTUVWXYZABCDEFGHIJKLMN
  OPQRSTUVWXYZABCDEFGHIJKLM
 NOPQRSTUVWXYZABCDEFGHIJKLMN

Note: Inside second nested while loop we are checking for the condition – if alphabet is equal to 90(ASCII value of Z), and then reinitializing it to 64(but ASCII value of A is 65), that is because after the if condition is executed we have alphabet++; statement which increments the value of variable alphabet from 64 to 65(which is the ASCII value of A).

Alphabets and ASCII Value
We can even write the code in if else block as shown below:

        i = 0;
        while(i < (2*count-1))
        {
            printf("%c", alphabet);

            if(alphabet == 90)
            {
                alphabet = 65;
            }
            else
            {
                alphabet++;
            }
            i++;
        }

In this case we assign the value of variable alphabet to 65(ASCII value of A) once it has reached a value of 90(ASCII value of Z).

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

C Program To Draw Pyramid of Stars, using While Loop

Lets write a C program to draw / print / display a pyramid / triangle formed from stars, using while loop.

Related Read:
while loop in C programming
Nested While Loop: C Program

Expected Output for the Input

User Input:
Enter no of rows of Pyramid
5

Output:

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

Pyramid With 20 Rows
pyramid of stars

Video Tutorial: C Program To Draw Pyramid of Stars, using While Loop


[youtube https://www.youtube.com/watch?v=pwLx0cELxY4]

YouTube Link: https://www.youtube.com/watch?v=pwLx0cELxY4 [Watch the Video In Full Screen.]

Logic To Draw Pyramid of Stars, using While Loop

We ask the user to enter the maximum number of rows for the pyramid and store it inside the address of variable num. We declare and initialize a variable count to 1(indicating first row of the pyramid).

In the outer while loop we check if count is less than or equal to num. For each iteration of the while loop we increment the value of count by 1. So the count value will have the row number i.e., for each iteration of the outer while loop we select row one by one (to print the stars).

Inside first inner while loop we print the adequate number of space for each row. Inside the second inner while loop we actually print the stars needed for each row.

At the end the result will be a pyramid with the number of rows as input by the user.

Source Code: C Program To Draw Pyramid of Stars, using While Loop

#include<stdio.h>

int main()
{
    int num, count = 1, i;

    printf("Enter no of rows of Pyramid\n");
    scanf("%d", &num);

    while(count <= num)
    {
        i = 0;
        while( i <= (num - count) )
        {
            printf(" ");
            i++;
        }

        i = 0;
        while(i < (2*count-1))
        {
            printf("*");
            i++;
        }

        printf("\n");
        count++;
    }

    return 0;
}

Output 1:
Enter no of rows of Pyramid
5

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

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

C Program To Find Sum of Squares of Numbers from 1 to N, using While Loop

Lets write a C program to find sum of squares of all the numbers between 1 and user entered number, using while loop.

Related Read:
while loop in C programming

Logic To Find Sum of Squares of Numbers from 1 to N, using While Loop

We ask the user to enter the limit. We initialize variable count to 1. Now inside while loop we square the value of variable count and add it to the previous value of variable sum – we repeat this for each iteration of while loop until value of count is less than or equal to value entered by the user(which is stored in variable N). For each iteration of the while loop value of variable count is incremented by 1.

Video Tutorial: C Program To Find Sum of Squares of Numbers from 1 to N, using While Loop


[youtube https://www.youtube.com/watch?v=YZWcf2lW1sA]

YouTube Link: https://www.youtube.com/watch?v=YZWcf2lW1sA [Watch the Video In Full Screen.]

Source Code: C Program To Find Sum of Squares of Numbers from 1 to N, using While Loop

#include < stdio.h >

int main()
{
    int N, count = 1, sum = 0;

    printf("Enter the limit\n");
    scanf("%d", &N);

    while(count <= N)
    {
        sum = sum + (count * count);
        count++;
    }
    printf("Sum of squares of numbers from 1 to %d is %d.\n", N, sum);

    return 0;
}

Output:
Enter the limit
5
Sum of squares of numbers from 1 to 5 is 55.

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert