C Programming Basics Problems for Beginners
1. Generating a series of numbers and calculating their sum in V
Generating like 9 99 999 9999 99999 and there sum
#include <stdio.h> void main() { long int n,i,t=9; int sum =0; printf("Input the number or terms :"); scanf("%d",&n); for (i=1;i<=n;i++) { t=t*10+9; sum =sum+t; printf("%d ",t); } printf("\nThe sum of the series = %d \n",sum); }
output :
2.The Sum of Odd Natural Numbers in C
#include<stdio.h> void main () { int i,n,odd=0,sum=0; scanf("%d",&n); for(i=1;i<=n;i++){ if(i%2!=0){ printf (" %d",i); sum=i+sum; } } printf("\nThe odd numbers are :%d\n",n/2+1); printf("The Sum of odd Natural Number upto 10 terms : %d\n",sum); }
For example, if the user enters 5, the output could be:
Output:
1 3 5
The odd numbers are: 3
The sum of the first 5 odd natural numbers is 9
3.Prime number Checking in C
#include<stdio.h> void main () { int i,n,f=0,count=1; printf("Enter a number:"); scanf("%d",&n); for(n=1;i<=100;n++){ for(i=2;i<n;i++){ if(n%i==0){ f++; break; } } } if(f==0) {printf("prime number ");} else { printf("prime not number ");} }
Input:
Enter a number: 5
Output:
prime not number
4.The Sum of Square Natural Number upto 5 terms in C
#include<stdio.h> void main () { int i, n,square=1,sum=0; scanf("%d",&n); printf("The square natural upto 5 terms are :"); for(i=1;i<=n;i++){ square=i*i; printf("%d ",square); sum=square+sum; } printf("\nThe Sum of Square Natural Number upto 5 terms:%d",sum); }
Input: 5
Output:
The square natural upto 5 terms are :1 4 9 16 25
The Sum of Square Natural Number upto 5 terms:55
5.The sum of the digits of the Given Number in C
#include<stdio.h> void main () { int r,n,sum=0; printf("Enter any number:"); scanf("%d",&n); while(n>0){ r=n%10; sum=sum+r; n=n/10; } printf("Sum of the digits of Given Number = %d",sum); }
Input: Enter any number:567
Output: Sum of the digits of the Given Number = 18
6. Area of Sphere surface in C
#include<stdio.h> int main() { double radius,area; const double pie =3.1416; printf("Enter a radius :"); scanf("%lf",&radius); area =4*3.1416*radius*radius; printf("Sphere surface is:%.3lf",area); return 0; }
Input: Enter a radius: 8
Output: Sphere surface is:804.250