Posts

Showing posts from September, 2020

C program to print the sum of even and odd numbers from 1 to n.

Image
  Program : #include<stdio.h> #include<conio.h> void main() { int i,n,even_sum=0,odd_sum=0; clrscr(); printf("Enter the maximum limit value\n"); scanf("%d",&n); for(i=1;i<=n;i++) { if(i%2==0) even_sum=even_sum+i; else odd_sum=odd_sum+i; } printf("The sum of even numbers upto %d =%d\n",n,even_sum); printf("The sum of odd numbers upto %d =%d",n,odd_sum); getch(); } Output :

C program to find sum of digits of the entered number.

Image
  Program : #include<stdio.h> #include<conio.h> void main() { int i,a,s=0; clrscr(); printf("Enter any number\n"); scanf("%d",&a); while(a) { i=a%10; a=a/10; s=s+i; } printf("The sum of digits is %d",s); getch(); } Output :

C program to find reverse of a number.

Image
  Program : #include<stdio.h> #include<conio.h> void main() { int n,reverse=0; clrscr(); printf("Enter a number to reverse\n"); scanf("%d",&n); while(n!=0) { reverse=reverse*10; reverse=reverse+n%10; n=n/10; } printf("Reverse of entered number is =%d\n",reverse); getch(); } Output :

Swap the values of two Integers using C programming.

Image
Swap using 3rd variable  :- Program  : #include<stdio.h> #include<conio.h> void main() { int a,b,c; clrscr(); printf("Enter the value of a\n"); scanf("%d",&a); printf("Enter the value of b\n"); scanf("%d",&b); printf("Before swapping a=%d, b=%d\n",a,b); c=a; a=b; b=c; printf("After swapping a=%d, b=%d",a,b); getch(); } Output  : Swap without using 3rd variable  :- Program  : #include<stdio.h> #include<conio.h> void main() { int a,b; clrscr(); printf("Enter the value of a\n"); scanf("%d",&a); printf("Enter the value of b\n"); scanf("%d",&b); printf("Before swapping a=%d, b=%d\n",a,b); a=a+b; b=a-b; a=a-b; printf("After swapping a=%d, b=%d",a,b); getch(); } Output  :

C program of Fibonacci series.

Image
Fibonaccci Series     0,1,1,2,3,5,8,13,21,34,55.............. Program  : #include<stdio.h> #include<conio.h> void main() {        int i,n,a=0,b=1,c; clrscr(); printf("Enter the number of terms\n"); scanf("%d",&n); printf("%d,",a); printf("%d,",b); for(i=0;i<n-2;i++) { c=a+b; printf("%d,",c); a=b; b=c; } getch(); } Output  :

C program that finds 5 subjects total marks and Average of that numbers.

Image
Program   #include<stdio.h> #include<conio.h> void main() {  int sub1,sub2,sub3,sub4,sub5,sum; float average; clrscr(); printf("Enter marks of subject 1:\t"); scanf("%d",&sub1); printf("Enter marks of subject 2:\t"); scanf("%d",&sub2); printf("Enter marks of subject 3:\t"); scanf("%d",&sub3); printf("Enter marks of subject 4:\t"); scanf("%d",&sub4); printf("Enter marks of subject 5:\t"); scanf("%d",&sub5); sum=sub1+sub2+sub3+sub4+sub5; average=sum/5; printf("Total marks obtained =%d\n",sum); printf("Average of the given data =%f",average); getch(); } Output :

C program that finds whether a given number is Even or Odd.

Image
  Program : #include<stdio.h> #include<conio.h> void main() { int n,r; clrscr(); printf("Enter a number :\n"); scanf("%d",&n); r=n%2; if(r==0) { printf("The number is EVEN"); }        else        { printf("The number is ODD");        }        getch(); } Output :

Sum of all the numbes upto a given number using C.

Image
  Program : #include<stdio.h> #include<conio.h> void main() { int i,initial_value,final_value,sum=0; clrscr(); printf("Enter the initial value:\n"); scanf("%d",&initial_value); printf("Enter the final value:\n"); scanf("%d",&final_value); for(i=initial_value;i<=final_value;i++) { sum=sum+i; } printf("Sum is %d\t",sum); getch(); } Output :