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

C Vs Python

Uploaded by

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

C Vs Python

Uploaded by

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

Add two numbers

C Python
#include<stdio.h> a=int(input("enter a number="))
int main() b=int(input("enter another number="))
{ print("Total=",(a+b))
int a,b; -------------------------------------------
printf("Enter 2 numbers="); ------------------------------------------
scanf("%d %d", &a, &b); a, b = input("Enter two values: ").split()
printf("Total=%d", a+b); c=int(a)+int(b)
return 0; print("Total=",c)
}
#include<stdio.h> a=input("enter a number=")
int main() b=input("enter another number=")
{ c=float(a)+float(b)
float a,b; print("Total=",c)
printf("Enter 2 numbers="); ------------------------------------------
scanf("%f %f", &a,&b); a=input("enter a number=")
printf("Total=%f",a+b); b=input("enter another number=")
return 0; c=float(a)+float(b)
} s = "{:.2f}".format(c)
# printf("Total=%.2f",a+b); print("Total=",c)
Odd/Even

C Python
#include<stdio.h> a=int(input("Enter a number:"))
int main() if a % 2==0:
{ print("Even")
int a; else:
printf("Enter a number="); print("odd")
scanf("%d", &a);
if(a%2==0)
printf("Even");
else
printf("odd");
return 0;
}

#include<stdio.h> a=int(input("Enter a number:"))


int main() r = "Even" if a % 2==0 else "Odd"
{ print(r)
int a;
printf("Enter a number=");
scanf("%d", &a);
printf(a%2==0 ? "Even" : "Odd");
return 0;
}
Multiple if

C Python
#include<stdio.h> a=int(input("Enter age:"))
int main() if a>=60:
{ print("old")
int a; elif a>=40 and a<60:
printf("Enter ager="); print("Middle aged");
scanf("%d", &a); elif a>=20 and a<40:
if(a>=60) print("young");
printf("old"); elif a>=13 and a<20:
else if(a>=40 && a<60) print("Teenager");
printf("Middle aged"); else:
else if(a>=20 && a<40) print("Child");
printf("young");
else if(a>=13 && a<20)
printf("Teenager");
else
printf("Child");

return 0;
}
For loop

C Python
#include<stdio.h> for a in range(1,11):
int main() print(a, end=" ")
{ 1,2,3,4,5,6,7,8,9,10
int a;
for(a=1; a<=10;a++)
{
printf("%d ",a);
}
return 0;
}

#include<stdio.h> for a in range(1,11,2):


int main() print(a, end=" ")
{ 1,3,5,7,9
int a;
for(a=1; a<=10;a=a+2)
{
printf("%d ",a);
}
return 0;
}
While loop
C Python
#include<stdio.h> a=1
int main() while(a<=10):
{ print(a, end=" ")
int a=1; a=a+1
while(a<=10)
{
1,2,3,4,5,6,7,8,9,10
printf("%d ",a);
a=a+1;
}
return 0;
}
#include<stdio.h> a=1
int main() 1,2,3,4,5,6,7,8,9,10 while True:
{ print(a, end=" ")
int a=1; a=a+1
while(1) if a>10:
{ break
printf("%d ",a);
a=a+1;
if(a>10)
break;
}
return 0;
}
C Python
#include<stdio.h> for r in range(1,6):
int main() for c in range(1,r+1):
{ print(c, end="")
1

Nested loop(Series)
int r,c; print(" ")
for(r=1;r<=5;r++) 12
{ 123
for(c=1;c<=r; c++) 1234
{ 12345
printf("%d",c);
}
printf("\n");
}
return 0;
}
#include<stdio.h> g=5
int main() 1 for r in range(1,6):
{ 12 for s in range(1,g+1):
int r, c, g=5,s; 1 2 3 print("",end=" ");
for(r=1;r<=5;r++) { 12 3 4 for c in range(1,r+1):
for(s=1;s<=g;s++) 12 3 4 5 print(c,end=" ")
printf(" "); print(" ")
for(c=1;c<=r;c++) { g=g-1
printf("%d ",c);
} printf("\n");g=g-1;
} return 0; }
C Python
#include<stdio.h> g=1
int main() for r in range(5,0,-1):
{ for s in range(1,g+1):

Nested loop(Series)
int r,c,g=1,s; print("",end=" ");
for(r=5;r>=1;r--) for c in range(1,r+1):
{
1 2 3 4 5 print(c, end=" ")
for(s=1;s<=g; s++) 1 2 3 4 print(" ")
{ 1 2 3 g=g+1
printf(" "); 1 2
} 1
for(c=1;c<=r;c++)
{
printf("%d ",c);
}
printf("\n");
g=g+1;
}
return 0;
}
Enter a Letter. Print vowel or consonant

C Python

Character data type


#include<stdio.h> c=input("Enter a letter:")
#include <ctype.h> c=c.upper()
int main() if c=='A' or c=='E' or c=='I' or c=='O' or c=='U':
{ print("Vowel")
char c; else:
printf("Enter a letter="); print("consonant")
scanf("%c", &c);
c=toupper(c);
if(c=='A' || c=='E' || c=='I' || c=='O' || c=='U')
printf("Vowel");
else
printf("Consonant");
return 0;
}
Enter a Sentence. Print sentence, Lenth,
uppercase, lowercase, reverse

C Python
#include<stdio.h> c=input("Enter a sentence:")

String data type


#include <ctype.h> print("Length=",len(c))
#include <string.h> print("in capital=",c.upper())
int main() print("in small=",c.lower())
{ print("Reverse=",c[::-1])
char sn[30];
int L=0,i;
printf("Enter a sentence=");
scanf("%[^\n]", &sn);
printf("Length=%d\n", strlen(sn));
printf("in capital=%s\n",strupr(sn));
printf("in small=%s\n",strlwr(sn));
printf("in reverse=%s\n",strrev(sn));

return 0;
}
Enter first name & surname. Join & print full name

C Python
#include<stdio.h> fn=input("enter first name:")
#include <string.h> sn=input("enter surname:")
int main() print(fn+" "+sn)
{
char fn[100], sn[30];
printf("Enter first name=");
scanf("%[^\n]", &fn);
strcat(fn," ");
fflush(stdin);
printf("Enter title=");
scanf("%[^\n]", &sn);
fflush(stdin);

strcat(fn,sn);
printf("Full name=%s",fn);
return 0;
}
Compare strings

C Python
#include<stdio.h> w1=input("enter first word:")
#include <string.h> w2=input("enter second word:")
int main() if w1==w2:
{ print("Same")
char w1[20], w2[20]; else:
printf("Enter first word="); print("Different")
scanf("%[^\n]", &w1);
fflush(stdin);
printf("Enter 2nd word=");
scanf("%[^\n]", &w2);
fflush(stdin);

if(strcmp(w1,w2)==0)
printf("same");
else
printf("not same");
return 0;
}
C Python
#include<stdio.h> c=input("Enter a character:")
int main() Ascii Code print("Ascii code=",ord(c))
{
char c;
printf("Enter a character=");
scanf("%c", &c);
printf("Ascii code=%d",c);

return 0;
}

#include<stdio.h> c=int(input("Enter Ascii code:"))


int main() Character print("Character=",chr(c))
{
int c;
printf("Enter ascii code=");
scanf("%d", &c);
printf("Character value=%c",c);

return 0;
}
C [ Array ] Python [ List ]
#include<stdio.h> ar=[11,22,33,44,55]
int main() print(ar)
{
int i; Or
int ar[5]={11,22,33,44,55}; ar=[11,22,33,44,55]
for(i=0;i<=4;i++) for i in ar:
printf("%d\n",ar[i]); print(i)
return 0;
}
C [ Array ] Python [ List ]
#include <stdio.h> ar=[33,11,55,44,22]
int main() for i in ar:
{ print(i)
int i,j,t=0;
int ar[5]={11,55,33,22,44}; ar.sort();
for(i=0;i<4;i++) print(ar)
{ ---------------------------------------------------------------------
for(j=i+1;j<=4;j++) ar=[33,11,55,44,22]
{ for i in ar:
if(ar[i]>ar[j]) print(i)
{
t=ar[i]; ar.sort(reverse = True)
ar[i]=ar[j]; print(ar)
ar[j]=t;
}
}
}
for(i=0;i<=4;i++) for(i=4; i>=0; i--)
printf("%d ",ar[i]); printf("%d ",ar[i]);

return 0;
}
C [ Array ] Python [ List ]
#include <stdio.h> ar=[]
int main() n=int(input("How many nos ?"))
{ for i in range(0, n):
int i; ele = int(input())
int ar[5]; ar.append(ele)
for(i=0;i<=4;i++)
{ print(ar)
printf("Enter a number=");
scanf("%d", &ar[i]);
}
for(i=0;i<=4;i++)
printf("%d ",ar[i]);

return 0;
}
Function not returning any value

C Python
#include <stdio.h> def add(x,y):
void add(int,int); print("Total=",x+y)
int main()
{ a=int(input("Enter a number="))
int a,b; b=int(input("Enter another number="))
printf("Enter 2 numbers="); add(a,b)
scanf("%d %d", &a,&b);

add(a,b);
return 0;
}
void add(int x,int y)
{
printf("Total=%d",x+y);
}

W.a.p to add two numbers using a function.


Function returning a value

C Python
#include <stdio.h> def add(x,y):
int add(int,int); return x+y
int main()
{ a=int(input("Enter a number="))
int a,b,c=0; b=int(input("Enter another number="))
printf("Enter 2 numbers="); c=add(a,b)
scanf("%d %d", &a,&b); print("Total=",c)

c=add(a,b);
printf("Total=%d",c);
return 0;
}
int add(int x,int y)
{
return x+y;
}

You might also like