Swapping of 2 numbers Without 3rd Variable

This is a simple  Program in C++ / C for Swapping of 2 variable without third variable. This is a part of Mumbai University MCA Colleges. This type of questions are often asked in Interviews.


Swapping of 2 variables without 3rd Variable.


#include <iostream.h>
#include <conio.h>

 void main() 
int a=11,b=20; 
b=(a+b)-(a=b);      
cout<<"B is "<<b<<endl<<"A is "<<a; 
getch(); 
}

Here in this program, we use a single statement to swap the 2 numbers. 
in the statement b=(a+b)-(a=b);  we 1st add the 2 variable and then assign b to a, which is then subtracted from the sum of the 2 variable and assigned to b.

There are many more variations to swapping the variables without the 3rd variable.

Hope this Program is useful to you in some sense or other. Keep on following this blog for more Mumbai University MCA College Programs. Happy Programming and Studying.

C Program Count Words in Sentence

This C Program counts the number of Words in a Sentence. This is a part of Mumbai University MCA Colleges C Programs MCA Sem 1.
The words are counted by counting the character " " between 2 chars. 


#include<stdio.h>
#include<conio.h>
void main()
{
int len,i,cntword=0;
char sent[100];
clrscr();
printf("Enter a sentence\n");
gets(sent);
for(len=0;sent[len]!='\0';len++);
for(i=0;i<=len;i++)
{
if(sent[i]==' '||sent[i]=='\0'||sent[i]=='\t')
{
cntword++;
while(sent[i+1]==' '||sent[i+1]=='\0'||sent[i+1]=='\t')
i++;
}
}
printf("Number of word in a sentence are = %d",cntword);
getch();
}

Hope this Program is useful to you in some sense or other. Keep on following this blog for more Mumbai University MCA College Programs. Happy Programming and Studying.

C Program Remove Vowels From Sentence

This C Program Removes the Vowels from a sentence. This is a part of Mumbai University MCA Colleges C Program MCA Sem 1.
The Vowels are a,e,i,o,u


#include<stdio.h>
#include<conio.h>
void main()
{
int i,j,len;
char sent[100];
clrscr();
printf("Enter a sentence\n");
gets(sent);
for(len=0;sent[len]!='\0';len++);
for(i=0;i<len;i++)
{
if(sent[i]=='a'||sent[i]=='e'||sent[i]=='i'||sent[i]=='o'||sent[i]=='u'||
sent[i]=='A'||sent[i]=='E'||sent[i]=='I'||sent[i]=='O'||sent[i]=='U')
{
for(j=i;j<len;j++)
{
sent[j]=sent[j+1];
}
i--;
}
}
printf("\n\nThe sentence with no vowels in it is as\n");
puts(sent);
getch();
}


Hope this Program is useful to you in some sense or other. Keep on following this blog for more Mumbai University MCA College Programs. Happy Programming and Studying.

C Program Average,Mean,Sum of Array

This C Programs Finds the Average, Sum and Mean Deviation of Elements of Array. This is a part of Mumbai University MCA Colleges C Programs MCA Sem 1

#include<stdio.h>
#include<conio.h>
#include<math.h>
int arrsum(int *,int);
float average(int,int);
float deviation(int *,float,int);
void main()
{
int a[10],n,i,sum;
float avg,mean;
clrscr();
printf("Enter number of elements of an array (upto 10)\n");
scanf("%d",&n);
printf("Enter elements of an array\n");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
sum=arrsum(a,n);
avg=average(sum,n);
mean=deviation(a,avg,n);
printf("Sum = %d\nAverage = %f\nMean Deviation = %f\n",sum,avg,mean);
getch();
}
int arrsum(int *a,int n)
{
int i,s=0;
for(i=0;i<n;i++) 
{
s+=*(a+i);
}
return s;
}
float average(int s,int n)
{
float avg=0.0;
avg=(float)s/n;
return avg;
}
float deviation(int *a,float avg,int n)
{
int i;
float s=0.0,dev=0.0;
for(i=0;i<n;i++)
{
s+=((*(a+i)-avg)*(*(a+i)-avg));
}
dev=sqrt(s/n);
return dev;
}


Hope this Program is useful to you in some sense or other. Keep on following this blog for more Mumbai University MCA College Programs. Happy Programming and Studying.

Basic Salary C Program Flow Char


This C program to calculate the Basic Salary of a Employee with Flow Chart. This is a part of Mumbai University MCA Colleges C Program MCA Sem 1


A program to read basic from the user and calculate net and gross salary with flow chart. 

Where

Da=41% of basic salary,
Hra=30% of basic salary,
Pf=12% of basic salary,
It=300,
Cca=800.




#include<stdio.h>
#include<conio.h>

void main()
{
        /*variable declaration*/
        float basic_salary,da,hra,cca,pf,it,gross_salary,net_salary;
        clrscr();
        printf("\nEnter the basic salary of an employee: ");
        scanf("%f",&basic_salary);

        /*calculation*/
        da = basic_salary*41/100;
        hra =basic_salary *30/100;
        cca = 800;
        pf = basic_salary*12/100;
        it = 300;
        gross_salary=basic_salary+hra+cca+da;
        net_salary=gross_salary-pf-it;
        printf("\nGross Salary is %.2f",gross_salary);
        printf("\nNet Salary is %.2f",net_salary);
        getch(); 
}


Flow Chart


Flow Chart Basic Salary



Hope this Program is useful to you in some sense or other. Keep on following this blog for more Mumbai University MCA College Programs. Happy Programming and Studying.

C Program GCD and LCM MCA Sem 1

This C program finds the Greatest Common Divisor (GCD) and Least Common Multiple (LCM)of 2 given numbers. This is a part of Mumbai University MCA Colleges C Programs MCA Sem 1

#include<stdio.h>
#include<conio.h>
#include<math.h>

void main()
{
int a,b,c,n1,n2,gcd,lcd;
clrscr();
printf("\nEnter number 1:");
scanf("%d",&n1);
printf("\nEnter number 2:");
scanf("%d",&n2);
if(n1>n2)
{
a=n2;
b=n1;
}
else
{
a=n1;
b=n2;
}
while(a>0)
{
c=b%a;
if(c==0)
{
gcd=a;
break;
}
b=a;
a=c;
}
lcd=(n1*n2)/gcd;
printf("\nLCM is: %d",lcd);
printf("\nGCD is: %d",gcd);
getch();
}

Hope this Program is useful to you in some sense or other. Keep on following this blog for more Mumbai University MCA College Programs. Happy Programming and Studying.

C Program Celsius to Fahrenheit

This C program is for the conversion of Fahrenheit to Celsius and reverse. This is a part of Mumbai University MCA Colleges C programs MCA Sem 1.

#include<stdio.h>
#include<conio.h>
#include<math.h>

void main()
{
clrscr();
int a;
float f,c;
printf("To convert Fahrenheit into Celsius press 1\n");
printf("To convert Celsius into Fahrenheit press 2\n");
printf("Enter your choice:");
scanf("%d",&a);
switch(a)
{
case 1:
printf("Enter value in Fahrenheit:");
scanf("%f",&f);
c=(f-32)/1.8;
printf("%f Fahrenheit = %f Celsius",f,c);
break;

case 2:
printf("Enter value in Celsius:");
scanf("%f",&c);
f=(1.8*c)+32;
printf("%f Celsius = %f Fahrenheit",c,f);
break;
}
getch();
}

Hope this Program is useful to you in some sense or other. Keep on following this blog for more Mumbai University MCA College Programs. Happy Programming and Studying.

C Program File Handling/Copy MCA Sem 1

This is a C Programs which copies the content of one file to other (file1.txt to file2.txt). This is a part of Mumbai University MCA Colleges File Handling in C program MCA Sem 1

#include<stdio.h>
#include<conio.h>
#include<math.h>
void main(void)
{
FILE *in, *out;
char ch;
int flag;
clrscr();
if ((in = fopen("C:\\hello\\file1.txt", "rt"))
== NULL)
{
printf("Cannot open input file.\n");
getch();
return;
}

if ((out = fopen("C:\\hello\\file2.txt", "wt"))
== NULL)
{
printf("Cannot open output file.\n");
getch();
return;
}


while (!feof(in))
{
flag=1;
ch=fgetc(in);
ch=toupper(ch);
fputc(ch, out);
}
fclose(in);
fclose(out);
if (flag==1)
{
printf("The File copy is complete\n");
}
getch();
}

Hope this Program is useful to you in some sense or other. Keep on following this blog for more Mumbai University MCA College Programs. Happy Programming and Studying.

Largest of two Numbers without If-Else

This is a simple  Program in C++ for the Largest of 2 without If else statement. This is a part of Mumbai University MCA Colleges Cpp. It is also usually asked in Interviews.


Largest number without If-Else

#include <iostream.h>
int main()
{
float a=3,b=2;

float c=((a+b)/2) + ((a-b)/2);           //c=2.5+0.5=3.0

cout<<"The largest number of the two is "<<c;
return 0;
}

Here we use a simple logic to find the largest of two without using the If Else Statement.

Hope this Program is useful to you in some sense or other. Keep on following this blog for more Mumbai University MCA College Programs. Happy Programming and Studying.

Add without Addition Operator

This is a simple  Program in C to print sum or addition of two numbers without using Arithmetical operators. This is a part of Mumbai University MCA Colleges. It is mostly asked in Interviews.


Add without Arithmetic Operators

#include <stdio.h>
#include <iostream.h>
int main()
{
int a=7,b=5;
while(b--) // b will take value 5,4,3,2,1,0
a++;    // a will take values 7,8,9,10,11,12

cout<<a;
}

Here what we have done is that we have run a added the number 1 or incremented the value of variable by 1 and decremented the other by 1 till the decremented variable is 0.
The number of times the loop runs for 'b', that many times the value of a will increase.


Hope this Program is useful to you in some sense or other. Keep on following this blog for more Mumbai University MCA College Programs. Happy Programming and Studying.

C/C++ Program without Main() function

This is a simple  Program in C or C++ without the Main() function This is a part of Mumbai University MCA Colleges. This question is often asked in interviews.


Program without Main()

#include<stdio.h>
#include<conio.h>

#define  main  my

void  my()
{
    printf("Programs and notes for mca");
}

What we have done here is that we have used the #define directive my as 'main'.
So where ever we need to use the main keyword we replace it with 'my'.



Hope this Program is useful to you in some sense or other. Keep on following this blog for more Mumbai University MCA College Programs. Happy Programming and Studying.

Floyds Triangle C Program


This is a C program for generating a Floyd's Triangle
This is a part of Mumbai University MCA College C program MCA Sem 1



Floyd’s triangle

#include<stdio.h>
#include<conio.h>
void main()
{
        int i , j , r ;
        int num=0 ;
        clrscr();
        printf(“How many rows you want in the triangle:”);
        scanf(“%d”,&r);
        for(i=1;i<=r;i++)
        {
                for(j=1;j<=i;j++)
                {
                        printf(“%d”,++num);
                }
                printf(“\n”); 
}
getch();

}


Input:

How many rows you want in the triangle: 4

Output:

1
2 3
4 5 6
7 8 9 10 



Hope this Program is useful to you in some sense or other. Keep on following this blog for more Mumbai University MCA College Programs. Happy Programming and Studying.

Swapping without 3rd Variable Bitwise

This is a simple  Program for Swapping of 2 Numbers with Bitwise Operators. This is a part of Mumbai University MCA Colleges . It is a Interview Question and Program.




Swapping of 2 numbers with Bitwise operator (without 3rd Variable).


#include <stdio.h>
#include <iostream.h>
int main()
{
int a=11;
int b=20;
b=a^b;
a=a^b;
b=a^b;
printf("%d %d",a,b);
return 0;
}


This Program can also be written as

#include <stdio.h>
#include <iostream.h>
int main()
{
int a=11;
int b=20;
b^=a;
a^=b;
b^=a;
printf("%d %d",a,b);
return 0;
}

The advantage of using bitwise operator program over normal arithmetic program  as follows
b=b+a;
a=b-a;
b=b-a;

is that the above code will lead to overflow i.e if your using a int data (2 Bytes) type then swapping a number 65,535 with other number 65,535 will lead to overflow and will return the garbage value in b=b+a;

The bitwise operators works on the bit level and hence doesnt face the problem of overflow.

We use the bitwise property of XOR gate to generate the desired result.
XOR Gate produces the same number if XORed Twice.

Swapping Numbers without 3rd Variable

This is a simple  Program for Swapping of Variable without 3rd Variable. This is a part of Mumbai University MCA Colleges Cpp. It is also usually asked in Interviews.




Swapping of Variable without 3rd Variable


#include <stdio.h>
#include <iostream.h>
int main()
{
int a=11;
int b=20;
b=(a+b);                 // a = 11      b=31
a=(b-a);                  // a = 20      b=31
b=(b-a);                 //  a = 20      b=11
printf("%d %d",a,b);
return 0;
}

Hope this Program is useful to you in some sense or other. Keep on following this blog for more Mumbai University MCA College Programs. Happy Programming and Studying.

Even Odd without Mod Operator

This is a simple  Program in C++ for finding if a given number is Even or Odd without Mod Operator. This is a part of Mumbai University MCA Colleges. This is a Interview Question and Program

Even Odd with out Mod operator


#include <stdio.h>
#include <string.h>
int main()
{
int a=8;
( a & 1 == 1)? printf(" odd number\n") : printf(" even number \n");
return 0;
}


In this program, I have used the bitwise AND operator for checking the last bit of the number. In binary form, if the number has the last bit set to 1 then its a Odd number and if the last bit is set to 0 then its a even number.
Using a simple AND Operation, we find the last bit and compare if its 1. 
Using conditional operator the code is reduced to single line.



Hope this Program is useful to you in some sense or other. Keep on following this blog for more Mumbai University MCA College Programs. Happy Programming and Studying.

Even Odd without If Else Statement

This is a simple  Program in C++ for finding if a given number is Even or Odd without IF. This is a part of Mumbai University MCA Colleges. This is a interview Question and Program


Even Odd without IF

This program will help you find even or a odd number without using the if statement.
This program is mostly asked in interviews and should be taken seriously.

#include <stdio.h>
#include <string.h>
int main()
{
string show[] = {"even", "odd"};
int a=6;
cout<<"the number "<<a<<" is "<<show[a%2];
return 0;
}

As you can see I have just used the String array to store the output. The array starts with index 0.
When I am Displaying Show[a%2], the output will be either 1 or 0. If its Zero then we need to print Even and if its 1 then Odd.
Using a simple logic we can get the even odd without if.


Hope this Program is useful to you in some sense or other. Keep on following this blog for more Mumbai University MCA College Programs. Happy Programming and Studying.

Program Without Semicolon " ; "

This is a simple  Program in C to print output without using a semi-colon " ; ". This is a part of Mumbai University MCA Colleges. It is mostly asked in Interviews.


Print without Semi-Colon ";"

Method 1 :-

#include<stdio.h>
#include<conio.h>

void main()
{
 if(printf("Programs and notes for mca!"))
{ }
}

In this program we use the function printf() with in the if statement to produce the output without ";".
The printf() function will return the length of the string within the if and will get executed.

Points to note here is that we have written a output statement within if.
This same logic can be used for other programming languages as well.


Method 2 :-

#include <stdio.h>
#include<conio.h>

#define m ;
void main()
{
clrscr()m
printf("Programs and notes for mca")m
}

How this isnt really the solution. 
What we have done here is that we have used the #define directive to represent the semicolon as "a".
So where ever we have to use a semicolon we can replace it with a.

Hope this Program is useful to you in some sense or other. Keep on following this blog for more Mumbai University MCA College Programs. Happy Programming and Studying.

Transpose of Matrix C Program


This is a C program for Printing the Transpose of a Matrix.
This is a part of Mumbai University MCA College C program MCA Sem 1


Transpose of a Matrix.

#include<stdio.h>
#include<conio.h>
void main()
{
            /*declarations*/
            int arr[3][3],i,j;
            clrscr();

            /*accept elements for the array*/
            printf("Enter elements for the array \n");
            for(i=0;i<=2;i++)
            {
                        for(j=0;j<=2;j++)
                        {
                                    scanf("%d",&arr[i][j]);
                        }
            }

            /*print the original array entered by the user*/
            printf("Original array entered by the user is \n");
            for(i=0;i<=2;i++)
            {
                        for(j=0;j<=2;j++)
                        {
                                    printf("%d ",arr[i][j]);
                        }
                        printf("\n");
            }

            /*print the transpose of the array*/
            printf("transpose of the array is \n");
            for(i=0;i<=2;i++)
            {
                        for(j=0;j<=2;j++)
                        {
                                    printf("%d ",arr[j][i]);
                        }
                        printf("\n");
            }

            getch();
}


Output:

Original array entered by the user is
1 2 3
4 5 6
7 8 9

transpose of the array is
1 4 7
2 5 8
3 6 9






Hope this Program is useful to you in some sense or other. Keep on following this blog for more Mumbai University MCA College Programs. Happy Programming and Studying.

Adding Digits in a number to reduce it to Single digit


This is a C program for recursively adding a number until it becomes a single digit.
This is a part of Mumbai University MCA College C program MCA Sem 1



Adding and Reducing number to Single Digit


#include<stdio.h>
#include<conio.h>
void main()
{
int n,sum=10,n1;
clrscr();
printf("Enter a number :-");
scanf("%d",&n);
while(sum>=10)
{
sum=0;
while(n!=0)
{
n1=n%10;
sum=sum+n1;
n=n/10;
}
n=sum;
}
printf("Sum of digits :- %d ",sum);
}



Intput :

Enter a number :- 179

Output :

Sum of digits :- 8

Flow Chart 

Adding Number till Single Digit



Hope this Program is useful to you in some sense or other. Keep on following this blog for more Mumbai University MCA College Programs. Happy Programming and Studying.