Mannu C Language File
Mannu C Language File
#include<stdio.h>
#include<conio.h>
void int main()( )
{
{
int n,r,a,b,c;
int sum=0,average;
clrscr();
printf("ENTER A POSITIVE INTEGER \nEnter any three integers: ");
scanf("%d",&n%d %d",&a,&b,&c);
while(n!=0)
{
r=n%10;
sum = a+b+c;
average=sum+r/3;
n=n/10;
}
printf("THE SUMOF INDIVIDUAL DIGITS OF A POSITIVE INTEGER IS..%Sum and average of three
integers: %d \t%d",sum,average);
getch();
}
OUTPUT:
ENTER A POSITIVE INTEGER
return 0;
}
Output
Enter any three integer: 3
4
5
HE SUM OF INDIVIDUAL DIGITS OF A POSITIVE INTEGER IS..11Sum and average of three integer: 12
4
321
Program for a simple “for” loop
#include <stdio.h>
int main() {
int i;
for(i=0;i<6;i++)//simple use of for loop
printf("%d\n",i);
return 0;
}
OUTPUT
0
1
2
3
4
5
PROGRAM FOR SIMPLE “WHILE” LOOP
#include <stdio.h>
int main() {
int i;
while(i<8){//simple use of while loop
printf("%d\n",i);
i++;
}
return 0;
}
OUTPUT
0
1
2
3
4
5
6
7
PROGRAM FOR SIMPLE “DO-WHILE” LOOP
#include <stdio.h>
int main() {
int i=0;
do{
printf("%d\n",i);
i++;
}
while(i<4);//simple use of do while loop
return 0;
}
OUTPUT
0
1
2
3
PROGRAM FOR PRINT AN ARRAY
#include <stdio.h>
int main() {
int arr[]={1,2,3,4,5,6,7,8,9};
int i;
for(i=0;i<9;i++)//loop for print the array
printf("%d,",arr[i]);
return 0;
}
OUTPUT
1,2,3,4,5,6,7,8,9,
PROGRAM FOR REVERSE THE ELEMENT OF AN ARRAY
#include<stdio.h>
#include<conio.h>
int main()
{
int arr[10], i;
printf("Enter any 5 elements: ");
for(i=0; i<5; i++)
{
scanf("%d", &arr[i]);
}
printf("\nThe array elements in reverse order:\n");
for(i=4; i>=0; i--)
{
if(i==0)
printf("%d", arr[i]);
else
printf("%d, ", arr[i]);
}
getch();
return 0;
}
OUTPUT
Enter any 5 elements: 1 2 3 4 5
The array elements in reverse order: 5 4 3 2 1
Program to nested “for“ loop
#include <stdio.h>
int main()
{
int n;
printf("Enter the value of n :");
scanf("%d",&n);
Output
Enter the value of n :5
1 2 3 4 5 6 7 8 9 10
2 4 6 8 10 12 14 16 18 20
3 6 9 12 15 18 21 24 27 30
4 8 12 16 20 24 28 32 36 40
5 10 15 20 25 30 35 40 45 50 T
Program for infinite loop
#include <stdio.h>
int main()
{
for(;;)
{
printf("Hello world");
}
return 0;
}
Output
Hello worldHello worldHello worldHello worldHello worldHello worldHello worldHello
worldHello worldHello worldHello worldHello worldHello worldHello worldHello
worldHello worldHello worldHello worldHello worldHello worldHello worldHello
worldHello worldHello worldHello worldHello worldHello worldHello worldHello
worldHello worldHello worldHello worldHello worldHello worldHello worldHello
worldHello worldHello worldHello worldHello worldHello worldHello worldHello
worldHello worldHello worldHello worldHello worldHello worldHello worldHello
worldHello worldHello worldHello worldHello worldHello worldHello worldHello
worldHello worldHello worldHello worldHello worldHello worldHello worldHello
worldHello worldHello worldHello worldHello worldHello worldHello worldHello
worldHello worldHello worldHello worldHello worldHello
wo…………………………………………………………………………………….
Program for “continue” statement.
#include<stdio.h>
int main(){
int i=1;
//starting a loop from 1 to 10
for(i=1;i<=8;i++){
if(i==5){//if value of i is equal to 5, it will continue the loop
continue;
}
printf("%d \n",i);
}
return 0;
}
OUTPUT
1
2
3
4
6
7
8
Program for “break” statement
#include<stdio.h>
void main ()
{
int i;
for(i = 0; i<10; i++)
{
printf("%d ",i);
if(i == 5)
break;
}
printf("\ncames outside of loop when it comes to i = %d",i);
OUTPUT
012345
cames outside of loop when it comes to i = 5
Program to present a function .
#include<stdio.h>
void printName()
{
printf(" mca class");
}
void main ()
{
printf("Hello ");
printName();
}
OUTPUT
Hello mca class
Program to “call by value” a function .
#include <stdio.h>
void swap (int a, int b)
{
int temp;
temp = a;
a=b;
b=temp;
printf("After swapping the values a = %d, b = %d\n",a,b);
}
int main()
{
int a = 10;
int b = 20;
printf("Before swapping the values = %d, b = %d\n",a,b);
swap(a,b);
}
OUTPUT
Before swapping the values = 10, b = 20
After swapping the values a = 20, b = 10
Program to call by “refrence” in function.
#include <stdio.h>
void swap (int *a, int *b)
{
int temp;
temp = *a;
*a=*b;
*b=temp;
printf("After swapping values in function = %d, b = %d\n",*a,*b);
}
int main()
{
int a = 10;
int b = 20;
printf("Before swapping the values = %d, b = %d\n",a,b);
swap(&a,&b);
printf("After swapping values in main function = %d, b = %d\
n",a,b);
return 0;
}
OUTPUT
Before swapping the values = 10, b = 20
After swapping values in function = 20, b = 10
After swapping values in main function = 20, b = 10
Program for 2-d array
#include<stdio.h>
int main(){
int i=0,j=0;
int arr[4][3]={{1,2,3},{2,3,4},{3,4,5},{4,5,6}};
for(i=0;i<4;i++){
for(j=0;j<3;j++){
printf("arr[%d] [%d] = %d \n",i,j,arr[i][j]);
}
}
return 0;
}
OUTPUT
arr[0] [0] = 1
arr[0] [1] = 2
arr[0] [2] = 3
arr[1] [0] = 2
arr[1] [1] = 3
arr[1] [2] = 4
arr[2] [0] = 3
arr[2] [1] = 4
arr[2] [2] = 5
arr[3] [0] = 4
arr[3] [1] = 5
arr[3] [2] = 6
Program for return an array.
#include <stdio.h>
void getarray(int arr[])
{
printf("Elements of array are : ");
for(int i=0;i<5;i++)
{
printf("%d ", arr[i]);
}
}
int main()
{
int arr[5]={45,67,34,78,90};
getarray(arr);
return 0;
}
OUTPUT
Elements of array are : 45 67 34 78 90
Program for represent a pointer.
#include<stdio.h>
int main(){
int number=50;
int *pt;
pt=&number;
printf("Address of pt variable is %x \n",pt);
printf("Value of pt variable is %d \n",*pt);
return 0;
}
OUTPUT
Address of pt variable is d7081564
Value of pt variable is 50
Program for POINTER TO POINTER.
#include<stdio.h>
void main ()
{
int a = 10;
int *p;
int **pp;
p = &a;
pp = &p;
printf("address of a: %x\n",p);
printf("address of p: %x\n",pp);
printf("value stored at p: %d\n",*p);
printf("value stored at pp: %d\n",**pp);
}
OUTPUT
address of a: 26b48274
address of p: 26b48268
value stored at p: 10
value stored at pp: 10
Program for “sizeof”.
#include <stdio.h>
int main()
{
int x=89;
printf("size of the variable x is %d bytes", sizeof(x));
printf("\nsize of the integer data type is %d
bytes",sizeof(int));
printf("\nsize of the character data type is %d
bytes",sizeof(char));
printf("\nsize of the floating data type is %d
bytes",sizeof(float));
return 0;
}
OUTPUT
size of the variable x is 4 bytes
size of the integer data type is 4 bytes
size of the character data type is 1 bytes
size of the floating data type is 4 bytes
PROGRAM TO PRINT A STRING.
#include<stdio.h>
#include <string.h>
int main(){
char ch[12]={'R', 'B', 'S', ' ', 'C', 'O', 'L', 'L', 'E', 'G','E','\0'};
char ch2[11]="MCA CLASS";
#include<stdio.h>
#include <string.h>
int main(){
char ch[20]={'R', 'B', 'S', 'C', 'O', 'L', 'L', 'E', 'G', 'E', '\0'};
printf("Length of string is: %d",strlen(ch));
return 0;
}
OUTPUT
Length of string is: 10
PROGRAM FOR COPY A STRING. #include<stdio.h>
#include <string.h>
int main(){
char ch[20]={'R', 'B', 'S', 'C', 'O', 'L', 'L', 'E', 'G', 'E', '\0'};
char ch2[20];
strcpy(ch2,ch);
printf("Value of second string is: %s",ch2);
return 0;
}
OUTPUT
Value of second string is: RBSCOLLEGE
Program for Strcat() function.
#include<stdio.h>
#include <string.h>
int main(){
char ch[10]={'h', 'e', 'l', 'l', 'o', '\0'};
char ch2[10]={'M', '\0'};
strcat(ch,ch2);
printf("Value of first string is: %s",ch);
return 0;
}
OUTPUT
Value of first string is: helloM