0% found this document useful (0 votes)
9 views

C Language Codes

C++/c turbo codes

Uploaded by

SHANKAR Banjara
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

C Language Codes

C++/c turbo codes

Uploaded by

SHANKAR Banjara
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 27

Cube from given range (Cube.

c)

void main()
{
int i,start,end;
clrscr();
printf("Enter start and end of range:");
scanf("%d %d", &start,&end);
printf("Cube of numbers from %d to %d:\n",start,end);
for(i=start;i<=end;i++)
{
printf("%d^3=%d\n",i,i*i*i);
}
getch();
}

Factors (factors.c)(ginu.c)
void main() {
int n,i,count=0;
clrscr();
printf("Enter a no:");
scanf("\n%d",&n);
\\printf("Factors of %d:",n);
for (i=1;i<=n;i++)
{
if (n%i==0)
{
printf("\n%d is a factor", i);
count++;
}
}
printf("\nNumber of factors: %d",count);
getch();
}

Students's Percentage (grade.c)


#include<stdio.h>
int main()
{
float percentage;
int eng,hindi,sci;
clrscr();
printf("Enter your marks of three subjects");
scanf("%d %d %d", &eng, &hindi,&sci);
printf("Total marks:%d + %d +%d=%d\n",eng,hindi,sci,eng+hindi+sci);
percentage=((float)eng+hindi+sci)/300.0*100;
printf("percentage:%2f%%\n",percentage);
if (percentage>=75)
printf("Distinction");
else
if (percentage>=60)
printf("I Division");
else
if (percentage>=48)
printf("II Division");
else
if (percentage>=36)
printf("III Division");
else
printf("Fail");
getch();
}

Greater number b/w 2:- (Hishu.c)(jk.c)

void main()
{
int a,b;
clrscr();
printf ("Enter two numbers:");
scanf ("%d %d",&a,&b);
(a>b)?printf("%d is greater", a): printf("%d is greater",b);
getch();
}

#include<stdio.h>
int main()
{
int n,i;
printf("Enter two no.");
scanf("%d %d",&i, &n);
if (i>=n)
printf("%d",n);
else
printf("%d",i);
return 0;
}

Identify odd/even number:- (kk.c)

#include<stdio.h>
void main()
{
int a;
clrscr();
printf ("Enter a number:");
scanf("%d",&a);
if (a%2==0)
{
printf("%d is even",a);
}
else
{
printf ("%d is odd",a);
}
getch();
}

Identify Fizz Number:-


(Kanhaiya.c)

#include<stdio.h>
int main()
{
int a;
clrscr();
printf("Enter a number);
scanf("%d",&a);
if ((a%10==0)||(a%7==0))
printf ("No. is fizz no. %d",a);
else
printf ("No. is not a Fizz no. %d",a);
getch();
}

Divide:- (Khushbu.c)

#include<stdio.h>
int main()
{
int a,b;
float c;
clrscr();
printf("Enter two values:");
scanf("%d %d",&a,&b);
c=(float)a/b;
printf ("Values of c: %f",c);
getch();
return 0;
}

Eligibility for loan

#include<stdio.h>
int main()
{
float a,b;
clrscr();
printf("Enter your age:");
scanf ("%d",&a);
if (a>=18)
{
printf("Enter your annual income:");
scanf("%f",&b);
if (b>=500000)
printf("Eligible for loan");
else
printf("Not eligible for loan");
}
else
printf("Not eligible for loan");
getch();
}

TRIANGLE FINDING (kiku.c)


void main()
{
int a,b,c;
clrscr();
printf("Enter value of three sides of triangle:");
scanf("%d %d %d", &a,&b,&c);
if ((a==b)&&(b==c))
{
printf("Equilateral triangle");
}
else
if ((a==b)||(b==c) ||(c==a))
{
printf("Isoscles triangle");
}
else
printf("Scalene Triangle");
getch();
}

ODD Number print (kirmanda.c)

void main()
{
int n,i;
clrscr();
printf("Enter a no:");
scanf("%d",&n);
i=1;
while (i<=n)
{
printf("%d\n", i);
i=i+2;
}
getch();
}

Descending Order for numbers

include<stdio.h>
int main()
{
int n,i;
printf("Enter two no.");
scanf("%d %d",&i, &n);
while (i>=n)
{
printf("%d\n",i);
i--;
}
return 0;
}
Factorial of any digit (kitty.c)
#include <stdio.h>
void main()
{
int a,f=1,i;
clrscr();
printf("Enter a digit:);
scanf("%d",&a);
for(i=1;i<=a;i++)
{
f=f*i;
}
printf("%d",f);
getch();
}

Sum of first ten odd numbers (kk1.c)

int main()
{
int i, sum = 0 , c = 0;
clrscr();
for( i = 1; i <= 20; i++)
{
if (i %2! = 0)
{
c = c + 1
sum=sum+i;
}
}
printf("sum of odd no.%d\n",sum);
printf("counting of odd no. %d",c);
getch();
}

Prime or not (kk456.c)

void main()
{
int i,n,c=0;
clrscr();
printf("Enter a number");
scanf("%d",&n);
for(i=2;i<=n-1;i++)
{
if (n%i==0)
{
printf("%d is not prime",n);
c=1;
break;
}
}
if (c==0)
printf("%d is prime",n);
getch();
}

Prime or not (kk12.c)

{
int i,n;
clrscr();
printf("Enter a +ve no.");
scanf("%d",&n);

for(i=2;i<n;i++)
{
if (n%i==0)
{
printf("%d is not a prime no.",n);
break:
}
}
if (i==n)
printf("%d is a prime no",n);
getch();
}

First ten even numbers sum


(kk123.c)

int main()
{
int i,sum=0,c=0;
for(i=1;i<=20;i++)
{
if (i%2==0)
{
c=c+1;
sum=sum+i;
}
printf( sum of even no. %d, sum);
printf(counting of even no, %d",c);
getch();
}

Swap Numbers (krish.c)

#include<stdio.h>
int main()
{
int a,b,c;
clrscr();
printf("enter two numbers:");
scanf ("%d %d",&a,&b);
printf("original values %d %d\n",a,b);
c=a;
a=b;
b=c;
printf("after swapping values are %d %d",a,b);
getch();
return 0;
}

#include<stdio.h>
int main()
{
int a,b,c;
clrscr();
printf("enter two numbers");
scanf("%d %d",&a,&b);
printf("Before Swapping values are %d %d\n",a,b);
c=a;
a=b;
b=c;
printf("After swapping values are %d %d",a,b);
getch();
return 0;
}

Menu Driven basic arithmetic operations (kuki.c)

void main()
{
int a,b,c,ch;
clrscr();
printf (" 1.Add\n 2.Sub\n 3. Multiply\n 4. Div\n 5. Modulas\n Enter your
choice")
scanf("%d",&ch);
printf ("Enter value of a and b");
scanf("%d %d",&a,&b);
switch(ch)
{
case 1: printf("a+b=%d",a+b); break;
case 2: printf("a-b=%d",a-b); break;
case 3: printf("a*b=%d, a*b);break;
case 4: printf("a/b=%d",a/b);break;
case 5: printf("a mod b"=%d",a%b); break;
default:printf("Enter valid data b/w 1 to 5");
}
getch();
}

Total notes required for n amount


(kuko.c=error file)(lmao.c)
void main()
{
float c500=0,c200=0,c100=0,c50=0,c20=0,c10=0,c5=0,c2=0,c1=0;
float tamt:
clrscr();
printf ("Enter your amt");
scanf("%f",&tamt);
c500=(tamt/500.0);
printf ("500 Rs notes require is %2f\n",c500);
tamt=(tamt%500.0);
c200=(tamt/200.0);
printf ("200 Rs notes require is %2f\n",c200);
tamt=(tamt%200.0);
c100=(tamt/100.0);
printf ("100 Rs notes require is %2f/n",c100);
tamt=(tamt%100.0);
c50=(tamt/50.0);
printf ("50 Rs notes require is %2f\n",c50);
tamt=(tamt%50.0);
c20=(tamt/20.0);
printf ("20 Rs notes require is %2f\n", c20);
tamt=(tamt%20.0);
c10=(tamt/10.0);
printf ("10 Rs notes require is %2f\n",c10);
tamt=(tamt%10.0);
c5=(tamt/5.0);
printf ("5 Rs.coins require is %2f\n",c5);
tamt=(tamt%5);
c2=(tamt/2.0);
printf ("2Rs coins require is %2f\n",c2);
tamt=(tamt%2.0);
c1=(tamt/1.0);
printf ("1Rs coins require is %2f\n",c1);
getch();
}

Eligible to vote (kuku.c)

#include<stdio.h>
int main()
{
int age;
clrscr();
printf ("Enter your age:");
scanf(%d,&age);
if (age>=18)
printf ("You are eligible to vote");
else {
int wait=18-age;
printf ("You are not eligible to vote. You have to wait for %d
year(s)",wait)
}
getch();
return 0;
}
Switch Statement (kushie.c)

void main()
{
int wd;
clrscr();
printf("Enter number b/w 1 to 7");
scanf("%d", &wd);
switch (wd)
{
case 1: printf("Monday); break;
case 2: printf("Tuesday"); break;
case 3: printf("Wednesday"); break;
case 4: printf("Thurday); break;
case 5: printf("Friday"); break;
case 6: printf("Saturday"); break;
case 7: printf("Sunday"); break;
Default: printf ("Enter valid number");
}
}
getch();
}

Check max no. b/w three numbers: (max.c)

#include<stdio.h>
int main()
{
int a,b,c;
clrscr();
printf("Enter three numbers");
scanf("%d %d %d", &a, &b, &c);
if (a>b)
{
if (a>c)
printf("%d is max",a);
else
printf("%d is max",c);
}
else
{
if (b>c)
printf("%d is max",b);
else
printf("%d is max",c);
}
getch();
return 0;
}

Natural Numbers taking out with the help of range: (Naturaln.c)

void main()
{
int i, start,end;
clrscr();
printf ("enter ur start and end range:");
scanf ("%d %d", &start,&end);
printf("Natural no(s) from %d to %d:\n", start,end);
for(i=start; i<=end; i++)
{
printf("%d\n", i);
}
getch();
}

Ranging square of numbers:- (Rangings.c)

void main()
{
int i,start,end;
clrscr();
printf("Enter start and end of range:");
scanf("%d %d", &start,&end);
printf("Square of numbers from %d to %d:\n",start,end);
for(i=start;i<=end;i++)
{
printf("%d^3=%d\n",i,i*i);
}
getch();
}

Reverse order Counting (Reversed.c)

void main()
{
int i,start,end;
clrscr();
printf ("enter ur range:");
scanf("%d %d", &start, &end);
printf ("Natural no(s) from %d to %d in reverse order: \n", start,end);
for(i=end;i>=start; i--)
{
printf("%d\n", i);
}
getch();
}

Simple Addition:- (Rivv.c)

#include<stdio.h>
void main()
{
int a,b,c;
clrscr();
printf("Enter value of a");
scanf("%d",&a);
printf("Enter value of b");
scanf("%d",&b);
c=a+b;
printf("%d",c);
getch();
}

Adding first digit and last two digit of three digit


number:-(Rizz.c)

#include<stdio.h>
void main()
{
int f,s,n,p;
clrscr();
printf("Enter a three digit number: ");
scanf("%d",&n);
if ((n>=100)&&(n<=999))
{
f=n/100;
printf("first digit: %d\n", f);
s=n%100;
printf("last two digit: %d\n",s);
p=f+s;
printf("sum of %d and %d is %d\n",f,s,p);
}
else
printf ("Enter valid number");
getch();
}

Electricity bill calculator:- (Sassy.c)

#include<stdio.h>
int main()
{
int unit;
float bill;
clrscr();
printf("Enter electricity unit:");
scanf ("%d",&unit);
if (unit<=50)
bill=unit*0.50;
else
if (unit<=150)
bill=(50×0.50)+((unit-50)*0.75);
else
if (unit<=250)
bill=(50*0.50)+(100*0.75)+((unit-150)*1.20);
else
bill=(50*0.50)+(100*0.75)+(100*1.20)+((unit-
250)*1.50);
printf("Electricity bill:Rs.%2f", bill);
getch();
}

int main()
{
int i,j;
clrscr();
for(j=1;j<=5; j++)
{
for (i=1;i<=5;i++)
{
printf("%d",i);
}
printf("\n");
}
getch();
return 0;
}

QUES) (Shinko.C)

OUTPUT:-
A
BB
CCC
DDDD
EEEEE

#include <stdio.h>
#include <locale.h>
int main()
{
int ch=65,i,j;
clrscr();
setlocale(LC_ALL, "C");
for(j=1;j<=5;j++)
{
for (i=1;i<=j;i++)
{
printf("%c", (char)ch);
}
printf("\n");
ch++;
}
getch();
return 0;
}

Fib Series (Shinku.c)

void main()
{
int ft=0,st=1,nt,n,i;
clrscr();
printf("Give a no.");
scanf ("%d",&n);
printf("%d %d",ft,st);
for(i=1;i<=n-2;i++)
{
nt=ft+st;
printf("%d",nt);
ft=st;
st=nt;
}
getch();
}

Ques) (Shinzo.c)

OUTPUT:-
1
12
123
1234
12345

#include<stdio.h>
int main()
{
int i,j;
clrscr();
for (j=1;j<=5;j++)
{
for(i=1;i<=j;i++)
{
printf("%d",i);
}
printf("\n");
}
getch();
}

Multiples of any number/Table

void main()
{
int n,i;
clrscr();
printf("Enter a number");
scanf("%d",&n);
i=1;
while(i<=10)
{
printf("%d*%d=%d\n",i,n,i*n);
i=i+1;
}
getch();
}

TABLE Calculator
(sizz.c)

void main()
{
int n,i;
clrscr();
printf("Enter a number");
scanf("%d",&n);
for(i=1;i<=10;i++);
{
printf("%d\n",i*n);
}
getch();
}

Squares
(squares.c)

void main()
{
int i;
clrscr();
printf("Squares of first ten numbers:");
for(i=1;i<=10;i++);
{
printf("%d^2=%d\n",i,i*i);
}
getch();
}

Quesn)
(Shinchan.c)
Output
12345
12345
12345
12345
12345
int main()
{
int i,j;
clrscr();
for(j=1;j<=5;j++)
{
for(i=1;i<=5;i++)
{
printf("%d",i);
}
}
printf("\n");
}
getch();
return 0;
}

Quesn)

Output

12345

1234

123

12
1

int main()
{
int i,j;
clrscr();
for(i=1;j<=5;i++)
{
for(j=1;j<=6-1;j++)
{
printf("%d",j);
}
}
printf("\n");
}
getch();
return 0;
}

QUES) (Shinnosu.c)

OUTPUT:-
A
AB
ABC
ABCD
ABCDE
ABCDEF

#include <stdio.h>
#include <locale.h>
#include <conio.h>
int main()
{
int i,j;
clrscr();
setlocale(LC_ALL, "C");
for(j=65;j<=70;j++)
{
for (i=65;i<=j;i++)
{
printf("%c", (char)i);
}
printf("\n");
}
getch();
return 0;
}

Quesn)
(sinsu.c)
OUTPUT:
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5

int main()
{
int i,j,k;
int rows =5;
clrscr();
for(i=1;i <= rows; i++)
{
for(k=1;k <=rows-i;k++)
{
printf(" ");
}
for(j=1;j <=i;j++)
{
printf("%d ", i);
}
printf("\n");
}
getch();
return 0;
}

Quesn)
(Suzumi.C)
OUTPUT:
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5

int main()
{
int i,j,k,sp =10:
clrscr();
for(i=1;i<=5; i++)
{
for(k=1;k<=sp;k++)
{
printf(" ");
}
sp=sp+2;
for(j=1;j<=i;j++)
{
printf("%d ", i);
}
printf("\n");
}
getch();
return 0;
}

Common code for factors and prime number (kk789.c)

void main()
{
int i,n,f=0;
clrscr();
printf("Enter a number:");
scanf("\n%d",&n);
for(i=1;i<=n;i++)
{
if (n%i==0)
{
printf("\n%d is a factor",i);
f++;
}
}
if (f ==2)
printf("\n%d is prime",n);
else
printf ("\ntotal factors are %d",f);
}
getch();
}
Sum of n natural numbers:- (DORAEMON.C)

void main()
{
int n,i,sum=0;
clrscr();
printf("Enter a positive number");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
sum=sum+i;
}
printf ("Sum of first %d natural numbers=%d\n",n,sum);
getch();
}

To Check given array are equal or not:- (Array.c)

void main()
{
int a[]={1,2,3,4,5};
int b[]={1,2,3,4,6);
int size=5;
for (i=0; i<size; i++)
{
if(a[i]!=b[i]) {
flag=0;
break;
}
}
if (flag==1)
{
printf("The arrays are equal.\n");
}
else
{
printf("The arrays are not equal.\n");
}
getch();
}

To Check any given array by user is equal or not:- (Array1.c)

void main()
int i, flag=1;
int a[5];
int b[5];
int size=5;
printf ("Enter 5 elements for the 1st array");
for(i=0;i<size;i++)
{
scanf("%d", &a[i]);
}
printf ("Enter 5 elements for the 2nd array");
for(i=0;i<size;i++)
{
scanf("%d", &b[i]);
}
for(i=0; i<size; i++)
{
if(a[i]!=b[i])
{
scanf("%d", &b[i]);
}
for (i=0; i<size; i++)
{
if (a[i]!=b[i])
{
flag=0;
break;
}
}
if (flag==1)
{
printf("The arrays are equal.\n");
}
else
{
printf("The arrays are not equal.\n");
}
getch();
}

To check if given element matches from the 10 elements of array input by user:-
(Doremi.c)

void main()
{
int n,i,a[10],flag=0;
clrscr();
printf("Enter 10 elements");
for(i=0;i<10;i++)
{
scanf("%d",&a[i]);
}
printf("Enter value of n");
scanf("%d",&n);
for(i=0;i<10;i++)
{
if (a[i]==n)
{
printf("%d element found at index %d",n,i);
flag=1;
break;
}// end of if
}// end of for
if (flag==0)
printf (2d not Found", n);
getch();
}

To check if all digits of a 3-digit number is unique or not:- (Hannah.c)

void main()
{
int n,d1,d2,d3;
clrscr();
printf("Enter a three digit number);
scanf ("%d",&n);
d1=n/100;
d2=n/10;
d2=d2%10;
d3=n%10;
if (d1!=d2 && d1!=d3 && d2!=d3)
{
printf("All digits are unique");
}
else
{
printf ("All digits are not unique"):
}
getch();
}

To check frequency of a element from the array:- (Hattori.c)

void main()
{
int n,i,a[10],count=0;
clrscr();
printf("Enter 10 elements");
for(i=0;i<10;i++)
{
scanf("%d",&a[i]);
}
printf("Enter value of n");
scanf("\n%d",&n);
for(i=0;i<10;i++)
{
if (a[i]==n)
{
count++;
}
}
printf("%d element Found %d times",n,count);
if (count==0)
printf ("%d not Found",n);
getch();
}

To check power of a number by asking user to input base and exponent:-


(Jish.c)

#include <stdio.h>
long power (int a, int b)
{
long result =1;
int i;
for(i=0;i<b;i++)
{
result=result*a;
}
}
int main()
{
int a,b;
clrscr();
printf("Enter base:");
scanf("%d",&a);
printf("Enter exponent:");
scanf("%d",&b);
printf(%d^%d=%ld\n",a,b,power(a,b));
getch();
return 0;
}

To check power of a number by asking user to input base and exponent:- (power.c)

#include<stdio.h>
#include<math.h>
int main()
{
int a,b;
clrscr();
printf("Enter base:");
scanf("%d",&a);
printf("Enter exponent:");
scanf("%d",&b);
printf(%d^%d=%0lf\n",a,b,pow(a,b));
getch();
return 0;
}

To check if eligible for loan by asking annual income and age:- (Khushi.c)
(khushu.c => Hello print)

include<stdio.h>
int main()
{
float a,b;
clrscr();
printf ("Enter your age");
scanf("%d",&a);
if (a>=18)
{
printf("Enter your annual income);
scanf("%d",&b);
if (b>=500000)
printf("Eligible for loan");
else
printf ("Not eligible for loan");
}
else
printf ("Not eligible for loan");
getch();
return 0;
}

To print reverse counting from a given range (kishu.c)

#include <stdio.h>
int main()
{
int n, i;
clrscr();
printf("Enter two no:");
scanf ("%d %d",&i,&n);
while (i>=n)
{
printf("%d\n", i);
i=i-1;
}
getch();
return 0;
}

To print sum of n terms (1,1/2,1/3,1/4,1/5_ _ _ _ n (kk101.c)

void main()
{
int n, i;
float sum=0.0;
printf (Enter the number of terms:);
scanf("%d",&n);
for(i=1;i<=n;i++)
{
sum=sum+1/(float)i;
}
printf (Sum of the series till %d terms is: %f\n,n,sum);
getch();
}

To print number in words:- (masao.c)

include <stdio.h>
char*units[]={"", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight",
"Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen",
"Seventeen", "Eighteen", "Nineteen"}; char*tens[]={"","", "Twenty", "Thirty",
"Forty", "Fifty", "Sixty", "Seventy", "Eight", "Ninety"};
char*thousands[]={"", "Thousand", "Lakh", "Crore"};
void printInWords (long long num)
{
if (num>= 10000000)
{// Crore
printInWords (num/10000000);
printf(" %s ", thousands[3]);
num% =10000000;
}
if (num>=1000)
{
printInWords(num/100000);
printf("%s ", thousands[2]);
num%=100000;
}
if (num>= 1000)
{
print InWords(num/1000);
printf("%s ", thousands[1]);
num%=1000;
}
if (num>=100)
{
printInWords(num/100);
printf (" Hundred ");
num%=100;
} if (num>=20)
{
printf("%s ",tens[num/10]);
num%=10;
}
if (num>0) { // Units (1-19)
{
printf(, units [num]);
}
}
}
int main()
{
long long num;
clrscr();
printf ("Enter a no. (b/w 0 to 99,99,99,999):");
scanf ("%lld", &num);
if (num<0||num>999999999)
{
printf (Plz enter a no. b/w 0 to 999999999.\n");
}
else
{
printf ("Number in words:");
if (num==0)
{
printf("zero");
}
else
{
printInWords(num);
}
printf("\n");
}
getch();
return 0;
}

To check the sum of all elements of an array (nanny.c)

void main()
{
int i,sum=0,a[10];
float avg;
clrscr();
printf ("Enter elements of array");
for(i=0;i<10;i++)
{
scanf("%d",&a[i]);
}
for(i=0;i<10;i++)
{
sum= sum+a[i];
}
printf( "sum of all elements is %d\n", sum);
avg=(float)sum/10;
printf("\navg of all elements is %3.2f \n", avg);
getch();
}

To check frequency of a digit:-


(Shizuka.C)

#include <stdio.h>
void countDigitFrequency (long num)
{
int frequency[10]={0};
int i;
if (num==0)
{
frequency[0]=1;
}
else
{
while(num>0)
{
int digit= num%10;
frequency[digit]++;
num/=10;
}
}
printf("Digit Frequency:\n");
for(i=0;i<10;i++)
{
if (frequency[i]>0)
{
printf("Digit %d: %d times\n",i,frequency[i]);
}
}
}
int main()
{
long num,i;
printf("Enter a number:");
scanf("%ld",&num);
if(num<0)
{
num=-num;
}
countDigitFrequency (num);
return 0;
}

To count No. of digits from a no. :- (Shwets.C)

#include<stdio.h>
int main()
{
long num,count=0;
clrscr();
printf("Enter a number:");
scanf("%ld",&num);
if(num==0)
{
count=1;
}
else{
if (num<0)
{
num= -num;
}
while (num>0)
{
num /= 10;
count++;
}
printf("Number of digits: %d\n",count);
getch();
} }

Quesn)

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

int main()
{
int i,j,k;
int rows =5;
clrscr();
for(i=1;i<=rows;i++)
{
for(k=1;k <=rows-i;k++)
{
printf(" ");
}
for(j=1;j<=i;j++)
{
printf("%d ",i);
}
printf("\n");
}
getch();
return 0;
}

To print sum of main diagonal,off diagonal and all elements of matrix:-

#include<stdio.h>
int main()
{
int i,j, a[3][3],sum=0,sum1=0,sum2=0;
printf("Enter elements of matrix\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",&a[i][j]);
}
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("%4d",a[i][j]);
}
printf("\n");
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
sum+=a[i][j];
}
}
printf("\nSum of all elements of matrix is %d\n",sum);
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
if (i==j)
sum1+=a[i][j];
}
}
printf("\nSum of main elements of diagonal is %d\n",sum1);
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
if (i+j==2)
sum2+=a[i][j];
}
}
printf("\nSum of elements off diagonal is %d",sum2);
return 0;
}

#include<stdio.h>
void odd_even(int a)
{
(a%2==0)?printf("%d is even",a):printf("%d is odd",a);

}
int sumofn(int n)
{
int sum=0;
int i;
for(i=1;i<=n;i++)
{
sum+=i;
}
return sum;
}
int main()
{
int s,n;
printf("Wlc Guys\nNow we gonna check odd/even and sum of n natural no.(s)\n");
printf("Enter a no. to check odd/even:");
scanf("%d",&n);
odd_even(n);
printf("\nEnter a positive integer for sum of n natural no:");
scanf("%d",&n);
s= sumofn(n);
printf("\nSum of natural numbers upto %d is %d",n,s);
return 0;
}

You might also like