Introduction to C Programming - while, do while, nested loops, arrays

Display all the numbers that are divisible by 7 between 1 and 100

 
      #include<stdio.h>

	int main()
	{
		int count = 0;
		int i=0;
		for (i = 1; i < 100;i++)
		{
			if (i % 7 == 0)
			{
				count++;
				//printf("%d", i);
			}
		}
		printf("The count is %d", count);
		system("pause");
		return 0;
	}
    
Output

Find the sum of n numbers by getting the value of n from the user.

 
      #include<stdio.h>

	int main()
	{
		int i=0;
		int n = 0;
		int sum = 0,input=0;
		printf("Enter the value for n");
		scanf("%d", &n);
		for (i = 0; i < n;i++)
		{
			printf("Enter the number");
			scanf("%d", &input);
			sum += input;
		}
		printf("The sum of numbers is %d", sum);
		system("pause");
		return 0;
	}
    
Output

Find the sum of sum of 1+3+5+7..n. Get the value of n as input from the user

 
      #include<stdio.h>

	int main()
	{
		int i=0;
		int n = 0;
		int sum = 0;
		int k = 1;
		printf("Enter the value for n");
		scanf("%d", &n);
		for (i = 0; i <n;i++)
		{
			sum += k;
			k = k + 2;
		}
		printf("The sum of numbers is %d", sum);
		system("pause");
		return 0;
	}
    
Output

Determine the count of even digits in a given number

 
      #include<stdio.h>

	int main()
	{   
		int num = 0,last_digit=0,counteven=0;
		printf("Enter a number");
		scanf("%d", &num);
		while (num > 0)
		{
			last_digit = num % 10;
			if (last_digit % 2 == 0)
			{
				counteven += 1;
			}
			num = num / 10;
		}
		printf("The count of even numbers is %d", counteven);
		system("pause");
		return 0;
	}
    
Output

Write a C program which will allow an user to add two numbers. The user should be able to add two numbers any number of times until he wishes to quit

 
      #include<stdio.h>

	int main()
	{   
		int i=0, num1=0,num2=0,choice=1;
		
		while (choice)
		{
			printf("Enter two numbers");
			scanf("%d%d", &num1, &num2);
			printf("The result is %d", num1 + num2);
			printf("\nEnter your choice 0-Exit 1-continue");
			scanf("%d", &choice);
		}
		system("pause");
		return 0;
	}
    
Output

Dividing two numbers by forcing the user to enter only a non zero denominator

 
      #include<stdio.h>

	int main()
	{   
		int numer=0,denom=0,result=0;
		
			printf("Enter the numerator");
			scanf("%d", &numer);
			do
			{
				printf("Enter the a non zero denominator");
				scanf("%d", &denom);
			} while (denom == 0);
			result = numer / denom;
			printf("result is %d", result);
		system("pause");
		return 0;
	}
    
Output

print the pattern

 
      #include<stdio.h>
	int main()
	{   
		int i = 0, j = 0,n=0;
		scanf("%d", &n);
		for (i = 0; i < n; i++)
		{
			for (j = 0; j <= i; j++)
			{
				printf("*");
			}
			printf("\n");
		}
		system("pause");
		return 0;
	}
    
Output

Print the pattern as shown below

 
      1
12
123
1234
.....

#include<stdio.h>
	int main()
	{   
		int i = 0, j = 0,n=0;
		scanf("%d", &n);
		for (i = 0; i <n ; i++)
		{
			for (j = 1; j <= i+1; j++)
			{
				printf("%d",j);
			}
			printf("\n");
		}
		system("pause");
		return 0;
	}
    
Output

Print the inverted pattern as shown below

 
      ****
***
**
*
#include<stdio.h>
	int main()
	{   
		int i = 0, j = 0,n=0;
		scanf("%d", &n);
		for (i = n; i > 0; i--)
		{
			for (j = 0; j < i; j++)
			{
				printf("*");
			}
			printf("\n");
		}
		system("pause");
		return 0;
	}
    
Output