SlideShare a Scribd company logo
1
Assignment #4
Subject: Programming Fundamentals
Semester: 1st
Submitted To: Sir. Junaid
Submitted By: Zohaib Zeeshan
Roll No: BSSE-F17-57
Date: 22/01/2018
Department of CS&IT
(BSSE)
University Of Sargodha Mandi Bahauddin Campus
2
Programming Fundamentals
(1). Printing text on screen:
a). Write a simple C program.
Code:
#include<stdio.h>
int main(void)
{
printf(“Welcome to C!”);
return 0;
}
Output:
b). Write a program to find sum of two integers.
Code:
#include<stdio.h>
int main()
{
int a=5;
int b=7;
int sum;
sum = a+b;
printf("The sum is = %d", sum);
}
Output:
(2). Write two code examples of if-else.
a). Find maximum between two numbers.
Code:
#include<stdio.h>
int main()
{
int num1, num2;
printf("Enter two integers to find which is maximumn");
scanf("%d%d", &num1, &num2);
if(num1 > num2){
printf("First numbers is maximumn");
}
else
{
printf("Second is maximumn");
}
return 0;
}
3
Output:
b). Write a program to check an integer is evenor odd.
Code:
#include<stdio.h>
int main()
{
int num;
printf("Enter an integer to check even or oddn");
scanf("%d", &num);
if(num % 2 == 0)
{
printf("Evenn");
}
else
{
printf("Oddn");
}
}
Output:
(3). Write two code examples of switch statement.
a). Write a code to check an alphabet is vowel or consonant.
Code:
#include<stdio.h>
int main ()
{
char ch;
printf("Enter an alphabetn");
scanf("%c", &ch);
switch (ch)
{
case'a':
printf("a is voweln");
break;
case 'e':
printf("e is voweln");
break;
4
case 'i':
printf("i is voweln");
break;
case 'o':
printf("o is voweln");
break;
case 'u':
printf("u is woweln");
break;
case 'A':
printf("A is voweln");
break;
case 'E':
printf("E is voweln");
break;
case 'I':
printf("I is voweln");
break;
case 'O':
printf("O is voweln");
break;
case 'U':
printf("U is voweln");
break;
default:
printf("is consonant");
}
return 0;
}
Output:
b). Write a code to check number is evenor odd.
Code:
#include<stdio.h>
int main()
{
int num;
printf("Enter a number to check even or oddn");
scanf("%d", &num);
switch(num % 2){
5
case 0:
printf("Number is Evenn");
break;
case 1:
printf("Number is Oddn");
break;
}
}
Output:
(4). Write two code examples of For Loop.
a). C program to find power ofa number using for loop.
Code:
#include<stdio.h>
int main(){
int base,exponent;
int power = 1;
int i;
printf("Enter base: n");
scanf("%d", &base);
printf("Enter exponenet: n");
scanf("%d", &exponent);
for(i=1; i<=exponent; i++){
power=power*base;
}
printf("%d ^ %d = %d", base, exponent, power);
return 0;
}
Output:
b). C program to print all even numbers from 1 to n.
Code:
#include<stdio.h>
int main(){
int i,n;
printf("Print all even numbers: n");
scanf("%d", &n);
printf("Even numbers from 1 to %d are n", n);
6
for(i=1; i<=n; i++){
if(i%2 == 0){
printf("%d ", i);
}
}
return 0;
}
Output:
(5). Write two code examples using while loop.
a). C program to print multiplication table ofa number using while loop.
Code:
#include <stdio.h>
int main()
{
int i, num;
printf("Enter number to print table: ");
scanf("%d", &num);
while(i <=10)
{
printf("%d * %d = %dn", num, i, (num*i));
i++;
}
return 0;
}
Output:
b). Write a program to genrate star pattern as shown below using while loop.
Code:
#include<stdio.h>
int main()
{
int i,j;
7
i=1;
while(i<=5){
printf("");
j=1;
while(j<=i)
{
printf("*");
j++;
}
printf("n");
i++;
}
return 0;
}
Output:
(6).Write two code examples using do while loop.
a). Value of a using do while loop.
Code:
#include <stdio.h>
int main(){
int a = 0;
// do loop execution
do {
printf("value of a: %dn", a);
a++;
}
while( a <= 5 );
return 0;
}
Output:
b). C program to print the table of 5 from 1 to 10.
Code:
#include<stdio.h>
int main()
{
int i=1;
do
{
printf("5 * %d = %dn",i,5*i);
8
i++;
}
while(i<=10);
return 0;
}
Output:
(7). Write two code examples of Functions.
a). C program to find cube ofa number using function.
Code:
#include <stdio.h>
/* Function declaration */
int cube(int num);
int main(){
int num;
int c;
printf("Enter any number: ");
scanf("%d", &num);
c = cube(num);
printf("Cube of %d is %d", num, c);
return 0;
}
int cube(int num)
{
return (num * num * num);
}
Output:
b). Find factorial of a number using function.
Code:
#include<stdio.h>
int factorial(int);
int main(){
int fact;
int numbr;
printf("Enter a number: ");
scanf("%d",&numbr);
9
fact= factorial(numbr);
printf("Factorial of %d is: %d",numbr,fact);
return 0;
}
int factorial(int n){
int i;
int factorial;
factorial =1;
for(i=1;i<=n;i++)
factorial=factorial*i;
return(factorial);
}
Output:
(8). Write two code examples of Array.
a). Write a program to find repeated elements using array.
Code:
#include<stdio.h>
int main(){
int i,arr[20],j,num;
printf("Enter size of array: ");
scanf("%d",&num);
printf("Enter any %d elements in array: ",num);
for(i=0;i<num;i++)
{
scanf("%d",&arr[i]);
}
printf("Repeated elements are: n");
for(i=0; i<num; i++)
{
for(j=i+1;j<num;j++)
{
if(arr[i]==arr[j])
{
printf("%dn",arr[i]);
}
}
}
return 0;
}
Output:
10
b). Find largest element using array.
Code:
#include <stdio.h>
int main()
{
int array[50], size, i, largest;
printf("Enter the size of the array: n");
scanf("%d", &size);
printf("Enter %d elements of the array: n", size);
for(i=0; i<size; i++){
scanf("%d", &array[i]);}
largest = array[0];
for (i = 1; i < size; i++)
{
if (largest < array[i])
largest = array[i];
}
printf("The largest element is : %dn", largest);
return 0;
}
Output:

More Related Content

What's hot

88 c-programs
88 c-programs88 c-programs
88 c-programs
Leandro Schenone
 
3. user input and some basic problem
3. user input and some basic problem3. user input and some basic problem
3. user input and some basic problem
Alamgir Hossain
 
Lab. Programs in C
Lab. Programs in CLab. Programs in C
Lab. Programs in C
Saket Pathak
 
Programming egs
Programming egs Programming egs
Programming egs
Dr.Subha Krishna
 
2. introduction of a c program
2. introduction of a c program2. introduction of a c program
2. introduction of a c program
Alamgir Hossain
 
Expressions using operator in c
Expressions using operator in cExpressions using operator in c
Expressions using operator in c
Saranya saran
 
C important questions
C important questionsC important questions
C important questions
JYOTI RANJAN PAL
 
C Programming
C ProgrammingC Programming
C Programming
Sumant Diwakar
 
Programming with c language practical manual
Programming with c language practical manualProgramming with c language practical manual
Programming with c language practical manual
Anil Bishnoi
 
Testing lecture after lec 4
Testing lecture after lec 4Testing lecture after lec 4
Testing lecture after lec 4emailharmeet
 
Chapter 5 exercises Balagurusamy Programming ANSI in c
Chapter 5 exercises Balagurusamy Programming ANSI  in cChapter 5 exercises Balagurusamy Programming ANSI  in c
Chapter 5 exercises Balagurusamy Programming ANSI in c
BUBT
 
B.Com 1year Lab programs
B.Com 1year Lab programsB.Com 1year Lab programs
B.Com 1year Lab programs
Prasadu Peddi
 
Decision making and branching
Decision making and branchingDecision making and branching
Decision making and branching
Saranya saran
 
Chapter 4 : Balagurusamy Programming ANSI in C
Chapter 4 : Balagurusamy Programming ANSI in CChapter 4 : Balagurusamy Programming ANSI in C
Chapter 4 : Balagurusamy Programming ANSI in C
BUBT
 
C programs
C programsC programs
C programsMinu S
 
Dam31303 dti2143 lab sheet 7
Dam31303 dti2143 lab sheet 7Dam31303 dti2143 lab sheet 7
Dam31303 dti2143 lab sheet 7alish sha
 
C language questions_answers_explanation
C language questions_answers_explanationC language questions_answers_explanation
C language questions_answers_explanation
srinath v
 
CP Handout#7
CP Handout#7CP Handout#7
CP Handout#7
trupti1976
 
USER DEFINE FUNCTIONS IN PYTHON[WITH PARAMETERS]
USER DEFINE FUNCTIONS IN PYTHON[WITH PARAMETERS]USER DEFINE FUNCTIONS IN PYTHON[WITH PARAMETERS]
USER DEFINE FUNCTIONS IN PYTHON[WITH PARAMETERS]
vikram mahendra
 

What's hot (20)

Practical no 6
Practical no 6Practical no 6
Practical no 6
 
88 c-programs
88 c-programs88 c-programs
88 c-programs
 
3. user input and some basic problem
3. user input and some basic problem3. user input and some basic problem
3. user input and some basic problem
 
Lab. Programs in C
Lab. Programs in CLab. Programs in C
Lab. Programs in C
 
Programming egs
Programming egs Programming egs
Programming egs
 
2. introduction of a c program
2. introduction of a c program2. introduction of a c program
2. introduction of a c program
 
Expressions using operator in c
Expressions using operator in cExpressions using operator in c
Expressions using operator in c
 
C important questions
C important questionsC important questions
C important questions
 
C Programming
C ProgrammingC Programming
C Programming
 
Programming with c language practical manual
Programming with c language practical manualProgramming with c language practical manual
Programming with c language practical manual
 
Testing lecture after lec 4
Testing lecture after lec 4Testing lecture after lec 4
Testing lecture after lec 4
 
Chapter 5 exercises Balagurusamy Programming ANSI in c
Chapter 5 exercises Balagurusamy Programming ANSI  in cChapter 5 exercises Balagurusamy Programming ANSI  in c
Chapter 5 exercises Balagurusamy Programming ANSI in c
 
B.Com 1year Lab programs
B.Com 1year Lab programsB.Com 1year Lab programs
B.Com 1year Lab programs
 
Decision making and branching
Decision making and branchingDecision making and branching
Decision making and branching
 
Chapter 4 : Balagurusamy Programming ANSI in C
Chapter 4 : Balagurusamy Programming ANSI in CChapter 4 : Balagurusamy Programming ANSI in C
Chapter 4 : Balagurusamy Programming ANSI in C
 
C programs
C programsC programs
C programs
 
Dam31303 dti2143 lab sheet 7
Dam31303 dti2143 lab sheet 7Dam31303 dti2143 lab sheet 7
Dam31303 dti2143 lab sheet 7
 
C language questions_answers_explanation
C language questions_answers_explanationC language questions_answers_explanation
C language questions_answers_explanation
 
CP Handout#7
CP Handout#7CP Handout#7
CP Handout#7
 
USER DEFINE FUNCTIONS IN PYTHON[WITH PARAMETERS]
USER DEFINE FUNCTIONS IN PYTHON[WITH PARAMETERS]USER DEFINE FUNCTIONS IN PYTHON[WITH PARAMETERS]
USER DEFINE FUNCTIONS IN PYTHON[WITH PARAMETERS]
 

Similar to Programming fundamentals

C lab
C labC lab
C Programming Example
C Programming ExampleC Programming Example
C Programming Example
PRATHAMESH DESHPANDE
 
C Programming
C ProgrammingC Programming
C Programming
Sumant Diwakar
 
Common problems solving using c
Common problems solving using cCommon problems solving using c
Common problems solving using c
ArghodeepPaul
 
Practical write a c program to reverse a given number
Practical write a c program to reverse a given numberPractical write a c program to reverse a given number
Practical write a c program to reverse a given number
Mainak Sasmal
 
Practical write a c program to reverse a given number
Practical write a c program to reverse a given numberPractical write a c program to reverse a given number
Practical write a c program to reverse a given number
Mainak Sasmal
 
Itp practical file_1-year
Itp practical file_1-yearItp practical file_1-year
Itp practical file_1-year
AMIT SINGH
 
Best C Programming Solution
Best C Programming SolutionBest C Programming Solution
Best C Programming Solution
yogini sharma
 
C file
C fileC file
Linux_C_LabBasics.ppt
Linux_C_LabBasics.pptLinux_C_LabBasics.ppt
Linux_C_LabBasics.ppt
CharuJain396881
 
In C Programming create a program that converts a number from decimal.docx
In C Programming create a program that converts a number from decimal.docxIn C Programming create a program that converts a number from decimal.docx
In C Programming create a program that converts a number from decimal.docx
tristans3
 
C lab programs
C lab programsC lab programs
C lab programs
Dr. Prashant Vats
 
C lab programs
C lab programsC lab programs
C lab programs
Dr. Prashant Vats
 
C Language Programs
C Language Programs C Language Programs
C Language Programs
Mansi Tyagi
 
Unit-IV.pptx
Unit-IV.pptxUnit-IV.pptx
Unit-IV.pptx
Mehul Desai
 
'C' language notes (a.p)
'C' language notes (a.p)'C' language notes (a.p)
'C' language notes (a.p)
Ashishchinu
 
C programs
C programsC programs
C programs
Vikram Nandini
 

Similar to Programming fundamentals (20)

C lab
C labC lab
C lab
 
C Programming Example
C Programming ExampleC Programming Example
C Programming Example
 
C Programming
C ProgrammingC Programming
C Programming
 
Common problems solving using c
Common problems solving using cCommon problems solving using c
Common problems solving using c
 
Practical write a c program to reverse a given number
Practical write a c program to reverse a given numberPractical write a c program to reverse a given number
Practical write a c program to reverse a given number
 
Practical write a c program to reverse a given number
Practical write a c program to reverse a given numberPractical write a c program to reverse a given number
Practical write a c program to reverse a given number
 
Itp practical file_1-year
Itp practical file_1-yearItp practical file_1-year
Itp practical file_1-year
 
Best C Programming Solution
Best C Programming SolutionBest C Programming Solution
Best C Programming Solution
 
C file
C fileC file
C file
 
Progr3
Progr3Progr3
Progr3
 
C
CC
C
 
Linux_C_LabBasics.ppt
Linux_C_LabBasics.pptLinux_C_LabBasics.ppt
Linux_C_LabBasics.ppt
 
In C Programming create a program that converts a number from decimal.docx
In C Programming create a program that converts a number from decimal.docxIn C Programming create a program that converts a number from decimal.docx
In C Programming create a program that converts a number from decimal.docx
 
C lab programs
C lab programsC lab programs
C lab programs
 
C lab programs
C lab programsC lab programs
C lab programs
 
C Language Programs
C Language Programs C Language Programs
C Language Programs
 
Unit-IV.pptx
Unit-IV.pptxUnit-IV.pptx
Unit-IV.pptx
 
comp2
comp2comp2
comp2
 
'C' language notes (a.p)
'C' language notes (a.p)'C' language notes (a.p)
'C' language notes (a.p)
 
C programs
C programsC programs
C programs
 

More from Zaibi Gondal

Modal Verbs
Modal VerbsModal Verbs
Modal Verbs
Zaibi Gondal
 
Parts of speech1
Parts of speech1Parts of speech1
Parts of speech1
Zaibi Gondal
 
Wirless Security By Zohaib Zeeshan
Wirless Security By Zohaib ZeeshanWirless Security By Zohaib Zeeshan
Wirless Security By Zohaib Zeeshan
Zaibi Gondal
 
C project on a bookshop for saving of coustmer record
C project on a bookshop for saving of coustmer recordC project on a bookshop for saving of coustmer record
C project on a bookshop for saving of coustmer record
Zaibi Gondal
 
Backup data
Backup data Backup data
Backup data
Zaibi Gondal
 
Functional english
Functional englishFunctional english
Functional english
Zaibi Gondal
 
application of electronics in computer
application of electronics in computerapplication of electronics in computer
application of electronics in computer
Zaibi Gondal
 
Model Verbs
Model VerbsModel Verbs
Model Verbs
Zaibi Gondal
 

More from Zaibi Gondal (8)

Modal Verbs
Modal VerbsModal Verbs
Modal Verbs
 
Parts of speech1
Parts of speech1Parts of speech1
Parts of speech1
 
Wirless Security By Zohaib Zeeshan
Wirless Security By Zohaib ZeeshanWirless Security By Zohaib Zeeshan
Wirless Security By Zohaib Zeeshan
 
C project on a bookshop for saving of coustmer record
C project on a bookshop for saving of coustmer recordC project on a bookshop for saving of coustmer record
C project on a bookshop for saving of coustmer record
 
Backup data
Backup data Backup data
Backup data
 
Functional english
Functional englishFunctional english
Functional english
 
application of electronics in computer
application of electronics in computerapplication of electronics in computer
application of electronics in computer
 
Model Verbs
Model VerbsModel Verbs
Model Verbs
 

Recently uploaded

Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
Thiyagu K
 
Additional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdfAdditional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdf
joachimlavalley1
 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
EverAndrsGuerraGuerr
 
Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
Jisc
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Thiyagu K
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
EugeneSaldivar
 
special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdf
Special education needs
 
How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17
Celine George
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
Pavel ( NSTU)
 
Fish and Chips - have they had their chips
Fish and Chips - have they had their chipsFish and Chips - have they had their chips
Fish and Chips - have they had their chips
GeoBlogs
 
Basic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumersBasic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumers
PedroFerreira53928
 
ESC Beyond Borders _From EU to You_ InfoPack general.pdf
ESC Beyond Borders _From EU to You_ InfoPack general.pdfESC Beyond Borders _From EU to You_ InfoPack general.pdf
ESC Beyond Borders _From EU to You_ InfoPack general.pdf
Fundacja Rozwoju Społeczeństwa Przedsiębiorczego
 
Polish students' mobility in the Czech Republic
Polish students' mobility in the Czech RepublicPolish students' mobility in the Czech Republic
Polish students' mobility in the Czech Republic
Anna Sz.
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
Tamralipta Mahavidyalaya
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
siemaillard
 
MARUTI SUZUKI- A Successful Joint Venture in India.pptx
MARUTI SUZUKI- A Successful Joint Venture in India.pptxMARUTI SUZUKI- A Successful Joint Venture in India.pptx
MARUTI SUZUKI- A Successful Joint Venture in India.pptx
bennyroshan06
 
The approach at University of Liverpool.pptx
The approach at University of Liverpool.pptxThe approach at University of Liverpool.pptx
The approach at University of Liverpool.pptx
Jisc
 
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
Nguyen Thanh Tu Collection
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
Vikramjit Singh
 
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXXPhrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
MIRIAMSALINAS13
 

Recently uploaded (20)

Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
 
Additional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdfAdditional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdf
 
Thesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.pptThesis Statement for students diagnonsed withADHD.ppt
Thesis Statement for students diagnonsed withADHD.ppt
 
Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
 
special B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdfspecial B.ed 2nd year old paper_20240531.pdf
special B.ed 2nd year old paper_20240531.pdf
 
How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17
 
Synthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptxSynthetic Fiber Construction in lab .pptx
Synthetic Fiber Construction in lab .pptx
 
Fish and Chips - have they had their chips
Fish and Chips - have they had their chipsFish and Chips - have they had their chips
Fish and Chips - have they had their chips
 
Basic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumersBasic phrases for greeting and assisting costumers
Basic phrases for greeting and assisting costumers
 
ESC Beyond Borders _From EU to You_ InfoPack general.pdf
ESC Beyond Borders _From EU to You_ InfoPack general.pdfESC Beyond Borders _From EU to You_ InfoPack general.pdf
ESC Beyond Borders _From EU to You_ InfoPack general.pdf
 
Polish students' mobility in the Czech Republic
Polish students' mobility in the Czech RepublicPolish students' mobility in the Czech Republic
Polish students' mobility in the Czech Republic
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
 
MARUTI SUZUKI- A Successful Joint Venture in India.pptx
MARUTI SUZUKI- A Successful Joint Venture in India.pptxMARUTI SUZUKI- A Successful Joint Venture in India.pptx
MARUTI SUZUKI- A Successful Joint Venture in India.pptx
 
The approach at University of Liverpool.pptx
The approach at University of Liverpool.pptxThe approach at University of Liverpool.pptx
The approach at University of Liverpool.pptx
 
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
GIÁO ÁN DẠY THÊM (KẾ HOẠCH BÀI BUỔI 2) - TIẾNG ANH 8 GLOBAL SUCCESS (2 CỘT) N...
 
Digital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and ResearchDigital Tools and AI for Teaching Learning and Research
Digital Tools and AI for Teaching Learning and Research
 
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXXPhrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
 

Programming fundamentals

  • 1. 1 Assignment #4 Subject: Programming Fundamentals Semester: 1st Submitted To: Sir. Junaid Submitted By: Zohaib Zeeshan Roll No: BSSE-F17-57 Date: 22/01/2018 Department of CS&IT (BSSE) University Of Sargodha Mandi Bahauddin Campus
  • 2. 2 Programming Fundamentals (1). Printing text on screen: a). Write a simple C program. Code: #include<stdio.h> int main(void) { printf(“Welcome to C!”); return 0; } Output: b). Write a program to find sum of two integers. Code: #include<stdio.h> int main() { int a=5; int b=7; int sum; sum = a+b; printf("The sum is = %d", sum); } Output: (2). Write two code examples of if-else. a). Find maximum between two numbers. Code: #include<stdio.h> int main() { int num1, num2; printf("Enter two integers to find which is maximumn"); scanf("%d%d", &num1, &num2); if(num1 > num2){ printf("First numbers is maximumn"); } else { printf("Second is maximumn"); } return 0; }
  • 3. 3 Output: b). Write a program to check an integer is evenor odd. Code: #include<stdio.h> int main() { int num; printf("Enter an integer to check even or oddn"); scanf("%d", &num); if(num % 2 == 0) { printf("Evenn"); } else { printf("Oddn"); } } Output: (3). Write two code examples of switch statement. a). Write a code to check an alphabet is vowel or consonant. Code: #include<stdio.h> int main () { char ch; printf("Enter an alphabetn"); scanf("%c", &ch); switch (ch) { case'a': printf("a is voweln"); break; case 'e': printf("e is voweln"); break;
  • 4. 4 case 'i': printf("i is voweln"); break; case 'o': printf("o is voweln"); break; case 'u': printf("u is woweln"); break; case 'A': printf("A is voweln"); break; case 'E': printf("E is voweln"); break; case 'I': printf("I is voweln"); break; case 'O': printf("O is voweln"); break; case 'U': printf("U is voweln"); break; default: printf("is consonant"); } return 0; } Output: b). Write a code to check number is evenor odd. Code: #include<stdio.h> int main() { int num; printf("Enter a number to check even or oddn"); scanf("%d", &num); switch(num % 2){
  • 5. 5 case 0: printf("Number is Evenn"); break; case 1: printf("Number is Oddn"); break; } } Output: (4). Write two code examples of For Loop. a). C program to find power ofa number using for loop. Code: #include<stdio.h> int main(){ int base,exponent; int power = 1; int i; printf("Enter base: n"); scanf("%d", &base); printf("Enter exponenet: n"); scanf("%d", &exponent); for(i=1; i<=exponent; i++){ power=power*base; } printf("%d ^ %d = %d", base, exponent, power); return 0; } Output: b). C program to print all even numbers from 1 to n. Code: #include<stdio.h> int main(){ int i,n; printf("Print all even numbers: n"); scanf("%d", &n); printf("Even numbers from 1 to %d are n", n);
  • 6. 6 for(i=1; i<=n; i++){ if(i%2 == 0){ printf("%d ", i); } } return 0; } Output: (5). Write two code examples using while loop. a). C program to print multiplication table ofa number using while loop. Code: #include <stdio.h> int main() { int i, num; printf("Enter number to print table: "); scanf("%d", &num); while(i <=10) { printf("%d * %d = %dn", num, i, (num*i)); i++; } return 0; } Output: b). Write a program to genrate star pattern as shown below using while loop. Code: #include<stdio.h> int main() { int i,j;
  • 7. 7 i=1; while(i<=5){ printf(""); j=1; while(j<=i) { printf("*"); j++; } printf("n"); i++; } return 0; } Output: (6).Write two code examples using do while loop. a). Value of a using do while loop. Code: #include <stdio.h> int main(){ int a = 0; // do loop execution do { printf("value of a: %dn", a); a++; } while( a <= 5 ); return 0; } Output: b). C program to print the table of 5 from 1 to 10. Code: #include<stdio.h> int main() { int i=1; do { printf("5 * %d = %dn",i,5*i);
  • 8. 8 i++; } while(i<=10); return 0; } Output: (7). Write two code examples of Functions. a). C program to find cube ofa number using function. Code: #include <stdio.h> /* Function declaration */ int cube(int num); int main(){ int num; int c; printf("Enter any number: "); scanf("%d", &num); c = cube(num); printf("Cube of %d is %d", num, c); return 0; } int cube(int num) { return (num * num * num); } Output: b). Find factorial of a number using function. Code: #include<stdio.h> int factorial(int); int main(){ int fact; int numbr; printf("Enter a number: "); scanf("%d",&numbr);
  • 9. 9 fact= factorial(numbr); printf("Factorial of %d is: %d",numbr,fact); return 0; } int factorial(int n){ int i; int factorial; factorial =1; for(i=1;i<=n;i++) factorial=factorial*i; return(factorial); } Output: (8). Write two code examples of Array. a). Write a program to find repeated elements using array. Code: #include<stdio.h> int main(){ int i,arr[20],j,num; printf("Enter size of array: "); scanf("%d",&num); printf("Enter any %d elements in array: ",num); for(i=0;i<num;i++) { scanf("%d",&arr[i]); } printf("Repeated elements are: n"); for(i=0; i<num; i++) { for(j=i+1;j<num;j++) { if(arr[i]==arr[j]) { printf("%dn",arr[i]); } } } return 0; } Output:
  • 10. 10 b). Find largest element using array. Code: #include <stdio.h> int main() { int array[50], size, i, largest; printf("Enter the size of the array: n"); scanf("%d", &size); printf("Enter %d elements of the array: n", size); for(i=0; i<size; i++){ scanf("%d", &array[i]);} largest = array[0]; for (i = 1; i < size; i++) { if (largest < array[i]) largest = array[i]; } printf("The largest element is : %dn", largest); return 0; } Output: